Facebook SDK FBSession代码无法使用Swift

时间:2014-07-28 12:23:22

标签: ios facebook xcode6

尝试在Swift中实现这个Objective C代码:

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

    // If there's one, just open the session silently, without showing the user the login UI
      [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];
                            }];

但在此行中获取FBSession.activeSession.state.value错误:

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

请帮忙。

4 个答案:

答案 0 :(得分:2)

使用此

更改您的第一个if语句
    if(FBSession.activeSession().state == FBSessionState.CreatedTokenLoaded)
    {

    }

答案 1 :(得分:0)

试试这个

 if(fbsessionState.value != FBSessionStateOpen.value && fbsessionState.value != FBSessionStateOpenTokenExtended.value)
    {

        let permission = ["email","public_profile", "user_friends" ];

        FBSession.openActiveSessionWithPublishPermissions(permission, defaultAudience: FBSessionDefaultAudienceFriends, allowLoginUI: true, completionHandler: self.handlerFB);

    }

func handlerFB(session:FBSession!, state:FBSessionState, error:NSError!)
{
    if let gotError = error
    {

    }
    else
    {

        self.fbSession = session;

        FBRequest.requestForMe()?.startWithCompletionHandler(self.requestfbComplete);
    }
}

答案 2 :(得分:-1)

IN SWIFT

if FBSession.activeSession().state == FBSessionState.CreatedTokenLoaded {
    FBSession.openActiveSessionWithReadPermissions(["public_profile","email"], 
        allowLoginUI: true, 
        completionHandler:{(session, state, error) in
            self.sessionStateChanged(session, state: state, error: error)
        })
}

答案 3 :(得分:-3)

FBSession不是swift中AnyObject的成员。在快速

中不允许使用FBSession