Cannot convert value of type FBSDKGraphRequestHandler - ios

I'm getting the following error when I try to make a data request using the Facebook SDK:
Cannot convert value of type '(_,_,_) throws -> Void' to expected argument type 'FBSDKGraphRequestHandler!'
Here is the relevant code where the error is occurring. I found this in a tutorial but I think it worked with older versions of XCode. I'm using the latest version 7.2.1.
UPDATED
let graphRequest = FBSDKGraphRequest(graphPath: "me", parameters: ["fields": "id, name, gender, email"])
graphRequest.startWithCompletionHandler ({ [weak self] connection, result, error in
if error != nil {
//onError()
print(error)
return
} else {
//get results
PFUser.currentUser()?["gender"] = result["gender"]
PFUser.currentUser()?["name"] = result["name"]
try PFUser.currentUser()?.save()
let userId = result["id"] as! String
let facebookProfilePictureUrl = "https://graph.facebook.com/" + userId + "/picture?type=large"
if let fbpicUrl = NSURL(string: facebookProfilePictureUrl) {
if let data = NSData(contentsOfURL: fbpicUrl) {
self.profilePic.image = UIImage(data: data)
let imageFile:PFFile = PFFile(data: data)!
PFUser.currentUser()?["image"] = imageFile
try PFUser.currentUser()?.save()
}
}
}
})
Thanks!
PICTURE (click on the image to make it larger)
http://imgur.com/skCa1VB

I hope you are using the latest FBSDK in that the completionHandler: has been changed. See the below code:
let graphRequest = FBSDKGraphRequest(graphPath: "me", parameters: param)
graphRequest.startWithCompletionHandler { [weak self] connection, result, error in
if error != nil {
//onError()
print(error.description)
return
}else{
let fbResult = result as! Dictionary<String, AnyObject>
//Do You rest of the code here
}
})

Related

Use of undeclared type 'GraphRequestResult' after update of pods

I'm using the new facebook graph request and after updating pods I get an error
< Use of undeclared type 'GraphRequestResult'>
let graphRequest = GraphRequest(graphPath: kGraphPathMe, parameters: ["fields":"id,email,last_name,first_name,picture"], tokenString: accessToken.tokenString, version: .init(), httpMethod: .get)
graphRequest.start {(response: HTTPURLResponse?, result: GraphRequestResult<GraphRequest>) in
switch result {
case .success(let graphResponse):
if let dictionary = graphResponse.dictionaryValue {
completion(FacebookUser(jsonDict: dictionary))
}
break
default:
print("Facebook request user error")
}
}
check this code
let parameters = ["fields": "email, id, name"]
let graphRequest = FBSDKGraphRequest(graphPath: "me", parameters: parameters)
_ = graphRequest?.start { [weak self] connection, result, error in
// If something went wrong, we're logged out
if (error != nil) {
// Clear email, but ignore error for now
return
}
// Transform to dictionary first
if let result = result as? [String: Any] {
// Got the email; send it to Lucid's server
guard let email = result["email"] as? String else {
// No email? Fail the login
return
}
guard let username = result["name"] as? String else {
// No username? Fail the login
return
}
guard let userId = result["id"] as? String else {
// No userId? Fail the login
return
}
}
} // End of graph request

iOS - Swift Facebook Graph Request completion handler - Type of expression is ambiguous without more context

I'm trying to make this Facebook Graph request work but I get two errors :
Cannot conver value of type '(,,_) throws -> Void' to expected argument type 'FBSDKGraphRequestHandler!' - On line #6
Type of expression is ambiguous without more context - On line #14
This is my code for viewDidLoad() :
override func viewDidLoad() {
super.viewDidLoad()
let graphRequest = FBSDKGraphRequest(graphPath: "me", parameters: ["fields": "id, name, gender"])
graphRequest.startWithCompletionHandler( {
(connection, results, error) -> Void in
if error != nil {
print(error)
} else if let result = results {
PFUser.currentUser()?["gender"] = result["gender"]
PFUser.currentUser()?["name"] = result["name"]
try PFUser.currentUser()?.save()
let userId = result["id"] as! String
let facebookProfilePictureUrl = "https://graph.facebook.com/" + userId + "/picture?type=large"
if let fbpicUrl = NSURL(string: facebookProfilePictureUrl) {
if let data = NSData(contentsOfURL: fbpicUrl) {
self.profilePicture.image = UIImage(data: data)
let imageFile:PFFile = PFFile(data: data)!
PFUser.currentUser()?["image"] = imageFile
try PFUser.currentUser()?.save()
}}}
})
}
If you have any suggestion for how to fix that, it would really appreciated if you could let me know! Thanks.
Problem is you are using try without do-catch.
let graphRequest = FBSDKGraphRequest(graphPath: "me", parameters: ["fields": "id, name, gender"])
graphRequest.startWithCompletionHandler { (connection, results, error) -> Void in
if error != nil {
print(error)
} else if let result = results {
PFUser.currentUser()?["gender"] = result["gender"]
PFUser.currentUser()?["name"] = result["name"]
do {
try PFUser.currentUser()?.save()
} catch let ex {
print(ex)
}
let userId = result["id"] as! String
let facebookProfilePictureUrl = "https://graph.facebook.com/" + userId + "/picture?type=large"
if let fbpicUrl = NSURL(string: facebookProfilePictureUrl),
data = NSData(contentsOfURL: fbpicUrl) {
self.profilePicture.image = UIImage(data: data)
let imageFile:PFFile = PFFile(data: data)!
PFUser.currentUser()?["image"] = imageFile
do {
try PFUser.currentUser()?.save()
} catch let ex {
print(ex)
}
}
}
}
or you can use try? or try!
try? PFUser.currentUser()?.save()
But using save() method is not good choose. It lock current thread. Better if you will use saveInBackground with completion block if you need to be sure it saved successfully or saveEventually.
Btw. your code is not very well formatted and really hard to read...

Facebook iOS SDK and Swift: how to get user's hometown?

I am working with iOS 9.2 & Swift 2.1 & FBSDKVersion: 4.7.0.
I tried with Graph API Explorer, at that time I am getting the desired output.
The converted code is in Objective-C and I changed it to Swift.
let graphRequest : FBSDKGraphRequest = FBSDKGraphRequest(graphPath: "/me", parameters: ["fields": "hometown"], HTTPMethod: "GET")
graphRequest.startWithCompletionHandler({ (connection, result, error) -> Void in
if ((error) != nil){
print("Error: \(error)")
}
else{
print("fetched details: \(result)")
})
See below example and add hometown option into "fields" parameter of FBSDKGraphRequest object and also change:
func fetchUserProfile()
{
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)
{
print("Error took place: \(error)")
}
else
{
print("Print entire fetched result: \(result)")
let id : NSString = result.valueForKey("id") as! String
print("User ID is: \(id)")
if let userName = result.valueForKey("name") as? String
{
self.userFullName.text = userName
}
if let profilePictureObj = result.valueForKey("picture") as? NSDictionary
{
let data = profilePictureObj.valueForKey("data") as! NSDictionary
let pictureUrlString = data.valueForKey("url") as! String
let pictureUrl = NSURL(string: pictureUrlString)
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)) {
let imageData = NSData(contentsOfURL: pictureUrl!)
dispatch_async(dispatch_get_main_queue()) {
if let imageData = imageData
{
let userProfileImage = UIImage(data: imageData)
self.userProfileImage.image = userProfileImage
self.userProfileImage.contentMode = UIViewContentMode.ScaleAspectFit
}
}
}
}
}
})
}
Also refer to this link
Fetching user details from Facebook in iOS

Facebook SDK - Profile picture is throwing an unwrapping error

I use Parse with Facebook login so if I try to get the profile picture of the logged in user I get the error:
unexpectedly found nil while unwrapping an Optional value
I think the version of the SDK must be v4.5.1, and I use the Xcode 7 beta with Swift 2.
I use the code below to get the job done, but it won't run.
let pictureRequest = FBSDKGraphRequest(graphPath: "me/picture?type=large&redirect=false", parameters: ["fields": "url"])
pictureRequest.startWithCompletionHandler({ (connection, result, error: NSError!) -> Void in
if error == nil {
print("\(result)")
if let dictPic = result as? Dictionary<String, AnyObject> {
print(dictPic)
let url: String = dictPic["data{url}"] as AnyObject? as! String // <- this line is causing the error
print(url)
let URLRequest = NSURL(string: url)
let URLRequestNeeded = NSURLRequest(URL: URLRequest!) NSURLSession.sharedSession().dataTaskWithRequest(URLRequestNeeded, completionHandler: { (data, response, error) -> Void in
let picture = self.scaleImageWith(UIImage(data: data!)!, and: CGSizeMake(75, 75))
let pictureData = UIImagePNGRepresentation(picture)
let endPicture = PFFile(data: pictureData!)
PFUser.currentUser()!.setObject(endPicture, forKey: "picture")
PFUser.currentUser()!.saveInBackground()
})
}
} else {
print("\(error)")
}

Get array of facebook albums in swift using Facebook SDK 4.1

I'm attempting to fetch an array of a users facebook albums via the new Facebook SDK 4.1 using the following Swift code:
func getAlbumList()
{
var FBAlbums = [String]()
let graphRequest : FBSDKGraphRequest = FBSDKGraphRequest(graphPath: "me?fields=albums", parameters: nil)
graphRequest.startWithCompletionHandler({ (connection, result, error) -> Void in
if ((error) != nil)
{
// Process error
println("Error: \(error)")
}
else
{
println("fetched data: \(result)")
let graphData = result.valueForKey("data") as Array;
for obj:FBGraphObject in graphData{
let desc = obj.description;
println(desc);
let name = obj.valueForKey("name") as String;
println(name);
}
}
})
}
The FB SDK query is working ok as the result value contains all the data from facebook (it prints fine), however any attempt to actually turn this into an array is failling.
Using XCode 6.3 this gives the following errors:
let graphData = result.valueForKey("data") as Array; = 'AnyObject?' is not convertible to 'Array<T>'
for obj:FBGraphObject in graphData{ = Use of undeclared type 'FBGraphObject'
I can't seem to find any information about what has replaced FBGraphObject, or why I can't convert the result to an array.
Articles used as background (but out of date it seems):
Creating an Array of objects from the Facebook SDK in Swift
http://selise.ch/build-a-facebook-album-browser-using-swift/
Just change this line of your code:
let graphData = result.valueForKey("data") as Array;
To:
self.dict = result["data"] as! NSArray
This works for me retrieving user photos from facebook:
FBSDKGraphRequest(graphPath: "me/photos", parameters: ["fields": "id, name, source"]).startWithCompletionHandler({ (connection, result, error) -> Void in
if (error == nil){
self.dict = result["data"] as! NSArray
for item in self.dict { // loop through data items
if let urlString = item["source"]! {
let url = NSURL(string: urlString as! String)
let imageData = NSData(contentsOfURL: url!)
let image = UIImage(data: imageData!)
self.socialPhotoCollection.append(image!)
}
}
}
})
This is for getting all albums in a different way
Swift 3x:
FBSDKGraphRequest(graphPath: "/me/albums", parameters: ["fields":"id,name,user_photos"], httpMethod: "GET").start(completionHandler: { (connection, result, error) in
print(result!)
})

Resources