IOS 植入Facebook SDK --- 分享

國內先翻牆,翻牆請用vpn,我有幾個免費的vpn可以使用

共享

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。

didComplete

BOOL

Always available. YES if the dialog completed successfully; NO if the dialog completed with an error.

completionGesture

NSString

Only available if the user logged into your app using Facebook and didComplete isYES. Value is eitherpost orcancel.

postId

NSString

Only available if the user is logged into your app using Facebook, has granted a publish permission (e.g.publish_actions), and the user chose to share the story. If present, this is the ID of the published story.

接下來要做的事情是爲了得到一個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裏面的應用設置裏面一樣,


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章