Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift Facebook Login

I'm trying to convert the following code lines from Objective C to the new programming language Swift. Maybe someone can help me and outline the differences. Would be awesome!

if (FBSession.activeSession.state == FBSessionStateCreatedTokenLoaded) {
 [FBSession openActiveSessionWithReadPermissions:@[@"public_profile"]
                                 allowLoginUI:NO
                            completionHandler:^(FBSession *session, FBSessionState state, NSError *error) {
                              // Handler for session state changes
                              // This method will be called EACH time the session state changes,
                              // also for intermediate states and NOT just when the session open
                              [self sessionStateChanged:session state:state error:error];
                            }];}

Thanks, Tobias

like image 365
Tobias Avatar asked Jun 23 '14 17:06

Tobias


People also ask

How do I connect my iOS account to Facebook?

Steps. Tap the Settings app on the device. Scroll and tap the "Facebook" button, which can be found just a little bit below the button labeled Twitter. Tap and enter your Facebook account credentials (username and password) into the login screen's appropriate boxes.

What is Facebook Express login?

Express login logs people in with their Facebook account across devices and platform. If a person logs into your app on Android and then changes devices, express login logs them in with their Facebook account, instead of asking for them to select a login method.


2 Answers

Here is my answer: Few keywords like FBSessionStateCreatedTokenLoaded thrown error for me.. So This might helps

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {

  // Whenever a person opens the app, check for a cached session
    if FBSession.activeSession().state == FBSessionState.CreatedTokenLoaded
    {

         // If there's one, just open the session silently, without showing the user the login UI
        FBSession.openActiveSessionWithReadPermissions(["public_profile"], allowLoginUI: false, completionHandler: {
            (session, state, error) -> Void in
             self.sessionStateChanged(session, state: state, error: error)
        })
    }

 return true
}
 func sessionStateChanged(session : FBSession, state : FBSessionState, error : NSError?)
{
    // If the session was opened successfully
    if  state == FBSessionState.Open
    {
        println("Session Opened")
    }
    // If the session closed
    if state == FBSessionState.Closed
    {
        println("Closed")
    }
}

On Button click do Facebook login

 @IBAction func FacebookLoginPressed(Sender: AnyObject)
{
   if (FBSession.activeSession().state == FBSessionState.Open || FBSession.activeSession().state == FBSessionState.OpenTokenExtended)
    {
        // Close the session and remove the access token from the cache
        // The session state handler (in the app delegate) will be called automatically
        FBSession.activeSession().closeAndClearTokenInformation()
    }
    else
    {
        // Open a session showing the user the login UI
        // You must ALWAYS ask for public_profile permissions when opening a session
        FBSession.openActiveSessionWithReadPermissions(["public_profile"], allowLoginUI: true, completionHandler: {
            (session:FBSession!, state:FBSessionState, error:NSError!) in

            let appDelegate = UIApplication.sharedApplication().delegate as AppDelegate
             // Call the app delegate's sessionStateChanged:state:error method to handle session state changes
            appDelegate.sessionStateChanged(session, state: state, error: error)
        })
    }

}
like image 145
Chitra Khatri Avatar answered Oct 14 '22 11:10

Chitra Khatri


func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: NSDictionary?) -> Bool {
        if FBSession.activeSession.state.value == FBSessionStateCreatedTokenLoaded.value {
            FBSession.openActiveSessionWithReadPermissions(self.facebookReadPermissions, allowLoginUI: true, completionHandler: {(session, state, error) -> Void in
                    self.sessionStateChanged(session, state: state, error: error)
                })
        }

}    
func sessionStateChanged(session:FBSession, state:FBSessionState, error:NSError?) {

}
like image 22
NS Spartan029 Avatar answered Oct 14 '22 10:10

NS Spartan029