program234 program234 - 4 months ago 45
iOS Question

Facebook SDK for iOS Access Token Issue

I am trying to use Facebook in my iOS swift application to get some basic user information. I used Cocoapods to install the Facebook dependencies. I have updated my info.plist using the following links.

https://developers.facebook.com/docs/ios/ios9
https://developers.facebook.com/quickstarts/115956755431487/?platform=ios

I receive the following compiler errors whenever I try logging in through facebook:

-canOpenURL: failed for URL: "fbauth2:/" - error: "(null)"
-canOpenURL: failed for URL: "fbauth2:/" - error: "(null)"
Error: Error Domain=com.facebook.sdk.core Code=8 "(null)" UserInfo={com.facebook.sdk:FBSDKGraphRequestErrorCategoryKey=0, com.facebook.sdk:FBSDKGraphRequestErrorHTTPStatusCodeKey=400, com.facebook.sdk:FBSDKErrorDeveloperMessageKey=An active access token must be used to query information about the current user., com.facebook.sdk:FBSDKGraphRequestErrorGraphErrorCode=2500, com.facebook.sdk:FBSDKGraphRequestErrorParsedJSONResponseKey={
body = {
error = {
code = 2500;
"fbtrace_id" = F1CmQQhd4pA;
message = "An active access token must be used to query information about the current user.";
type = OAuthException;
};
};
code = 400;
}}


Podfile

source 'https://github.com/CocoaPods/Specs.git'
platform :ios, '9.0'
use_frameworks!
pod 'FBSDKCoreKit', '~> 4.8'
pod 'FBSDKLoginKit', '~> 4.8'
pod 'FBSDKShareKit', '~> 4.8'


ViewController with FBSDKLoginButtonDelegate

override func viewDidLoad() {
super.viewDidLoad()
var loginButton = FBSDKLoginButton()
loginButton.readPermissions = ["user_about_me", "email", "public_profile"]
loginButton.center = self.view.center
loginButton.delegate = self
self.view.addSubview(loginButton)
}

func loginButton(loginButton: FBSDKLoginButton!, didCompleteWithResult result: FBSDKLoginManagerLoginResult!, error: NSError!) {
let fields = "email, name, first_name, last_name"
let graphRequest : FBSDKGraphRequest = FBSDKGraphRequest(graphPath: "me", parameters: ["fields" : fields])
graphRequest.startWithCompletionHandler({ (connection, result, error) -> Void in
if ((error) != nil) {
print("Error: \(error)")
}
})
}


App Delegate

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
return FBSDKApplicationDelegate.sharedInstance().application(application, didFinishLaunchingWithOptions: launchOptions)
}

func application(application: UIApplication, openURL url: NSURL, sourceApplication: String?, annotation: AnyObject) -> Bool {
return FBSDKApplicationDelegate.sharedInstance().application(application, openURL: url, sourceApplication: sourceApplication, annotation: annotation)
}

func applicationDidBecomeActive(application: UIApplication) {
FBSDKAppEvents.activateApp()
}


Other Information: Swift 2, Xcode 7, iOS 9

Any suggestions would be appreciated to resolve this issue.

Answer

My mistake was that I had both of the following functions within my AppDelegate. openURL method with sourceApplication is deprecated so the application would use the updated method. However, Facebook's own documentation states to use the deprecated version which lead to my mistake located here: https://developers.facebook.com/docs/ios/getting-started

func application(application: UIApplication, openURL url: NSURL, sourceApplication: String?, annotation: AnyObject) -> Bool {
        return FBSDKApplicationDelegate.sharedInstance().application(application, openURL: url, sourceApplication: sourceApplication, annotation: annotation)
}

func application(app: UIApplication, openURL url: NSURL, options: [String : AnyObject]) -> Bool {
}

Facebook Stating to Use Deprecated openURL http://imgur.com/kwI5VId