ios - How to get the cover photo from Facebook + Swift + Parse -
i have log in system in app swift 2.0 integrated facebook, i'm able user informations , profile_picture.
i'm wondering how cover image user logged :
let requestparameters = ["fields": "id, email, first_name, last_name, name, gender, cover"] let userdetails = fbsdkgraphrequest(graphpath: "me", parameters: requestparameters) userdetails.startwithcompletionhandler { (connection, result, error:nserror!) -> void in if(error != nil) { print("\(error.localizeddescription)") return } if(result != nil) { let userid:string = result["id"] as! string let userfirstname:string? = result["first_name"] as? string let userlastname:string? = result["last_name"] as? string let useremail:string? = result["email"] as? string let username:string? = result["name"] as? string let usergender:string? = result["gender"] as? string let usercover:uiimage? = result["cover"] as? uiimage print(requestparameters) print(userdetails) print(usercover) print("\(useremail)") let myuser:pfuser = pfuser.currentuser()! // save first name if(userfirstname != nil) { myuser.setobject(userfirstname!, forkey: "firstnamecolumn") } //save last name if(userlastname != nil) { myuser.setobject(userlastname!, forkey: "lastnamecolumn") } // save email address if(useremail != nil) { myuser.setobject(useremail!, forkey: "email") } // save email address if(usergender != nil) { if (usergender == "male"){ myuser.setobject("masculino", forkey: "gendercolumn") } else { myuser.setobject("feminino", forkey: "gendercolumn") } } dispatch_async(dispatch_get_global_queue(dispatch_queue_priority_default, 0)) { // facebook profile picture let userprofile = "https://graph.facebook.com/" + userid + "/picture?type=large" let profilepictureurl = nsurl(string: userprofile) let profilepicturedata = nsdata(contentsofurl: profilepictureurl!) if(profilepicturedata != nil) { let profilefileobject = pffile(data:profilepicturedata!) myuser.setobject(profilefileobject!, forkey: "photousercolumn") } myuser.saveinbackgroundwithblock({ (success:bool, error:nserror?) -> void in if(success) { print("user details updated") } }) }
this code not working cover image.
any ideas?
here's working example using fb sdk in swift. posted answer in other question because answer came first me on google thought it'll nice put here well.
i needed cover photo of page in graphpath used page id. parameter can changed fit users / events / etc...
let request = fbsdkgraphrequest(graphpath: "\\(page.id)?fields=cover", parameters: [ "access_token": "your_access_token" ], httpmethod: "get") request.startwithcompletionhandler({(connection , result , error) in if ((error) != nil) { print("error in fetching cover photo \\(page.id): \(error)", terminator: "") } else { if let data = result["cover"] as? nsdictionary { self.fetchimagefromurl(data["source"] as! string, cb: {(image: uiimage) -> void in //do image }) } }) func fetchimagefromurl(url: string, cb: (uiimage) -> void) { let urlobj = nsurl(string: url)! dispatch_async(dispatch_get_global_queue(dispatch_queue_priority_default, 0)) { let data = nsdata(contentsofurl: urlobj) dispatch_async(dispatch_get_main_queue(), { let image = uiimage(data: data!)! cb(image) }); } }
p.s - i'm newbie in swift / ios code might not best. comments appreciated.
Comments
Post a Comment