7

I'm using;

  • swift sdk 0.2.0 and
  • swift 3.1

So, on my viewcontroller (initial.swift) I've

import UIKit
import FacebookCore
import FacebookLogin
...
override func viewDidLoad() {
if let accessToken = AccessToken.current {
   print("User is already logged in")
   ...
}
else {
   let loginButton = LoginButton(readPermissions: [ .publicProfile, .email ])
   loginButton.center = view.center

   //AFTER ACTION
   let loginManager = LoginManager()
   loginManager.logIn([ .publicProfile, .email ], viewController: self)
        { loginResult in
            switch loginResult {
            case .failed(let error):
                print(error)
                break
            case .cancelled:
                print("User cancelled login.")
            case .success(let grantedPermissions, let declinedPermissions, let accessToken):
                print("Logged in!")
            }
        }
}    

Login button shows up as expected but after permission page, white page stays open, when I click "done" manually app thinks that user cancelled login.
There are similar threads suggesting missing app delegate functions (34734885), but I guess it's a solution for FBSDKCoreKit and FBSDKLoginKit, not for new facebook swift sdk. Again, I guess. Any suggestion?

EDIT
I've also tried this one;

let myLoginButton = UIButton()
myLoginButton.backgroundColor = UIColor.darkGray
myLoginButton.frame = CGRect(0, 0, 180, 40);
myLoginButton.center = view.center;
myLoginButton.setTitle("My Login Button", for: UIControlState.normal)
// Add the button to the view
view.addSubview(myLoginButton)
// Handle clicks on the button
myLoginButton.addTarget(self, action: #selector(self.FBloginButtonClicked), for: UIControlEvents.touchUpInside)


@objc func FBloginButtonClicked() {  
    //AFTER ACTION
    let loginManager = LoginManager()
    loginManager.logIn([ .publicProfile, .email ], viewController: self)
    { loginResult in
        switch loginResult {
        case .failed(let error):
            print(error)
            break
        case .cancelled:
            print("User cancelled login.")
        case .success(let grantedPermissions, let declinedPermissions, let accessToken):
            print("Logged in!")
        }
    }
}
Community
  • 1
  • 1
code_ada
  • 874
  • 12
  • 25

2 Answers2

0

As I understand you did everything accordingly to facebook guide?

Including this part:

// Handle clicks on the button
  myLoginButton.addTarget(self, action: @selector(self.loginButtonClicked) forControlEvents: .TouchUpInside) 

And

// Once the button is clicked, show the login dialog
@objc func loginButtonClicked() {
  let loginManager = LoginManager()
  ...
  }
Kamajabu
  • 596
  • 1
  • 5
  • 19
0

As per understanding your question, You seems to write one method into appdelegate. This will return to your application after successful login.

import FacebookCore // (FBSDKCore's alternative for swift)

func application(_ application: UIApplication, open url: URL, sourceApplication: String?, annotation: Any) -> Bool {
    let isFBOpenUrl = SDKApplicationDelegate.shared.application(application, open: url, sourceApplication: sourceApplication, annotation: annotation)

    if isFBOpenUrl { return true }
    return false
}

SWIFT 4:

func application(_ application: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey:Any] = [:]) -> Bool {
        return SDKApplicationDelegate.shared.application(application, open: url, options: options)
    }

Hope this thing will help you.

Kegham K.
  • 1,589
  • 22
  • 40
Jitendra Modi
  • 2,344
  • 12
  • 34