没有调用Facebook登录完成块

时间:2016-07-28 10:27:50

标签: ios swift facebook fbsdk fbsdkloginkit

我尝试使用自定义登录按钮实现Facebook登录,我已完成所有设置步骤,当我点击按钮时,Web视图出现并要求我提供权限。

但是,如果我点击完成(在Web视图控制器的左上角),完成块被称为但是当我点击取消或确定时,Web视图控制器被解除但完成块永远不会被调用?< /强> enter image description here 这是我使用的代码:

    let fbLoginManager = FBSDKLoginManager()
    fbLoginManager.loginBehavior = .Native
    fbLoginManager.logInWithReadPermissions(["email"], fromViewController: self) { (result, error) in
        // put a breakpoint here but it won't stop here if I tap OK
        if error != nil {
            print(error.localizedFailureReason)
        } else if result.isCancelled {
            // dismiss view
        } else {
            let token = result.token.tokenString
            // do something with the token
        }
    }

编辑1

这是AppDelegate中的open url方法:

func application(app: UIApplication, openURL url: NSURL, options: [String : AnyObject]) -> Bool {
    return application(app, openURL: url, sourceApplication: nil, annotation: [:])
}
func application(application: UIApplication, openURL url: NSURL, sourceApplication: String?, annotation: AnyObject) -> Bool {
    if url.scheme == Key.FACEBOOK_URL_SCHEME {
        let handled = FBSDKApplicationDelegate.sharedInstance().application(application, openURL: url, sourceApplication: sourceApplication, annotation: annotation)
        return handled
    }
    let dynamicLink = FIRDynamicLinks.dynamicLinks()?.dynamicLinkFromCustomSchemeURL(url)
    if let dynamicLink = dynamicLink {
        if let url = dynamicLink.url {
            if let paths = url.pathComponents{
                if paths.count > 2 {
                    let slug = paths[2]
                    if let book = AppManager.instance.findBookBySlug(slug) {
                        // Open detail book
                        self.openDetailBook(book)
                    } else {
                        self.openDetailBookWithSlug(slug)
                    }
                }
            }
        }
    }
    if url.scheme == Key.DEEP_LINK_URL_SCHEME {
        if let paths = url.pathComponents, host = url.host {
            if host == "truyen" {
                if paths.count > 1 {
                    let slug = paths[1]
                    if let book = AppManager.instance.findBookBySlug(slug) {
                        // Open detail book
                        self.openDetailBook(book)
                    } else {
                        self.openDetailBookWithSlug(slug)
                    }
                }
            }
        }
    }
    return true
}

3 个答案:

答案 0 :(得分:2)

如果您遇到过这种情况,请自行调试,因为Facebook SDK是开源的。您看到的窗口是SFSafariViewController。当您点击其中任何一个按钮时,回拨将由SFSafariViewController处理。在Facebook sdk中查找SFSafariViewController delegate方法。

在调用登录代码后放置一个断点,然后一步一步地转到使用SFSafariViewController的类。

在其中一个delegate方法中,您会发现对open url方法的调用。 在app delegate方法中实现相同的开放URL方法。

在您的情况下,问题是使用错误的openURL方法。正确的一个是

func application(application: UIApplication,openURL url: NSURL, options: [String: AnyObject]) -> Bool {
return FBSDKApplicationDelegate.sharedInstance().application(application, 
     openURL: url, 
     sourceApplication: options[UIApplicationOpenURLOptionsSourceApplicationKey] as! String,
     annotation: options [UIApplicationOpenURLOptionsAnnotationKey])
}

答案 1 :(得分:2)

Swift 3 ,删除此方法对我有用,

func application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any] = [:]) -> Bool {
    return application(app, open: url, sourceApplication: nil, annotation: [:])
}

答案 2 :(得分:0)

在AppDelegate.m文件中添加Open url方法,并在该方法中添加断点

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