Facebook 是目前最紅的社交網站,流量已超過 Google 了,它不只有 Flash 遊戲,連 app 的應用分享也愈來愈普及。曾經有傳言 Facebook 會像 Twitter 一樣內建在 iOS 5 SDK 裡,直到 iOS5 GM 出來,卻還是未見 Facebook 內建在其中。但沒有關係,因為 Facebook 官方有提供 Facebook for iOS sdk 可運用,用法雖然沒有 Twitter api 來的簡易,但功能卻是更全面。這裡我們示範簡單的 Facebook sdk 用法,包含將文字貼到塗鴉牆上,及貼上一張圖片到照片集的範例。
事前準備工作
首先請到 http://www.facebook.com/developers/createapp.php 申請一個 Facebook app,選擇 “建立應用程式”,再依指示輸入名稱等
最後會產生一組 AppId,把這組 id 記錄下來備用。

因為 Facebook sdk 使用時,在登入時會跳至 Safari 或是 Facebook app 輸入帳號密碼,輸入完畢後必須跳回原本的 app。所以必須在你的 app 設定 URL Schemes 讓登入後可以正確的跳回來,在 URL Schemes 填入 fbxxxxxxxxxx (xxxxxxxxxx 為取得的 facebook appid)。

接下來就直接在 XCode 中撰寫相關的 Code 了。因為必須取得 Facebook 的事件,所以在 AppDelegate.h 中宣告 FBSessionDelegate, FBRequestDelegate, FBDialogDelegate,並 import 以下兩個宣告檔及 Facebook 物件
#import "Facebook.h"
#import "FBConnect.h"
Facebook *facebook;
並在 didFinishLaunchingWithOptions 函式中加入初始化 Facebook 及其對應函式
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
facebook = [[Facebook alloc] initWithAppId:@"250701411638931" andDelegate:self];
[self.window addSubview:viewController.view];
[self.window makeKeyAndVisible];
}
- (void)dealloc
{
[facebook release];
[viewController release];
[window release];
[super dealloc];
}
- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url
{
return [facebook handleOpenURL:url];
}
開始撰寫使用 Facebook,將文字貼到塗鴉牆上
這裡我為 Facebook 使用寫了幾個專屬的函式,一個是 Login, Logout 及一個 post 的函式,使用時只要依自己需求修改 sendToWall 函式內的資訊。
- (void)login
{
NSArray *permissions = [NSArray arrayWithObjects:@"read_stream", @"publish_stream", @"offline_access", nil];
[facebook authorize:permissions];
}
- (void)logout
{
[facebook logout:self];
}
- (void)sendToWall
{
SBJSON *jsonWriter = [[SBJSON new] autorelease];
NSDictionary* attachment = [NSDictionary dictionaryWithObjectsAndKeys:
@"name", @"name",
@"caption", @"caption",
@"description", @"description",
@"http://xxx.xxx.xxx.xxx", @"href", nil];
NSMutableDictionary* params1 = [NSMutableDictionary dictionaryWithObjectsAndKeys:
@"xxxxxxxxxxxx", @"api_key",
[jsonWriter stringWithObject:attachment], @"attachment",
nil];
[facebook dialog:@"stream.publish" andParams:params1 andDelegate:self disableTextArea:TRUE];
}
接下來還必須處理 Facebook 的事件,這些事件如果沒有特殊的用法,其實可以照範例來寫即可。
#pragma mark - FBRequestDelegate
- (void)requestLoading:(FBRequest *)request
{
}
- (void)request:(FBRequest *)request didReceiveResponse:(NSURLResponse *)response
{
NSLog(@"received response");
}
- (void)request:(FBRequest *)request didFailWithError:(NSError *)error
{
}
- (void)request:(FBRequest *)request didLoad:(id)result
{
NSLog(@"result = %@", result);
if ([result isKindOfClass:[NSArray class]])
{
// Facebook 視窗
FacebookViewController *detailViewController = [[FacebookViewController alloc] initWithNibName:@"FacebookViewController" bundle:nil];
detailViewController.image = [viewController getImage:FALSE];
[viewController presentModalViewController:detailViewController animated:YES];
[detailViewController release];
}
else if ([result objectForKey:@"id"])
NSLog(@"Photo upload Success");
}
- (void)request:(FBRequest *)request didLoadRawResponse:(NSData *)data
{
}
寫好以上的 code,真正使用時只要呼叫 login 及 sendToWall 函式即可,一般簡單的貼文用途應該也夠用了。
貼上圖片和文字到塗鴉牆上
接下來的函式,是示範怎麼把圖片貼到塗鴉牆上,方法其實和上述的例子相同。
- (void)postFacebookImage:(NSNotification*)notification
{
UIImage *image = [UIImage imageNamed:@"test.png"];
NSMutableDictionary *params = [NSMutableDictionary dictionaryWithObjectsAndKeys:
image, @"picture",
@"message", @"message",
nil];
[facebook requestWithGraphPath:@"me/photos"
andParams:params
andHttpMethod:@"POST"
andDelegate:self];
}
0 意見:
張貼留言