共享
https://developers.facebook.com/docs/ios/share
地址
一个共享可以有两种类型:
1、和一个link一样,或者状态更新。(左图)
2、Open Graph, 比如一个故事,关于做美食,跑步或者读一本书。(右图)
见下图,
项目可以以两种方式分享:
1、使用Share Dialog,一个内置的dialog和Facebook外观一样,很容易使用,不需要用户登录你的app。
2、使用API调用,允许你进一步控制你的分享内容但是需要用户登录你的app,并且允许分享。
下面文章会介绍以上两种方法,如何使你的分享链接回到你的app,所有可用的SDK分享方法,例子再github有
github.com/fbsamples/ios-howtos。
先决条件,
1、环境配置。
2、一个Facebook app适当的配置好了并且连接到你的app,并且登录了。
3、Facebook SDK已经添加到project。
4、你的Facebook app ID和应用名称已经添加到app的.plist文件了。
分享一个链接,
内容包括:
1、link:我们想分享的url。
2、name:一个题目。
3、caption:一个副标题。
4、picture:关于这个post的url或者略缩图。
5、description:一小条信息用来秒速你的分享。
注意:url只支持"http"或者"https"
使用Share Dialog
首先满足如上提的先决条件。例子在FBShareSample里面,就是如上提到的github里下载。
Share Dialog的使用流程如下:
1、配置你要分享的内容。
2、使app跳转到本地已安装的Facebook,然后分享。
3、返回你的应用当用户分享结束后。
效果如下:
Share Dialog需要用户的设备已经安装了Facebook的官方app。当用户没有安装官方Facebook的app,会指向Feed Dialog(一个web dialog,不需要本地Facebook应用)。在这个教程我们也会讲到how to use the Feed Dialog as fallback,我们强烈建议你这么做。
我们首先要做的是确认用户已经安装官方app。使用FBDialogs canPresentShareDialogWithParams:方法。
// Check if the Facebook app is installed and we can present the share dialog
FBShareDialogParams *params = [[FBShareDialogParams alloc] init];
params.link = [NSURL URLWithString:@"https://developers.facebook.com/docs/ios/share/"];
params.name = @"Sharing Tutorial";
params.caption = @"Build great social apps and get more installs.";
params.picture = [NSURL URLWithString:@"http://i.imgur.com/g3Qc1HN.png"];
params.description = @"Allow your users to share stories on Facebook from your app using the iOS SDK.";
// If the Facebook app is installed and we can present the share dialog
if ([FBDialogs canPresentShareDialogWithParams:params]) {
// Present the share dialog
} else {
// Present the feed dialog
}
如果本地的app被调用了,为了展示share dialog,我们调用一个FBDialogspresentShareDialogWithLink:方法。这个方法会接收一些参数,包括包括我们想要分享的链接,和一个我们分享结束后调用的handler。代码如下:
// Present share dialog
[FBDialogs presentShareDialogWithLink:params.link
name:params.name
caption:params.caption
description:params.description
picture:params.picture
clientState:nil
handler:^(FBAppCall *call, NSDictionary *results, NSError *error) {
if(error) {
// An error occurred, we need to handle the error
// See: https://developers.facebook.com/docs/ios/errors
NSLog([NSString stringWithFormat:@"Error publishing story: %@", error.description]);
} else {
// Success
NSLog(@"result %@", results);
}
}];
最后一步告诉handler你的app已经打开了dialog。这个handler会接收三个参数:一个FBAppCall,一个包含结果的字典和一个NSError。FBAppCall包含了运行状态,即是否需要转换到官方的app或者接收一个app link。
|
| Always available. |
|
| Only available if the user logged into your app using Facebook and |
|
| Only available if the user is logged into your app using Facebook, has granted a publish permission (e.g. |
接下来要做的事情是为了得到一个share dialog,你要处理从那里得到的响应。当share dialog关闭后,使用一个ios url返回你的app。你的app需要处理那个来的url,使用FBAppCall方法,它来自你app的app delegate,就是application:openURL:sourceApplication:annotation:你需要添加如下代码到你的app delegate。
- (BOOL)application:(UIApplication *)application
openURL:(NSURL *)url
sourceApplication:(NSString *)sourceApplication
annotation:(id)annotation {
BOOL urlWasHandled = [FBAppCall handleOpenURL:url
sourceApplication:sourceApplication
fallbackHandler:^(FBAppCall *call) {
NSLog(@"Unhandled deep link: %@", url);
// Here goes the code to handle the links
// Use the links to show a relevant view of your app to the user
}];
return urlWasHandled;
}
测试方法:为了测试share dialog,你需要安装facebook官方app和你的app,因为模拟器暂不支持安装facebook官方app,你需要在真机测试,并且ios版本在6.0以上。
最后,为了使所有用户可以调用,你还要添加feed dialog。
Feed dialog as fallback
以上方法已经说明要安装官方facebook app,对于那些没有安装的用户,使用此方法。
除非用户已经登录facebook,不然feed dialog会询问用户问题用来验证用户身份。这样会比share dialog慢,因为会问用户凭证。所以我们不建议使用它作为主要方法。但是在share dialog无法工作时非常有用。
如下图所示:
代码如下会说明如何使用feed dialog。我们会调用FBWebDialogspresentFeedDialogModallyWithSession:parameters:handler:方法去展现feed dialog。这个方法有三个参数:
session:这个参数可以为nil,是否让用户再web view登录。
NSDictionary:保持之前使用的参数。
handler:当会到你的app时调用。
当一个分享结束后,story‘s ID作为一个返回结果,指向FBWebDialogHandler,具体来说,story ID 再resultURL的post_id参数中。以下例子用来说明如何解析返回值。
// Put together the dialog parameters
NSMutableDictionary *params = [NSMutableDictionary dictionaryWithObjectsAndKeys:
@"Sharing Tutorial", @"name",
@"Build great social apps and get more installs.", @"caption",
@"Allow your users to share stories on Facebook from your app using the iOS SDK.", @"description",
@"https://developers.facebook.com/docs/ios/share/", @"link",
@"http://i.imgur.com/g3Qc1HN.png", @"picture",
nil];
// Show the feed dialog
[FBWebDialogs presentFeedDialogModallyWithSession:nil
parameters:params
handler:^(FBWebDialogResult result, NSURL *resultURL, NSError *error) {
if (error) {
// An error occurred, we need to handle the error
// See: https://developers.facebook.com/docs/ios/errors
NSLog([NSString stringWithFormat:@"Error publishing story: %@", error.description]);
} else {
if (result == FBWebDialogResultDialogNotCompleted) {
// User cancelled.
NSLog(@"User cancelled.");
} else {
// Handle the publish feed callback
NSDictionary *urlParams = [self parseURLParams:[resultURL query]];
if (![urlParams valueForKey:@"post_id"]) {
// User cancelled.
NSLog(@"User cancelled.");
} else {
// User clicked the Share button
NSString *result = [NSString stringWithFormat: @"Posted story, id: %@", [urlParams valueForKey:@"post_id"]];
NSLog(@"result %@", result);
}
}
}
}];
解析dialog url,为了核对post id
// A function for parsing URL parameters returned by the Feed Dialog.
- (NSDictionary*)parseURLParams:(NSString *)query {
NSArray *pairs = [query componentsSeparatedByString:@"&"];
NSMutableDictionary *params = [[NSMutableDictionary alloc] init];
for (NSString *pair in pairs) {
NSArray *kv = [pair componentsSeparatedByString:@"="];
NSString *val =
[kv[1] stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
params[kv[0]] = val;
}
return params;
}
分享已经完成,接下来的步骤是返回到你的app。
使用API calls,
条件:符合以上要求,登录facebook。
当用户登录facebook后,发布一条状态的步骤如下:
1、核对必要的请求是否符合,在publish_actions里面。
2、如果没有请求,就请求一下。
3、如果批准了请求,call to post the link。
// NOTE: pre-filling fields associated with Facebook posts,
// unless the user manually generated the content earlier in the workflow of your app,
// can be against the Platform policies: https://developers.facebook.com/policy
[FBRequestConnection startForPostStatusUpdate:@"User-generated status update."
completionHandler:^(FBRequestConnection *connection, id result, NSError *error) {
if (!error) {
// Status update posted successfully to Facebook
NSLog([NSString stringWithFormat:@"result: %@", result]);
} else {
// An error occurred, we need to handle the error
// See: https://developers.facebook.com/docs/ios/errors
NSLog([NSString stringWithFormat:@"%@", error.description]);
}
}];
分享一个open graph story,
open graph用app讲一个故事,facebook以一个故事形式来分享,被分享的用户可以直接去app store下载你的app。
故事有如下核心组成要素:
1、actor:分享者,即用户。
2、action:内容,比如:烹饪,跑步,读书。
3、object:动作,比如:做一顿饭,跑一次跑步,看一本书。
4、app:你的app。
效果如下:
让你的app连接到news feed。
当用户点击这个故事时,会有两种情况,用户如果已经安装你的app,会打开你的app,如果没有,会跳转到appstore。
流程如下:
比如,如果我在我的facebook看到了你的分享页面,我打开点连接跳转到这个app时希望直接跳转到你分享那个view,而不是你app的root view。
设置app links:
app的link从app dashboard设置。需要如下:
Bundle ID:关联你的app。
iPhone App Store ID:你app在app store的id。
iPad App Store ID:
Facebook Login:允许。
Deep Linking:允许。
注意plist里面的bundle id要和facebook里面的应用设置里面一样,