探索iPhone平台下Facebook的开发

 

From  http://iphoneinaction.com/?p=10

* 有iPhone上的开发库吗 ?
* 开发流程 ?

官方提供 https://github.com/facebook/facebook-ios-sdk

下面简单总结下具体的开发流程

开发者最关心的应该是接口.
Facebook就是iPhone client 和后台通信的接口。其实所有的流程都包含在interface 的定义里面。

大致流程应该是:

Step 1: Login In
Step 2: Communication ( send request and handle response )
Step 3: Login Out

Step 1: Login In

首先,创建Facebook instance

Facebook* facebook = [[Facebook alloc] init];

在UI上,弹出



Button 实际会调用

- (void) authorize:(NSString*) application_id
permissions:(NSArray*) permissions
delegate:(id) delegate;

参数说明:

application_id
创建自己的app id http://www.facebook.com/developers/createapp.php

permissions

比如
_permissions = [[NSArrayarrayWithObjects:
@"read_stream", @"offline_access",nil] retain];

delegate
负责login ,log out etc

/*
*Your application should implement this delegate
*/
@protocol FBSessionDelegate

@optional

/**
* Called when the dialog successful log in the user
*/
- (void)fbDidLogin;

/**
* Called when the user dismiss the dialog without login
*/
- (void)fbDidNotLogin:(BOOL)cancelled;

/**
* Called when the user is logged out
*/
- (void)fbDidLogout;

@end

Step 2: 和facebook 后台通信

让我们先来看看通信的api

- (void) requestWithParams:(NSMutableDictionary *) params
andDelegate:(id ) delegate;

- (void) requestWithMethodName:(NSString *) methodName
andParams:(NSMutableDictionary *) params
andHttpMethod:(NSString *) httpMethod
andDelegate:(id ) delegate;

- (void) requestWithGraphPath:(NSString *) graphPath
andDelegate:(id ) delegate;

- (void) requestWithGraphPath:(NSString *)graphPath
andParams:(NSMutableDictionary *) params
andDelegate:(id ) delegate;

- (void) requestWithGraphPath:(NSString *)graphPath
andParams:(NSMutableDictionary *) params
andHttpMethod:(NSString *) httpMethod
andDelegate:(id ) delegate;

实际上这5个API 可以分为两个大类:

1, Old REST API

- (void) requestWithParams:(NSMutableDictionary *) params
andDelegate:(id ) delegate;

- (void) requestWithMethodName:(NSString *) methodName
andParams:(NSMutableDictionary *) params
andHttpMethod:(NSString *) httpMethod
andDelegate:(id ) delegate;

例子:

NSMutableDictionary* params = [NSMutableDictionary dictionaryWithObjectsAndKeys: @"4", @"uids", @"name", @"fields", nil]; [facebook requestWithMethodName: @"users.getInfo" andParams: params andHttpMethod: @"GET" andDelegate: self];

2, Graph API

- (void) requestWithGraphPath:(NSString *) graphPath
andDelegate:(id ) delegate;

- (void) requestWithGraphPath:(NSString *)graphPath
andParams:(NSMutableDictionary *) params
andDelegate:(id ) delegate;

- (void) requestWithGraphPath:(NSString *)graphPath
andParams:(NSMutableDictionary *) params
andHttpMethod:(NSString *) httpMethod
andDelegate:(id ) delegate;
比如

[facebook requestWithGraphPath:@"me" andDelegate:self]; // get information about the currently logged in user [facebook requestWithGraphPath:@"platform/posts" andDelegate:self]; // get the posts made by the "platform" page
[facebook requestWithGraphPath:@"me/friends" andDelegate:self]; // get the logged-in user's friends
还有一类很重要的API
Displaying Dialogs

- (void) dialog:(NSString *) action
andDelegate:(id) delegate;

- (void) dialog:(NSString *) action
andParams:(NSMutableDictionary *) params
andDelegate:(id ) delegate;

比如用户在 wall 上发消息

NSMutableDictionary* params = [NSMutableDictionary dictionaryWithObjectsAndKeys: apiKey, @"api_key", nil]; [facebook dialog:@"stream.publish" andParams:params andDelegate:self];

Step 3 : Log out

这一切似乎很容易啊!
具体可以参考例子 DemoApp


This was written by admin. Posted on Monday, November 22, 2010, at 5:02 pm. Filed under Uncategorized. Bookmark the permalink. Follow comments here with the RSS feed.     Post a comment or leave a trackback. Edit this entry.
Post a Comment
Logged in as ADMIN. Log out?
COMMENT


‹ The TTModel Ecosystem Diagram
posted @ 2012-03-20 18:00  痴人指路  阅读(257)  评论(0编辑  收藏  举报