Search code examples
iosswiftfacebook-graph-apisafarifacebook-login

Swift iOS Facebook SDK loop on login with safari view controller


I'm trying to add login with facebook in iOS app written in swift.

the problem here is that when I fill the login proccess in SSFSafariViewController and click Done it re-opens the login dialog in SSFSafariViewController and keeps doing that for ever.

here is my code:

// Loads user data if he is logged in, othewise asks him to login
static func login(ViewController controller: UIViewController) -> Bool {
    // If user has logged in before: dont ask him to login again
    // use previus login data
    if(AccessToken.current != nil){
        // Load user data
        create();
        return true;
    }
    // If user is'nt logged in: ask him to login
    else {
        var result: Bool = false;
        let loginManager = LoginManager()
        loginManager.logIn([ .publicProfile, .userFriends, .email], viewController: controller) { loginResult in
            switch loginResult {
            case .failed(let error):
                print(error)
                result = false;
            case .cancelled:
                result = false;
            case .success( _, _, _):
                result = true;
            }
        }

        // If user has logged in: laod his data
        if(result){
            create();
        }

        return result;
    }
}

Solution

  • I have solved my problem, it looks like is was a problem with the delegate where SSFSafariViewController presented by Facebook SDK wasn't calling back! and the SDK Never got the result of the SSFSafariViewController so it was trying to resolve the problem by re-presenting the login view SSFSafariViewController and that what cased the loop over and over again.

    so the solution was to add this function to AppDelegate:

    @available(iOS 9.0, *)
    func application(application: UIApplication,openURL url: NSURL, options: [String: AnyObject]) -> Bool {
        return FBSDKApplicationDelegate.sharedInstance().application(application, open: url as URL!, sourceApplication: UIApplicationOpenURLOptionsKey.sourceApplication.rawValue, annotation: UIApplicationOpenURLOptionsKey.annotation);
    }
    

    then every thing works out!