Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/119.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 快速Facebook登录_Ios_Xcode_Facebook_Login_Swift - Fatal编程技术网

Ios 快速Facebook登录

Ios 快速Facebook登录,ios,xcode,facebook,login,swift,Ios,Xcode,Facebook,Login,Swift,我正在尝试将以下代码行从Objective C转换为新的编程语言Swift。也许有人能帮我概括一下区别。那太棒了 if (FBSession.activeSession.state == FBSessionStateCreatedTokenLoaded) { [FBSession openActiveSessionWithReadPermissions:@[@"public_profile"] allowLoginUI:NO

我正在尝试将以下代码行从Objective C转换为新的编程语言Swift。也许有人能帮我概括一下区别。那太棒了

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];
                            }];}
谢谢,托拜厄斯这是密码

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?) {

}
if FBSession.activeSession().state == FBSessionStateCreatedTokenLoaded
    {
        FBSession.openActiveSessionWithPublishPermissions("publish_actions", defaultAudience: FBSessionDefaultAudienceFriends, allowLoginUI: true, completionHandler: ^(session : FBSession, state : FBSessionState, error : NSError))
            {
                // 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)
        }
    }
           return true
}

func sessionStateChanged(session : FBSession, state : FBSessionState, error : NSError)
{
    // If the session was opened successfully
    if  state == FBSessionStateOpen
    {
        println("Session Opened")
    }
}

以下是我的答案:
FBSessionStateCreatedTokenLoaded
这样的关键词很少会为我抛出错误。。所以这可能会有帮助

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")
    }
}
在按钮上单击DoFacebook登录

 @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)
        })
    }

}

这可以简化为:

let state = FBSession.activeSession().state

if state == .Open && state == .OpenTokenExtended {
  FBSession.activeSession().closeAndClearTokenInformation()
} else {
  FBSession.openActiveSessionWithReadPermissions(["public_profile"], allowLoginUI: true) {
    _ in
    FBSession.activeSession().closeAndClearTokenInformation()
  }
}

您尝试了哪些swift代码以及出现了哪些错误?此外,这可能会有所帮助:对于这一行:如果state==FBSessionStateOpen,我将出现编译错误:“使用未解析标识符FBSessionStateOpen”,有什么线索吗?尝试FBSessionState.Openuse使用未解析标识符“FBSessionStateCreatedTokenLoaded”FBSession!没有名为“state”的成员。我在实现此代码时出错。错误,如“使用已解析的标识符'FBSession'”