忍者刘米米

Parse on Heroku

Configuration for heroku(required)

AppDelegate.swift

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {

// configuration of using Parse code in heroku
let parseConfig = ParseClientConfiguration {(ParseMutableClientConfiguration) in

//accessing heroku app via id & keys
ParseMutableClientConfiguration.applicationId = "instagramlyh"
ParseMutableClientConfiguration.clientKey = "instagramhc"
ParseMutableClientConfiguration.server = "http://fakeinstagram.herokuapp.com/parse"
}
Parse.initialize(with: parseConfig)

// Override point for customization after application launch.
return true
}

Save data to parse server

viewController.swift

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
// Unwrap image file data from UIImageView
let data = UIImageJPEGRepresentation(picture.image!, 5)

// Convert image to file
let file = PFFile(name: "picture.jpg", data: data!)

// Create a class in Heroku
let table = PFObject(className: "messages")
table["sender"] = "Akhmed"
table["receiver"] = "Bob"
table["message"] = "Hello Bob!"
table["picture"] = file

table.saveInBackground { (success:Bool, error:Error?) in
if success {
print("Saved in server")
}else{
print(error!)
}
}

Get data from parse server

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
let information = PFQuery(className: "messages")
information.findObjectsInBackground { (objects:[PFObject]?, error:Error?) in
// if no error...

if error == nil {

//take each object from list of objects.
for object in objects!{

let message = object["message"] as! String
let receiver = object["receiver"] as! String
let sender = object["sender"] as! String
let picture = object["picture"] as! PFFile

self.messageLbl.text = "Message:\(message)"
self.receiverLbl.text = "Receiver:\(receiver)"
self.senderLbl.text = "Sender:\(sender)"
picture.getDataInBackground(block: { (data:Data?, error:Error?) in

if error == nil {
if data != nil {
self.picture.image = UIImage(data:data!)
}
} else {
print(error!)
}
})
}
}else{
print(error!)
}
}
}