こんにちはこんばんは!暑いですね!~“Q。(‘‐’。)” パタパタ。凄く暑いですね。暑いのでiPhoneのネイティブアプリでFacebookにログインするアプリが作りたくなりました。無茶な流れですが暑いので許してください。
■インポート
ソースを取得
以下のコマンドでgithubから取得できます。
git clone git://github.com/facebook/facebook-ios-sdk.git
SDKをgithubで配布ですよ!日本企業も見習わなくちゃいけませんね!持ってきたディレクトリのsrcディレクトリに必要なファイルは揃っていますので、そのままプロジェクトに突っ込みましょう。(.xcodeprojなどは不要です)
■ログインの実装
AppDelegate.h
以下のようにFBConnect.hをインポートし、FBSessionDelegateを実装します。
#import <UIKit/UIKit.h>
#import "FBConnect.h"
@class MainViewController;
@interface facebookAppDelegate : NSObject <UIApplicationDelegate, FBSessionDelegate> {
Facebook *facebook;
}
@property (nonatomic, retain) IBOutlet UIWindow *window;
@property (nonatomic, retain) Facebook *facebook;
@property (nonatomic, retain) MainViewController *mainViewController;
@end
AppDelegate.m
#import "facebookAppDelegate.h"
/**
* @see https://developers.facebook.com/apps/
* アプリを登録すると発行されます(公開までsandboxモードにしておきましょう)
*/
#define APP_ID @"123456789"
@implementation facebookAppDelegate
@synthesize window=_window;
@synthesize facebook;
@synthesize mainViewController;
- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url {
return [facebook handleOpenURL:url];
}
// ブラウザのログイン画面から戻ってきた時に実行される
// ログイン情報をNSUserDefaultsに保存する
- (void)fbDidLogin {
NSUserDefaults *ud = [NSUserDefaults standardUserDefaults];
[ud setObject:[facebook accessToken] forKey:@"FBAccessTokenKey"];
[ud setObject:[facebook expirationDate] forKey:@"FBExpirationDateKey"];
[ud synchronize];
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// APP IDをセットしてインスタンス化
facebook = [[Facebook alloc] initWithAppId:APP_ID];
// ログイン情報があればセット
NSUserDefaults *ud = [NSUserDefaults standardUserDefaults];
if ([ud objectForKey:@"FBAccessTokenKey"] && [ud objectForKey:@"FBExpirationDateKey"]) {
[facebook setAccessToken:[ud objectForKey:@"FBAccessTokenKey"]];
[facebook setExpirationDate:[ud objectForKey:@"FBExpirationDateKey"]];
}
// パーミッションをセットして有効なセッションがなければブラウザでログインする
NSArray* permissions = [[NSArray arrayWithObjects:@"email", @"read_stream", nil] retain];
if (![facebook isSessionValid]) {
[facebook authorize:permissions delegate:self];
}
[self.window makeKeyAndVisible];
return YES;
}
info.plist
Facebook SDKではinfo.plistに記述したURLによってアプリケーションを起動できる仕組みを使っています。以下のように変更してください。
「(Array) URL types > (Dictionary) Item 0 > (Array) URL Schemes > (Dictionary) Item 0 > (String) fb[APP ID]」となるように入力します。
起動
起動するとちゃんとログインができるかと思います。
■API
まぁ、ログインだけというのもなんなのでGraph APIを叩いてみましょう。
AppDelegate.h
FBRequestDelegateを実装します。
#import <UIKit/UIKit.h>
#import "FBConnect.h"
@class MainViewController;
@interface facebookAppDelegate : NSObject <UIApplicationDelegate, FBSessionDelegate, FBRequestDelegate> {
Facebook *facebook;
}
@property (nonatomic, retain) IBOutlet UIWindow *window;
@property (nonatomic, retain) Facebook *facebook;
@property (nonatomic, retain) MainViewController *mainViewController;
@end
AppDelegate.m
makeKeyAndVisibleの前あたりでrequestWithGraphPathを呼びます。
[facebook requestWithGraphPath:@"me" andDelegate:self];
[self.window makeKeyAndVisible];
結果を受け取る
以下の4メソッドをAppDelegate.mに実装することで、NSURLConnection的に結果を受け取れます。
/**
* Called just before the request is sent to the server.
*/
- (void)requestLoading:(FBRequest *)request
{
}
/**
* Called when the server responds and begins to send back data.
*/
- (void)request:(FBRequest *)request didReceiveResponse:(NSURLResponse *)response
{
}
/**
* Called when an error prevents the request from completing successfully.
*/
- (void)request:(FBRequest *)request didFailWithError:(NSError *)error
{
}
/**
* Called when a request returns and its response has been parsed into
* an object.
*
* The resulting object may be a dictionary, an array, a string, or a number,
* depending on thee format of the API response.
*/
- (void)request:(FBRequest *)request didLoad:(id)result
{
NSLog(@"%@", result);
}
/**
* Called when a request returns a response.
*
* The result object is the raw response from the server of type NSData
*/
- (void)request:(FBRequest *)request didLoadRawResponse:(NSData *)data
{
}
以上となります。公式ドキュメントを翻訳しただけのようになってしまいましたが暑いので許してください?
■参考
Mobile Apps