Facebook Graphrequest for Swift 5 and Facebook Sdk 5

facebook graph request using swift 5

I got the same behavior. I am not that experienced in Swift and the Facebook Graph API and I'm not sure if it is a good solution but, currently (until I find a better solution), for me works:

connection.add(GraphRequest(graphPath: "/me", parameters: ["fields":"email"])) { httpResponse, result, error   in
if error != nil {
NSLog(error.debugDescription)
return
}

// Handle vars
if let result = result as? [String:String],
let email: String = result["email"],
let fbId: String = result["id"] {

// internal usage of the email
self.userService.loginWithFacebookMail(facebookMail: email)

}

}

Converting Graph API from Swift 3 to Swift 5 for Facebook SDK

        func getFacebookProfileInfo() {
let requestMe = GraphRequest.init(graphPath: "me", parameters: ["fields" : "id,name,email,picture.type(large)"])
let connection = GraphRequestConnection()
connection.add(requestMe, completionHandler: { (connectn, userresult, error) in
if let dictData: [String : Any] = userresult as? [String : Any] {
DispatchQueue.main.async {
if let pictureData: [String : Any] = dictData["picture"] as? [String : Any] {
if let data : [String: Any] = pictureData["data"] as? [String: Any] {

print(data)
print(dictData["email"]!)

}
}
}
}
})
connection.start()
}

Facebook Graph API: Swift SDK not working: GraphRequest

However i implement facebook login my app which works fine.

On button click I called facebook login using LoginManager.

@IBAction func continueWithFacebook(_ sender: Any) {
let fbLoginManager : LoginManager = LoginManager()
fbLoginManager.logIn(permissions: ["email"], from: self) { (result, error) -> Void in
if (error == nil){
let fbloginresult : LoginManagerLoginResult = result!
if (result?.isCancelled)!{
return
}
if(fbloginresult.grantedPermissions.contains("email")) {
self.getFBUserData()
}
}
}
}

After that I get the data of user using GraphAP

func getFBUserData() {
if((AccessToken.current) != nil){
GraphRequest(graphPath: "me", parameters: ["fields": "id, name, first_name, last_name, picture.type(large), email"]).start(completionHandler: { (connection, result, error) -> Void in
if (error == nil){
guard let userDict = result as? [String:Any] else {
return
}
if let picture = userDict["picture"] as? [String:Any] ,
let imgData = picture["data"] as? [String:Any] ,
let imgUrl = imgData["url"] as? String {
self.uURLString = imgUrl
/*let url = URL(string: imgUrl)*/
print(imgData)
}
if let n = userDict["name"] as? String {
self.name = n
}
if let e = userDict["email"] as? String {
self.email = e
}
self.socialLogIn()
}
})
}
}

Add in your pod file -
pod 'FBSDKCoreKit' and
pod 'FBSDKLoginKit'

Try using like this you will get your derided result.
Happy Coding

Showing a Facebook User Picture using the URL (Swift 5 | Xcode 10)

Try the below code.

func getFacebookProfileInfo() {
let requestMe = GraphRequest.init(graphPath: "me", parameters: ["fields" : "id,name,email,picture.type(large)"])

let connection = GraphRequestConnection()

connection.add(requestMe, completionHandler:{ (connectn, userresult, error) in

if let dictData: [String : Any] = userresult as? [String : Any]
{
DispatchQueue.main.async
{
if let pictureData: [String : Any] = dictData["picture"] as? [String : Any]
{
if let data : [String: Any] = pictureData["data"] as? [String: Any]
{
print(data)
print(dictData["email"]!)

if let pictureURL = pictureData["url"] as? String { //image url of your image

if let url = URL(string: pictureURL) {

if let data = try? Data(contentsOf: url) { //here you get image data from url

//generate image from data and assign it to your profile imageview
imageView.image = UIImage(data: data)
}
}
}
}
}
}
}
})
connection.start()
}

I hope it will help you to get the image from the URL.

Facebook ios sdk Graph request allows only one field as parameter

I too got the same error when trying with the latest release. I suggest you to downgrade to the previous version. For me below versions are working fine:

pod 'FBSDKCoreKit', '~> 4.38.0' 
pod 'FBSDKLoginKit', '~> 4.38.0'

After that run

pod deintegrate

pod install

Swift version of Facebook iOS SDK and how to access data in Response

I faced this problem today as well. I got the user id and name inside MyProfileRequest

struct Response: GraphResponseProtocol {
init(rawResponse: Any?) {
// Decode JSON from rawResponse into other properties here.
guard let response = rawResponse as? Dictionary<String, Any> else {
return
}

if let name = response["name"],
let id = response["id"] {

print(name)
print(id)
}
}
}

EDIT: I redesigned my code like this to use the values in .success(let response) case

struct Response: GraphResponseProtocol {

var name: String?
var id: String?
var gender: String?
var email: String?
var profilePictureUrl: String?

init(rawResponse: Any?) {
// Decode JSON from rawResponse into other properties here.
guard let response = rawResponse as? Dictionary<String, Any> else {
return
}

if let name = response["name"] as? String {
self.name = name
}

if let id = response["id"] as? String {
self.id = id
}

if let gender = response["gender"] as? String {
self.gender = gender
}

if let email = response["email"] as? String {
self.email = email
}

if let picture = response["picture"] as? Dictionary<String, Any> {

if let data = picture["data"] as? Dictionary<String, Any> {
if let url = data["url"] as? String {
self.profilePictureUrl = url
}
}
}
}
}

And in the success case you can get the values like this:

let connection = GraphRequestConnection()
connection.add(MyProfileRequest()) { response, result in
switch result {
case .success(let response):
print("My facebook id is \(response.id!)") //Make sure to safely unwrap these :)
print("My name is \(response.name!)")
case .failed(let error):
print("Custom Graph Request Failed: \(error)")
}
}
connection.start()

Handle if user cancels Facebook-Login in Swift 5

You need to handle 3 cases

 if error != nil {
print("Facebook login failed. Error \(error)")
} else if result?.isCancelled == true {
print("Facebook login was cancelled.")
} else {
// clicked ok go on
}

Facebook Graph Request using Swift3 -

Browsing on the github issues, i found a solution.

https://github.com/facebook/facebook-sdk-swift/issues/63

Facebook documentation for Swift 3.0 and SDK 0.2.0 is not yet updated.

This works for me:

    let params = ["fields" : "email, name"]
let graphRequest = GraphRequest(graphPath: "me", parameters: params)
graphRequest.start {
(urlResponse, requestResult) in

switch requestResult {
case .failed(let error):
print("error in graph request:", error)
break
case .success(let graphResponse):
if let responseDictionary = graphResponse.dictionaryValue {
print(responseDictionary)

print(responseDictionary["name"])
print(responseDictionary["email"])
}
}
}

enjoy.



Related Topics



Leave a reply



Submit