首页 文章

没有调用Facebook登录完成块

提问于
浏览
2

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

但是,如果我点击Done(在Web视图控制器的左上角),则称为完成块 but when I tap Cancel or OK, the web view controller dismissed but the completion block is never get called?
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
        }
    }

EDIT 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 回答

  • 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])
    }
    
  • 2

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

    func application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any] = [:]) -> Bool {
        return application(app, open: url, sourceApplication: nil, annotation: [:])
    }
    
  • 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];
    }
    

相关问题