2015-11-02 70 views
3

我正在使用最新的FBSDK,使用FBSDKLoginManager進行登錄。我有這一切工作,但有一件事我無法弄清楚爲什麼SFSafariViewController在登錄重定向後完全沒有響應?FBSDK FBSDKLoginManager和SFSafariViewController不會關閉

這裏是代碼:

- (void) login:(void(^)(NSError *error, NSString * token)) completion { 
    FBSDKLoginManager * manager = [[FBSDKLoginManager alloc] init]; 
    manager.loginBehavior = FBSDKLoginBehaviorNative; 

    if([FBSDKAccessToken currentAccessToken]) { 
     NSLog(@"already logged in"); 
     NSLog(@"%@",[FBSDKAccessToken currentAccessToken]); 
     completion(nil,[FBSDKAccessToken currentAccessToken].tokenString); 
     return; 
    } 

    [manager logInWithReadPermissions:@[@"public_profile"] fromViewController:nil handler:^(FBSDKLoginManagerLoginResult *result, NSError *error) { 
     dispatch_sync(dispatch_get_main_queue(), ^{ 
      NSLog(@"%@",error); 
      NSLog(@"fb token: %@",[FBSDKAccessToken currentAccessToken].tokenString); 
      if(error) { 
       completion(error,nil); 
       return; 
      } 
      completion(nil,[FBSDKAccessToken currentAccessToken].tokenString); 
     }); 
    }]; 
} 

這是發生了什麼:

  1. SFSafariViewController打開
  2. Facebook的提示登錄
  3. 回調發生在AppDelegate.openURL:選擇:(我回來的調用FBSDKApplicationDelegate.application:openURL:sourceApplication:annotation)。
  4. Safari網頁視圖控制器不做任何事情。

所以SFSafariViewController是白色/空白。完成按鈕什麼都不做。我無法關閉它。

我也沒有從loginManager.loginWithReadPermissions方法獲得回調。

Empty SFSafariViewController

任何人都有經驗嗎?

+0

面臨着同樣的錯誤。有一些進展? –

回答

0

您需要connect the app delegate

// AppDelegate.m 
#import <FBSDKCoreKit/FBSDKCoreKit.h> 

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { 
    [[FBSDKApplicationDelegate sharedInstance] application:application 
    didFinishLaunchingWithOptions:launchOptions]; 
    return YES; 
} 

- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation { 
    return [[FBSDKApplicationDelegate sharedInstance] application:application 
    openURL:url 
    sourceApplication:sourceApplication 
    annotation:annotation 
    ]; 
} 
+0

我這樣做,如果你看我的帖子#3我解釋我是怎麼稱呼它的。 – gngrwzrd

3

其實它們是兩個不同的OpenURL *回調,你需要實現,一個用於iOS8上,一個用於iOS9:

@available(iOS 8, *) 
func application(app: UIApplication, openURL url: NSURL, source: String?, annotation: AnyObject) -> Bool 
{ 
    return FBSDKApplicationDelegate.sharedInstance().application(app, openURL: url, sourceApplication: source, annotation: annotation) 
} 

@available(iOS 9, *) 
func application(app: UIApplication, openURL url: NSURL, options: [String : AnyObject]) -> Bool 
{ 
    let source = options[UIApplicationOpenURLOptionsSourceApplicationKey] as? String // For ex: com.apple.SafariViewService from a SFSafariViewController 
    let annotation = options[UIApplicationOpenURLOptionsAnnotationKey] 
    return FBSDKApplicationDelegate.sharedInstance().application(app, openURL: url, sourceApplication: source, annotation: annotation) 
}