2016-09-15 6 views
1

私はIOS9でFacebook SDKを迅速に使用していますが、私はaceestokenを取得していません。私はviewcontroller.swiftファイルにログインコードを書いています。私はすべての設定を私のinfo.plistファイルにも行っていますが、常にaccesstokenにはゼロの値を得ています。何のアクセストークンが存在しない場合はここでIOS SwiftでFBSDKAccessTokenを取得する方法

は私のコードは

AppDelegate.swift

@UIApplicationMain 
class AppDelegate: UIResponder, UIApplicationDelegate { 
    var window: UIWindow? 
    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool { 

    FBSDKLoginButton.classForCoder() 
    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) 
    } 
} 

ViewController.swift

class ViewController: UIViewController,FBSDKLoginButtonDelegate { 


override func viewDidLoad() { 
      super.viewDidLoad() 
      //outlet for fbsdkloginbutton 
      @IBOutlet weak var facebookLoginButton: FBSDKLoginButton! 
     var error:NSError? 

       //check for error 
       if error != nil{ 
            print(error) 
            return 
           } 
        //Check for the access token 
           if (FBSDKAccessToken.currentAccessToken() == nil){ 
            print("USER NOT LOGGED IN") 
           }else{ 
            print("USER LOGGED IN") 
           } 
       //set delegate for button 
         self.facebookLoginButton.delegate = self 
           //read permission 
           facebookLoginButton.readPermissions = ["public_profile","user_friends","email"] 
          } 
         func loginButton(loginButton: FBSDKLoginButton!, didCompleteWithResult result: FBSDKLoginManagerLoginResult!, error: NSError!) { 
           /* 
           Check for successful login and act accordingly. 
           Perform your segue to another UIViewController here. 
           */ 
         if (error != nil){ 
            print(error.localizedDescription) 
           } 

           if let userToken = result.token{ 
            // GET USER TOKEN HERE 
            let token:FBSDKAccessToken = userToken 
            print(token) 
            //print token id and user id 
            print("TOKEN IS \(FBSDKAccessToken.currentAccessToken().tokenString)") 
            print("USER ID IS \(FBSDKAccessToken.currentAccessToken().userID)") 

           } 

          } 
          func applicationWillTerminate(application: UIApplication) 
          { 
           // Called when the application is about to terminate.  Save data if 
          } 


          func loginButtonDidLogOut(loginButton: FBSDKLoginButton!) { 
           // Actions for when the user logged out goes here 
          } 

         } 
+0

どのようにアクセストークンを取得しようとしていますか? – donnywals

答えて

0
FBSDKAccessToken.currentAccessToken() 

でNULLを返し

0

ViewControllerにデリゲート関数を実装する必要があります。このsiteは非常に役に立ちます。 HTH。

func loginButton(loginButton: FBSDKLoginButton!, didCompleteWithResult result: FBSDKLoginManagerLoginResult!, error: NSError!) { 
    if ((error) != nil) { 
     // Process error 
    } else if result.isCancelled { 
     // Handle cancellations 
    } else { 
     let graphRequest : FBSDKGraphRequest = FBSDKGraphRequest(graphPath: "me", parameters: ["fields" : "id, email, name"]) 
     graphRequest.startWithCompletionHandler({ (connection, result, error) -> Void in 
      if ((error) != nil) { 
       print("Error: \(error)") 
      } else { 
       // Do work in app. 
       let token = FBSDKAccessToken.currentAccessToken().tokenString 
       let loginProvider = CustomIdentityProvider(tokens: ["graph.facebook.com": token!]) 
       if let user : NSString = result!.valueForKey("name") as? NSString { 
        print("user: \(user)") 
       } 
       if let id : NSString = result!.valueForKey("id") as? NSString { 
        print("id: \(id)") 
       } 
       if let email : NSString = result!.value(forKey: "email") as? NSString { 
        print("email: \(email)") 
       } 
      } 
     }) 
    } 
} 
関連する問題