2016-08-28 6 views
0

FBSDKAccessToken currentAccessToken nil after quitting appios9 : FBSDK currentAccessToken 전무

나는 많은 관련 스레드를 보았고, 내 경우에 해결책은 여전히 ​​식별 할 수없는 것입니다. 응용 프로그램에

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool { 

    FBSDKApplicationDelegate.sharedInstance().application(application, didFinishLaunchingWithOptions: launchOptions) 

    if let accessToken = FBSDKAccessToken.currentAccessToken(){ 
    print(accessToken) 
    }else{ 
    print("Not logged In.") 
    } 

    return true 
} 

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


return GIDSignIn.sharedInstance().handleURL(url, 
              sourceApplication: options[UIApplicationOpenURLOptionsSourceApplicationKey] as? String, 
              annotation: options[UIApplicationOpenURLOptionsAnnotationKey]) 


} 


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

그래서 바로이 accessTokennil입니다 시작 :

여기 내 AppDelegate.swift 파일입니다.

액세스 토큰을 새로 고치려고하면 nil 사용자 액세스 토큰을 다시 작성할 수 없기 때문에 액세스 토큰을 새로 고칩니다. 내가 만료 된 사용자 토큰을 캐시했을 수도 있습니다. 현재 사용자 토큰이 만료 되더라도 FBSDK를 호출해야하기 때문에 여전히 이상합니다. 제 경우에는 nil을 반환합니다. 나는 라이 SO 올리기 권리 AppDelegate 방법에 내 FBSDK 위임 호출을 넣어되지 않은 : Facebook SDK login never calls back my application on iOS 9

여기

내가 나중에 참조 할 수 있도록 내 currentAccessToken

func fetchFBInfoForLogin(){ 
let parameters = ["fields": "email, first_name, last_name"] 

if (FBSDKAccessToken.currentAccessToken() != nil) { 
    print("current access token is not nil") 
    // User is logged in, do work such as go to next view controller. 
    let graphRequest : FBSDKGraphRequest = FBSDKGraphRequest(graphPath: "me", parameters: nil) 
    graphRequest.startWithCompletionHandler({ (connection, result, error) -> Void in 

    if ((error) != nil) 
    { 
     // Process error 
     print("Error: \(error)") 
    } 
    else 
    { 
     print(result.valueForKey("id") as? String) 
     print(result.valueForKey("name") as? String) 
     print(result.valueForKey("email") as? String) 

    } 
    }) 

} else { 

    //refresh 

    FBSDKAccessToken.refreshCurrentAccessToken{ (connection, result, error) -> Void in 
    if error != nil { 
     print(error) 
     return 
    } 
    print(FBSDKAccessToken.currentAccessToken()) 
    print("refresh doesn't work?") 
    let parameters = ["fields": "email, first_name, last_name"] 
    let graphRequest : FBSDKGraphRequest = FBSDKGraphRequest(graphPath: "me", parameters: parameters) 
    graphRequest.startWithCompletionHandler({ (connection, result, error) -> Void in 

     if ((error) != nil) 
     { 
     // Process error 
     print("Error: \(error)") 
     } 
     else 
     { 
     print(result.valueForKey("id") as? String) 
     print(result.valueForKey("name") as? String) 
     print(result.valueForKey("email") as? String) 

     } 
    }) 

    } 
} 
} 

를 사용하려고하는 방법이다 따라서 수동으로 FB 로그인을 닫아야하는 이유는 콜백 결과가 result.isCancelled이되어서 토큰을받지 못하는 이유입니다.

답변

1

로그인 및 로그 아웃 이벤트의 알림을 받으려면 FBSDKLoginButtonDelegate 프로토콜을 구현하십시오.

class FirstViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, FBSDKLoginButtonDelegate { 

    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 { 
        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)") 
        } 
       } 
      }) 
     } 
    } 
} 

HTH.

+0

FBSDK 대리자 메서드 처리기에서 반환하는 'result.isCancelled' 사례를 처리하지 않았습니다. 그게 무슨 뜻 이죠? – Thalatta

+1

나는 정직하지 않을 것이다. :-) 코드가 어디서 왔는지는 기억이 나지 않습니다. 아마도 2를 신속하게 수정했을 것입니다. 일반적으로 오류 또는 확인 중 하나입니다. 가장 자주 확인됩니다. – kometen