After integrating with facebook when I tap on continue with FaceBook button on simulator, it popups this window on simulator..and when I tap on back to home it redirects me to login window and allows me to scroll in facebook insted of redirecting back to my app, and I am not getting any user data with it.
Here is steps that I have done
- Pod installation: pod 'FBSDKLoginKit'
- Added Bundle ID in quickstart guide in developer.facebook
- Enable Single Sign option is selected to NO
- Added the following code as it is in info.plist
Added the following code in AppDelegate
According to Facebook document we have to import FacebookCore in AppDelegate when we Import it we get warning No such module 'FacebookCore'
import FBSDKCoreKit
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
ApplicationDelegate.shared.application(
application,
didFinishLaunchingWithOptions: launchOptions
)
return true
}
func application(
_ app: UIApplication,
open url: URL,
options: [UIApplication.OpenURLOptionsKey : Any] = [:]
) -> Bool {
ApplicationDelegate.shared.application(
app,
open: url,
sourceApplication: options[UIApplication.OpenURLOptionsKey.sourceApplication] as? String,
annotation: options[UIApplication.OpenURLOptionsKey.annotation]
)
}
Added the following code in SceneDelegate
According to Facebook document we have to import FacebookCore in SceneDelegate when we Import it we get warning No such module 'FacebookCore'
import FBSDKCoreKit
func scene(_ scene: UIScene, openURLContexts URLContexts: Set<UIOpenURLContext>) {
guard let url = URLContexts.first?.url else {
return
}
ApplicationDelegate.shared.application(
UIApplication.shared,
open: url,
sourceApplication: nil,
annotation: [UIApplication.OpenURLOptionsKey.annotation]
)
}
Added the following code in ViewController
According to Facebook document we have to import FacebookLogin in ViewController when we Import it we get warning No such module 'FacebookLogin'
import FBSDKLoginKit
class ViewController: UIViewController, LoginButtonDelegate
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
if let token = AccessToken.current,
!token.isExpired {
let token = token.tokenString
let request = FBSDKLoginKit.GraphRequest(graphPath: "me", parameters: ["fields": "email,name"], tokenString: token, version: nil, httpMethod: .get)
request.start { connecton, result, error in
print("\(result)")
}
} else{
let loginButton = FBLoginButton()
loginButton.center = view.center
loginButton.delegate = self
loginButton.permissions = ["public_profile", "email"]
view.addSubview(loginButton)
}
}
func loginButton(_ loginButton: FBLoginButton, didCompleteWith result: LoginManagerLoginResult?, error: Error?) {
let token = result?.token?.tokenString
let request = FBSDKLoginKit.GraphRequest(graphPath: "me", parameters: ["fields": "email,name"], tokenString: token, version: nil, httpMethod: .get)
request.start { connecton, result, error in
print("\(result)")
}
}
CodePudding user response:
I think you've mistaken the Facebook login step.
step 1:- install the below pods
pod 'FacebookCore'
pod 'FacebookLogin'
pod 'FacebookShare'
step 2:- add necessary facebook credentials (facebook App Id) and CFBundleURLTypes
in info.plist
file
step 3:- import FacebookCore
and FacebookLogin
in your UIViewController
where you want the Facebook login feature.
call this method for login with facebook.
func setupFacebookLogin(){
let loginManager = LoginManager()
loginManager.logOut()
loginManager.logIn(permissions: ["public_profile", "email"], from: self, handler: { result, error in
if error != nil {
print("ERROR: Trying to get login results")
} else if result?.isCancelled != nil {
print("The token is \(result?.token?.tokenString ?? "")")
if result?.token?.tokenString != nil {
print("Logged in")
let graphRequest: GraphRequest = GraphRequest(graphPath: "me", parameters: ["fields": "id, first_name, middle_name, last_name, name, picture, email, gender, birthday"])
graphRequest.start { _, result, error in
if error != nil {
let data: [String: AnyObject] = result as! [String: AnyObject]
print("Facebook user data - ",data)
}
} else {
print("Cancelled")
}
}
})
}