第一步必須在AppDelegate的地方接住http近來之Handler,並將此url遞給Facebook物件:
//=====In appDelegate.m=======
- (BOOL)application:(UIApplication *)application
openURL:(NSURL *)url
sourceApplication:(NSString *)sourceApplication
annotation:(id)annotation {
// attempt to extract a token from the url
//利用rootViewController來取得擁有Facebook物件之viewController
UINavigationController* testing = (UINavigationController*)self.window.rootViewController;
IndexViewController *controller = (IndexViewController*)[testing.viewControllers objectAtIndex:1];
//將url遞給facebook物件,之後facebook之Delegate才會驅動fbDidLogin與fbDidLogout
return [controller.facebook handleOpenURL:url];
}
接著在擁有Facebook物件之ViewController初始化facebook,並使用NSUserDefault來記錄登入的狀態:
- (void)viewDidLoad
{
[super viewDidLoad];
//Facebook物件初始化,並登入AppID
facebook = [[Facebook alloc] initWithAppId:AppID andDelegate:self];
//使用NSUserDefault來記錄登入狀態
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
//檢查是否有用過
if ([defaults objectForKey:@"FBAccessToken"]
&&[defaults objectForKey:@"FBExpirationDate"]) {
//如果沒有重新開一個
facebook.accessToken = [defaults objectForKey:@"FBAccessToken"];
facebook.expirationDate = [defaults objectForKey:@"FBExpirationDate"];
}
}
登入程序:
-(void)login
{
//檢查Facebokk是否連線還在
if (![facebook isSessionValid]) {
//如果已經不再連線狀態,重新連線
//定義需要的權限
NSArray *permissions = [NSArray arrayWithObjects:
@"user_hometown", @"read_stream", @"user_birthday",
@"user_about_me", @"publish_stream", @"offline_access", nil];
//登入
[facebook authorize:permissions];
}else{
//如果已經在登入狀態的話,擷取使用者資訊
[facebook requestWithGraphPath:@"me" andDelegate:self];
}
}
貼文程序:
-(void)postToWall
{
NSMutableDictionary* params = [NSMutableDictionary dictionaryWithObjectsAndKeys:
AppID, @"app_id",
@"http://www.google.com.tw/", @"link",
@"http://www.google.com.tw/pic.jpg", @"picture",
@"Facebook test", @"name",
@"caption", @"caption",
@"", @"description",
@"", @"message",
nil];
[facebook dialog:@"stream.publish" andParams:params andDelegate:self];
}
剩下一些FB之Delegate:
#pragma mark - FBRequestDelegate
- (void)requestLoading:(FBRequest *)request
{
}
- (void)request:(FBRequest *)request didReceiveResponse:(NSURLResponse *)response
{
NSLog(@"received response:%@",response);
}
- (void)request:(FBRequest *)request didFailWithError:(NSError *)error
{
}
- (void)request:(FBRequest *)request didLoad:(id)result
{
//利用Facebook物件request之資訊會傳遞到result,可以從這邊去收資料
if ([result isKindOfClass:[NSArray class]] && [result count]>0) {
result = [result objectAtIndex:0];
}
if ([result objectForKey:@"name"]){
userName = [result objectForKey:@"name"];
}
}
- (void)request:(FBRequest *)request didLoadRawResponse:(NSData *)data
{
}
//如果登入成功會呼叫此function
- (void)fbDidLogin {
//擷取個人資訊,回饋資訊回從- (void)request:(FBRequest *)request didLoad:(id)result接收
[facebook requestWithGraphPath:@"me" andDelegate:self];
//將成功登入之資訊傳到NSUserDefault
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:[facebook accessToken] forKey:@"FBAccessToken"];
[defaults setObject:[facebook expirationDate] forKey:@"FBExpirationDate"];
[defaults synchronize];
}
-(void)fbDidLogout
{
//登出時,清除帳戶資訊
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
if ([defaults objectForKey:@"FBAccessToken"]) {
[defaults removeObjectForKey:@"FBAccessToken"];
[defaults removeObjectForKey:@"FBExpirationDate"];
[defaults synchronize];
}
}