2014-06-23 24 views
6

Tôi đang cố chuyển đổi các dòng mã sau từ Objective C sang ngôn ngữ lập trình mới Swift. Có thể ai đó có thể giúp tôi và vạch ra sự khác biệt. Sẽ tuyệt vời!Swift Facebook Đăng nhập

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]; 
          }];} 

Cảm ơn, Tobias

+2

gì đang nhanh chóng bạn đã cố gắng và những gì lỗi bạn đang nhận được? – drewag

+0

Ngoài ra, điều này có thể giúp: http://stackoverflow.com/questions/24189946/call-facebook-delegates-in-swift/24196013#24196013 – drewag

Trả lời

13

Dưới đây là câu trả lời của tôi: Một vài từ khóa như FBSessionStateCreatedTokenLoaded lỗi ném cho tôi .. Vì vậy, sức mạnh này giúp

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 Butto n click làm Facebook đăng nhập

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

} 
+0

Tôi gặp lỗi khi triển khai mã này. Lỗi như "Sử dụng số nhận dạng đã giải quyết 'FBSession'" –

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

} 
+0

sử dụng số nhận dạng chưa được giải quyết 'FBSessionStateCreatedTokenLoaded' FBSession! không có thành viên có tên 'state' –

1

Đây là mã

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") 
    } 
} 
+0

cho dòng này: nếu state == FBSessionStateOpen, tôi nhận được lỗi biên dịch: 'Sử dụng mã định danh chưa được giải quyết FBSessionStateOpen', bất kỳ manh mối nào? – Devarshi

+0

Hãy thử với FBSessionState.Open – Rohit

+0

Cả hai đều không hoạt động trong Swift 1.2 – confile

0

này có thể được đơn giản hóa để:

let state = FBSession.activeSession().state 

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

Tôi gặp lỗi ''Không thể tìm thấy thành viên' Open''' – confile

Các vấn đề liên quan