i'm trying fetch user's fb profile pic wasn't able far. i'm trying simple: user log in fb account , app goes view appears name, email , profile picture. user's name , email okay, can't picture!
the app crashing actual code because apparently i'm unwrapping nil optional value, don't know why it's nil.
my code:
override func viewdidload() { super.viewdidload() // additional setup after loading view. let graphrequest : fbsdkgraphrequest = fbsdkgraphrequest(graphpath: "me", parameters: ["fields":"id,email,name,picture.width(480).height(480)"]) graphrequest.startwithcompletionhandler({ (connection, result, error) -> void in if ((error) != nil) { // process error print("error: \(error)") } else { print("fetched user: \(result)") let username : nsstring = result.valueforkey("name") as! nsstring print("user name is: \(username)") let useremail : nsstring = result.valueforkey("email") as! nsstring print("user email is: \(useremail)") let id = result.valueforkey("id") as! string self.namelabel.text = username string self.emaillabel.text = useremail string self.profilepic.image = self.getprofpic(id) } }) } func getprofpic(fid: string) -> uiimage? { if (fid != "") { let imgurlstring = "http://graph.facebook.com/" + fid + "/picture?type=large" //type=normal let imgurl = nsurl(string: imgurlstring) let imagedata = nsdata(contentsofurl: imgurl!) let image = uiimage(data: imagedata!) // code crashes in here return image } return nil }
from comments understand crashes @ image assigning, should doing conditional binding methodology of swift (if let) in order avoid unwrapping nil optional value:
if let data = result["picture"]?["data"] { if let url = data["url"] as? string { profilepictureurl = url } }
also, can see not using valueforkey method.
Comments
Post a Comment