swift 3 facebook sdk get friends list - ios

I need to get all my friends list:
I found this code:
var fbRequestFriends: FBSDKGraphRequest = FBSDKGraphRequest(graphPath: "/{friend-list-id}", parameters: [AnyHashable : Any]())
fbRequestFriends.start { (connection, result, error) in
if error == nil && result != nil {
print("Request Friends result : \(result!)")
} else {
print("Error \(error)")
}
}
But in return I get this error:
Error Optional(Error Domain=NSURLErrorDomain Code=-1002 "unsupported
URL"
UserInfo={NSErrorFailingURLStringKey=(null)?access_token=EAAZAaT5Yj9qQBABsXA5KGpwvGX6wIdgX6h9mLtzz6YtgjiOB12XwWxl1MrDJLtZChWSOOKXDiizZCu7uZAixBmqP97HYUXkeg3lkTeeeyjmvhZBOZBibrp2Exa9uZB7V5fflJTvISF2aqK94H7E52svqei6SHvxi3W9vcynSI8hPQBBSawND9NHQif0wYIPuHIpbzl6ErBErvk6nm4ysMo1ZA0BFHVmflVYZD&format=json&include_headers=false&sdk=ios,
NSErrorFailingURLKey=(null)?access_token=EAAZAaT5Yj9qQBABsXA5KGpwvGX6wIdgX6h9mLtzz6YtgjiOB12XwWxl1MrDJLtZChWSOOKXDiizZCu7uZAixBmqP97HYUXkeg3lkTeeeyjmvhZBOZBibrp2Exa9uZB7V5fflJTvISF2aqK94H7E52svqei6SHvxi3W9vcynSI8hPQBBSawND9NHQif0wYIPuHIpbzl6ErBErvk6nm4ysMo1ZA0BFHVmflVYZD&format=json&include_headers=false&sdk=ios,
NSLocalizedDescription=unsupported URL, NSUnderlyingError=0x7a6285b0
{Error Domain=kCFErrorDomainCFNetwork Code=-1002 "unsupported URL"
UserInfo={NSErrorFailingURLKey=(null)?access_token=EAAZAaT5Yj9qQBABsXA5KGpwvGX6wIdgX6h9mLtzz6YtgjiOB12XwWxl1MrDJLtZChWSOOKXDiizZCu7uZAixBmqP97HYUXkeg3lkTeeeyjmvhZBOZBibrp2Exa9uZB7V5fflJTvISF2aqK94H7E52svqei6SHvxi3W9vcynSI8hPQBBSawND9NHQif0wYIPuHIpbzl6ErBErvk6nm4ysMo1ZA0BFHVmflVYZD&format=json&include_headers=false&sdk=ios,
NSErrorFailingURLStringKey=(null)?access_token=EAAZAaT5Yj9qQBABsXA5KGpwvGX6wIdgX6h9mLtzz6YtgjiOB12XwWxl1MrDJLtZChWSOOKXDiizZCu7uZAixBmqP97HYUXkeg3lkTeeeyjmvhZBOZBibrp2Exa9uZB7V5fflJTvISF2aqK94H7E52

#CBroe is right, this is how the code should look like
let params = ["fields": "id, first_name, last_name, name, email, picture"]
let graphRequest = FBSDKGraphRequest(graphPath: "/me/friends", parameters: params)
let connection = FBSDKGraphRequestConnection()
connection.add(graphRequest, completionHandler: { (connection, result, error) in
if error == nil {
if let userData = result as? [String:Any] {
print(userData)
}
} else {
print("Error Getting Friends \(error)");
}
})
connection.start()

Related

Cannot fetch Facebook uploaded images

func fetchListOfUserPhotos() {
// let graphRequest = GraphRequest(graphPath: "me", parameters: ["fields": "picture"])
graphRequest.start(completionHandler: { [self] (_, result, error) in
if error != nil {
print(error as Any)
} else {
print(result)
}
})
}
getting empty data.. please help me to resolve this issue.

Facebook Graph API Issues

i am implementing facebook graph APi for get posts list but i am getting error in this Here Is my Code
var Requset : FBSDKGraphRequest
print("\(FBSDKAccessToken.current())")
let acessToken = String(format:"%#", FBSDKAccessToken.current().tokenString) as String
print("\(acessToken)")
let parameters1 = ["access_token":FBSDKAccessToken.current().tokenString]
Requset = FBSDKGraphRequest(graphPath:"me/?fields=feed{message,full_picture,id,application,likes{name,pic_crop},comments{message,from,created_time},created_time}", parameters:parameters1, httpMethod:"GET")
Requset.start(completionHandler: { (connection, result, error) -> Void in
//MBProgressHUD.hideHUDForView(appDelegate.window, animated: true)
if ((error) != nil)
{
print("Error: \(error)")
}
else
{
print("fetched user: \(result)")
//var dataDict: AnyObject = (result! as AnyObject).object(forKey: "data")!
}
i am getting error in this error is given below
Error: Optional(Error Domain=NSURLErrorDomain Code=-1002 "unsupported URL" UserInfo={NSUnderlyingError=0x60000005fbf0 {Error Domain=kCFErrorDomainCFNetwork Code=-1002 "(null)"}, NSErrorFailingURLStringKey=(null)?
can any one tell me how to solve this

iOS facebook graph api comments

Is there way to fetch facebook comments with graph api without logging. When user is logged in comments can be fetch without problem, first I fetch user comment box object from web and after that make second request with id of comment box. If user is logged out in first request completition handler returns error The operation couldn’t be completed. (com.facebook.sdk.core error 8.)
let request = FBSDKGraphRequest(graphPath: "http://testapi.club/test.php", parameters: nil, HTTPMethod: "GET")
request.startWithCompletionHandler { (connection, result, error) in
if error == nil {
print(result)
let og_object = (result as! Dictionary<String, AnyObject>)["og_object"]
let objecId = (og_object as! Dictionary<String, AnyObject>)["id"]
let requestForComments = FBSDKGraphRequest(graphPath: "/\(objecId!)/comments", parameters: ["summary": "true","filter": "toplevel", "order":"chronological"], HTTPMethod: "GET")
requestForComments.startWithCompletionHandler({ (connection, result, error) in
if error != nil {
print(error.description)
}else {
print(result)
}
})
}else{
print(error.localizedDescription)
}
}

Get data on Facebook user - Swift

I'm trying to get data on a facebook user by using his ID.
I have an array of facebook user ids called IDs, and I set the graphPath to: "/IDs[0]", when I run the code I get an error.
cam you tell me whats wrong with my code?
var pathIs: String = "/"
pathIs += IDs[0]
let graphRequest : FBSDKGraphRequest = FBSDKGraphRequest(graphPath: pathIs, parameters: ["fields": "id, name, birthday, picture.type(small)"])
graphRequest.startWithCompletionHandler({ (connection, result, error) -> Void in
if ((error) != nil)
{
// Process error
println("Error: \(error)")
}
else
{
println("fetched user: \(result)")
}
})
Thanks!

iOS Facebook SDK 4.0.1 getting user's email

I'm trying to get the user's email address with the followings:
fbLoginManager.logInWithReadPermissions(["email", "public_profile"], handler: { (loginResult, error) -> Void in
if error != nil {
//handle error
} else if loginResult.isCancelled {
//handle cancellation
} else {
//Logged in
self.loggedinUser.fbToken = FBSDKAccessToken.currentAccessToken().tokenString
var graphReq = FBSDKGraphRequest(graphPath: "me", parameters: nil).startWithCompletionHandler { (connection, result, error) -> Void in
if let user = result as? NSDictionary {
var email = result.objectForKey("email") as? String
var name = result.objectForKey("name") as? String
self.loggedinUser.email = email ?? nil
self.loggedinUser.fullName = name ?? nil
}
}
}
})
But in the loginResult object the user's email is not sent. Any idea how to solve this?
thanks
You must provide in the parameters what you want to get:
FBSDKGraphRequest(graphPath: "me", parameters: ["fields": "email"]).startWithCompletionHandler({ (connection, result, error) -> Void in
For example to get more than one field:
parameters: ["fields": "id, name, email, picture.type(large)"]

Resources