I am trying to create an API for login user on facebook, but I have this error:
Thread 1:EXC_BAD_INSTRUCTION(code=EXC_I386_INVOP,subcode=0x0)
on this line:
self.accessToken = jsonData["access_token"].string!
Console shows:
My Code:
fatal error: unexpectedly found nil while unwrapping an Optional value
(lldb)
func login(userType: String, completionHandler: @escaping (NSError?) -> Void) {
let path = "api/social/convert-token/"
let url = baseURL!.appendingPathComponent(path)
let params: [String: Any] = [
"grant_type": "convert_token",
"client_id": CLIENT_ID,
"client_secret": CLIENT_SECRET,
"backend": "facebook",
"token": FBSDKAccessToken.current().tokenString,
"user_type": userType
]
Alamofire.request(url!, method: .post, parameters: params, encoding: URLEncoding(), headers: nil).responseJSON { (response) in
switch response.result {
case .success(let value):
let jsonData = JSON(value)
self.accessToken = jsonData["access_token"].string!
self.refreshToken = jsonData["refresh_token"].string!
self.expired = Date().addingTimeInterval(TimeInterval(jsonData["expires_in"].int!))
completionHandler(nil)
break
case .failure(let error):
completionHandler(error as NSError?)
break
}
}
}
2
Answers
The problem was the app info in Constants.swift
If you decide to use FBSDK, you should use kit’s functions to get data from Facebook. FBSDK has integrated functions for working with the network.
Details
xCode 8.2.1, Swift 3
Full Sample: authorisation Facebook in Swift 3 FBSDK
add to plist
add to AppDelegate class
Result
You will be logged in and get user ID and name.
Read about
https://developers.facebook.com/docs/ios/getting-started
https://developers.facebook.com/docs/reference/ios/current/class/FBSDKLoginManager