facebook app what is NEW:【facebook SDK 3.1】what is NEW?
facebook app的基本設定,請參考:【facebook SDK 3.1】教程1-設定
說明:
這篇文章,主要在說明,登入-->驗證-->登出 等步驟說明。
若有需要基本設定等,請參考上面文章。
目前facebook login共有三種,現在說明的是目前2012釋放的版本教學。
在新本的SDK 3.1中,若用戶嘗試登入facebook,facebook SDK將會針對目前用戶的設定,選擇最佳的登入方式。
若用戶已經給與ios app權限,SDK將會立即取得token。
現在讓我們開始享受coding的流程吧...
step 1 : include the Facebook SDK
首先建立一個uiviewcontroller,命名為login,將此view設定為windows的rootviewcontroller。
分別在login.m和delegate.m下加入
#import <FacebookSDK/FacebookSDK.h>
step 2 : impelment the login flow
1.在login.m中建立一個uibutton名稱自定,若要跟下面所講的內容一樣的話,那就authButton吧
2.開啓你的delegate.h檔,建立一個全域變數
extern NSString *const FBSessionStateChangedNotification;
3.開啓你的delegate.m檔,並定義一個notification string
NSString *const FBSessionStateChangedNotification = @"com.example.Login:FBSessionStateChangedNotification";
你要注意的是『com.example.Login』這是你iOS app的bundle id…..
4.接下來,將下列兩個方法放到你的delegate.m中
/* * Callback for session changes. */ - (void)sessionStateChanged:(FBSession *)session state:(FBSessionState) state error:(NSError *)error { switch (state) { case FBSessionStateOpen: if (!error) { // We have a valid session NSLog(@"User session found"); } break; case FBSessionStateClosed: case FBSessionStateClosedLoginFailed: [FBSession.activeSession closeAndClearTokenInformation]; break; default: break; } [[NSNotificationCenter defaultCenter] postNotificationName:FBSessionStateChangedNotification object:session]; if (error) { UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Error" message:error.localizedDescription delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil]; [alertView show]; } } /* * Opens a Facebook session and optionally shows the login UX. */ - (BOOL)openSessionWithAllowLoginUI:(BOOL)allowLoginUI { return [FBSession openActiveSessionWithReadPermissions:nil allowLoginUI:allowLoginUI completionHandler:^(FBSession *session, FBSessionState state, NSError *error) { [self sessionStateChanged:session state:state error:error]; }]; }
5.在你的.h檔中加入
- (BOOL)openSessionWithAllowLoginUI:(BOOL)allowLoginUI;
6.若你需要額外的權限,修改openSessionWithAllowLoginUI:方法
ex:要求email 和 user_likes 的用戶權限
- (BOOL)openSessionWithAllowLoginUI:(BOOL)allowLoginUI { NSArray *permissions = [[NSArray alloc] initWithObjects: @"email", @"user_likes", nil]; return [FBSession openActiveSessionWithReadPermissions:permissions allowLoginUI:allowLoginUI completionHandler:^(FBSession *session, FBSessionState state, NSError *error) { [self sessionStateChanged:session state:state error:error]; }]; }
7.若你需要寫入的權限,如:public_actions,你必須使用
reauthorizeWithPermissions:defaultAudience:completionHandler:
permissions guide會有更多相關訊息
8.login flow最大的不同點在於 iOS 6+ 與 早期的iOS版本 兩者。
在iOS 6中,登入時,你的app將傳遞控制權給Facebook iOS app or 手機瀏覽器中的Facebook。
當使用者認證後,控制權將返回給你的app,同時夾帶session的資訊在編碼過的URL中。
為了記錄這些資訊,你需要給在返回的url中做一些處理,在ios6+中,login flow從ios中取得用戶的證書,用戶不需傳送資訊到facebook app。
9.在delegate.m中,分別找到對應的位置,把下面的code塞進去
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation { // attempt to extract a token from the url return [FBSession.activeSession handleOpenURL:url]; } - (void)applicationDidBecomeActive:(UIApplication *)application { [FBSession.activeSession handleDidBecomeActive]; } - (void)applicationWillTerminate:(UIApplication *)application { [FBSession.activeSession close]; }
10.在你的login.m中
#import "AppDelegate.h"
並將剛剛設定好的button的action,寫成
- (void)authButtonAction { AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate]; // If the user is authenticated, log out when the button is clicked. // If the user is not authenticated, log in when the button is clicked. if (FBSession.activeSession.isOpen) { [appDelegate closeSession]; } else { // The user has initiated a login, so call the openSession method // and show the login UX if necessary. [appDelegate openSessionWithAllowLoginUI:YES]; } }
此時你run project的話會看到兩種畫面,
『根據你的設定會有所不同:有無使用ios 6 內建的facebook帳號登入』
並在log中看到
『2012-12-21 18:56:16.832 facebooktest[2725:19a03] User session found』
到了這一步...你已經完成了登入+驗證的程序
step 3 : impelment the logout flow
1.在delegate.m中
- (void) closeSession { [FBSession.activeSession closeAndClearTokenInformation]; }2.在delegate.h中
- (void) closeSession;
3.改寫你的- (void)authButtonAction
- (void)authButtonAction { AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate]; // If the user is authenticated, log out when the button is clicked. // If the user is not authenticated, log in when the button is clicked. if (FBSession.activeSession.isOpen) { [appDelegate closeSession]; } else { // The user has initiated a login, so call the openSession method // and show the login UX if necessary. [appDelegate openSessionWithAllowLoginUI:YES]; } }
4.在login.m中的viewdidload中
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(sessionStateChanged:) name:FBSessionStateChangedNotification object:nil];
5.在login.m中的- (void)didReceiveMemoryWarning
[[NSNotificationCenter defaultCenter] removeObserver:self];
6.在login.n中添加此方法
- (void)sessionStateChanged:(NSNotification*)notification { if (FBSession.activeSession.isOpen) { [self.authButton setTitle:@"Logout" forState:UIControlStateNormal]; } else { [self.authButton setTitle:@"Login" forState:UIControlStateNormal]; } }
7.在login.m中的viewdidload中的最後面加上
AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate]; [appDelegate openSessionWithAllowLoginUI:NO];
==
到這邊就完成了自建的登入、驗證、註冊...等動作
當然facebook sdk中也有提供login ui的api
不過就不像這種彈性方式了~
最後若有error,請檢查facebook app的設定是否正確.
ex:bundle id沒有設定好,請參考前面的設定文章
As always , if you have any question , feel free to contact me.
有任何問題,請聯絡我
歡迎轉載,請註明出處,感謝。