为什么FBLogin不能在模拟器中工作? - 它留在Safari?

时间:2016-05-11 08:59:57

标签: ios swift facebook safari

我正在使用xcode 7.3。最新版本的fb sdk.I根据Fb文档进行了所有方法声明。但是现在当我按下facebook按钮登录时。它重定向到safari页面并询问你是否想要允许。我给了Yes.After后它仍然在safari页面。这里是一个“完成”按钮在safari页面的左上角知道。如果我按下完成按钮只有它进入我的主屏幕。我不知道为什么?

我尝试重置我的模拟器,并在info.plist中添加所有需要的东西。但它不起作用。我有fb,谷歌加上两个登录按钮。但是这个fb无效。

任何解决方案都会很棒

enter image description here

enter image description here

enter image description here

在我按下完成按钮后,只允许我进入应用程序。(主屏幕)。

我在appdelegate中的代码:

 func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
  return FBSDKApplicationDelegate.sharedInstance().application(application, didFinishLaunchingWithOptions: launchOptions)
    }


    func application(app: UIApplication, openURL url: NSURL, sourceApplication: String?, annotation: AnyObject) -> Bool {

       // let GoogleHandler = GIDSignIn.sharedInstance().handleURL(url, sourceApplication: sourceApplication, annotation: annotation)

        let FacebookHandler = FBSDKApplicationDelegate.sharedInstance().application(app, openURL: url, sourceApplication: sourceApplication, annotation: annotation)

        return  FacebookHandler
    }

我在viewcontroller中的代码:

import UIKit
import FBSDKCoreKit
import FBSDKLoginKit

class LoginViewController: UIViewController, UIAlertViewDelegate,  FBSDKLoginButtonDelegate
{
 override func viewDidLoad()
    {
        super.viewDidLoad()

        self.navigationController?.navigationBar.barTintColor = UIColor.redColor()

        self.navigationController?.navigationBar.tintColor = UIColor.whiteColor()

        if (FBSDKAccessToken.currentAccessToken() != nil)
        {
            // User is already logged in, do work such as go to next view controller.
            print("User Already Logged In")
        }
        else
        {
            print("User Not Logged In")
        }

        fbLoginBtn.readPermissions = ["public_profile", "email", "user_friends"]
        fbLoginBtn.center = self.view.center
        fbLoginBtn.delegate = self
        self.view.addSubview(fbLoginBtn)
}

    ////// ------- Fb login delegate methods ------ //////

    func loginButton(loginButton: FBSDKLoginButton!, didCompleteWithResult result: FBSDKLoginManagerLoginResult!, error: NSError!)
    {
        if error == nil
        {
            print("Login complete.")
            dispatch_async(dispatch_get_main_queue())
            {
                let appdelegate:AppDelegate = UIApplication.sharedApplication().delegate as! AppDelegate

                _ = appdelegate.window!.rootViewController


                let mainStoryBoard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)

                let mainViewController = mainStoryBoard.instantiateViewControllerWithIdentifier("HomeViewController") as! HomeViewController

                let leftViewController = mainStoryBoard.instantiateViewControllerWithIdentifier("MenuViewController") as! MenuViewController


                let leftSideNav = UINavigationController(rootViewController: leftViewController)

                let centerNav = UINavigationController(rootViewController: mainViewController)


                let centerContainer:MMDrawerController = MMDrawerController(centerViewController: centerNav, leftDrawerViewController: leftSideNav)



                centerContainer.openDrawerGestureModeMask = MMOpenDrawerGestureMode.PanningCenterView;

                centerContainer.closeDrawerGestureModeMask = MMCloseDrawerGestureMode.PanningCenterView;

                               appdelegate.centerContainer = centerContainer

                appdelegate.window!.rootViewController = appdelegate.centerContainer

                appdelegate.window!.makeKeyAndVisible()
            }

        }
        else
        {
            print(error.localizedDescription)
        }
    }
    func loginButtonDidLogOut(loginButton: FBSDKLoginButton!)
    {
        print("User logged out...")
    }

3 个答案:

答案 0 :(得分:1)

此代码适用于XCode 7.3。当您从Facebook登录时,它会返回到您的视图控制器上。

override func viewDidLoad() {
    super.viewDidLoad()

    // Login steps
    if (FBSDKAccessToken.currentAccessToken() == nil)
    {
        // User is not already logged
        print("Not logged")
    }
    else
    {
        // User is already logged
        print("Logged")
    }
}

// Facebook Delegate Methods
func loginButton(connection: FBSDKLoginButton!, didCompleteWithResult result: FBSDKLoginManagerLoginResult!, error: NSError!) {

    if (error != nil)
    {
        print("Error")
    }
    else {
        // If you ask for multiple permissions at once, you
        // should check if specific permissions missing
        if result.grantedPermissions.contains("email")
        {
            // Do work
        }
        print("User logged in")
    }
}

func loginButtonDidLogOut(loginButton: FBSDKLoginButton!) {
    print("User Logged Out")
}

答案 1 :(得分:1)

尝试覆盖此AppDelegate方法:

-(BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation {
    // Call FBAppCall's handleOpenURL:sourceApplication to handle Facebook app responses
    BOOL wasHandled = [FBAppCall handleOpenURL:url sourceApplication:sourceApplication];

    // You can add your app-specific url handling code here if needed
    return wasHandled;
}

对于Objective-C答案,我很抱歉,我刚从项目中复制了它。但你应该能够明白这一点。

答案 2 :(得分:1)

首先,您必须在Appdelegate中添加一个方法,如下所示

  - (void)applicationDidBecomeActive:(UIApplication *)application {
  [FBSDKAppEvents activateApp];
}

第二,这是因为ios版本会对ios 9及以上版本产生一些问题,所以它在设备上也能正常工作。