【教學】Facebook SDK 三部曲(下):點一下,分享到Facebook(這是懶人包...)

這篇跟大家分享幾種比較常見的,推出Facebook分享介面的方法,


我們有:

1. 內建的分享介面。
    我們用的是:
    a. UIActivityViewController,iOS 6.0 up。
    b. social.frameworkSLComposeViewController,iOS 6.0 up。
    c. Facebook SDK 的 FBDialogs




2. 呼叫Facebook App的分享介面。
    我們用的是:Facebook SDK 的 FBDialogs




因為這次分享的方法,都是利用使用者設定在手機裡的Facebook帳號,所以只需要準備:

1. Facebook SDK 3.0 up。
2. social.framework,iOS framework有。
3. 實機,iOS6.0 up。



因為使用方法都挺簡便的,我們直接來寫Code吧!


內建的分享介面


 a. UIActivityViewController。



- (IBAction)pushCustomActivityView:(id)sender {

    

    NSString *words = @"我很帥!!!";

    UIImage *sharePic = [UIImage imageNamed:@"google.jpg"];

    NSURL *shareURL = [NSURL URLWithString:@"http://https://www.google.com.tw/search?q=%E6%88%91%E5%BE%88%E5%B8%A5&ie=utf-8&oe=utf-8&rls=org.mozilla:zh-TW:official&client=firefox-a&channel=fflb&gws_rd=cr"];

    

    NSArray *item = @[words, sharePic, shareURL];

    

    UIActivityViewController *ac = [[UIActivityViewController alloc] initWithActivityItems:item applicationActivities:nil];

    ac.excludedActivityTypes = @[UIActivityTypeCopyToPasteboard, UIActivityTypeMail, UIActivityTypeMessage, UIActivityTypeSaveToCameraRoll, UIActivityTypePrint];

    

    [ac setCompletionHandler:^(NSString *activityType, BOOL completed) {

        //  while complete, do this

        NSLog(@"Is complete ? %@, activityType : %@", completed?@"YES":@"NO", activityType);

    }];

    

    [self presentViewController:ac animated:YES completion:nil];

 } 





Note:

1. ActivityItem,放的是要分享的文字、連結、圖片。
2. applicationActivities,放的是自定的UIActivityView。
3. 如果不要有其他icon選項出現,設定在excludedActivityTypes裡,預設的社交服務有Facebook、Twitter、WeiBo,其他選項可以在這裡的Constants找到。


注意!

UIActivityView 在模擬器上,如果沒有設定Facebook帳號,會以AlertView來提醒,

但是實機來測試,如果沒有設定,就不會出現選項,也沒有提醒。

(Twitter沒設帳號)

(只剩Facebook)
(設定完成)
(Twitter出現了!)


b. SLComposeViewController。
    請先#import <Social/Social.h>。




 - (IBAction)socialAction:(id)sender {

   

    NSString *shareWords = @"我很帥!";

    UIImage *shareImage = [UIImage imageNamed:@"google.jpg"];

   

    SLComposeViewController *shareView = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeFacebook];

    [shareView setInitialText:shareWords];

    [shareView addImage:shareImage];

   

    [shareView setCompletionHandler:^(SLComposeViewControllerResult result) {

        //  while complete, do this

        switch (result) {

            case SLComposeViewControllerResultCancelled:

                NSLog(@"User Cancelled.");

                break;

            case SLComposeViewControllerResultDone:

                NSLog(@"Share Done.");

            break;

        }

    }];

   

    [self presentViewController:shareView animated:YES completion:nil];

} 




Note:

1. 支援的服務在這裡
2. social.framework也有提供像FBRequestConnection的Class:SLRequest
    不過很不好用 : p,很多狀況不能處理,有空可以試一試。



- (IBAction)sendSLRequest:(id)sender {
    
    NSURL *requestURL = [NSURL URLWithString:@"https://graph.facebook.com/me/feed"];
    NSString *shareWords = @"我很帥!!!";
    NSMutableDictionary *para = [[NSMutableDictionary alloc] init];
    [para setValue:shareWords forKey:@"message"];
    
    SLRequest *request = [SLRequest requestForServiceType:SLServiceTypeFacebook
                                            requestMethod:SLRequestMethodPOST
                                                      URL:requestURL
                                               parameters:para];
    
    [request performRequestWithHandler:^(NSData *responseData,
                                         NSHTTPURLResponse *urlResponse,
                                         NSError *error) {
        //  while complete, do this
        NSString *result = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
        
        NSLog(@"Response : %@", [urlResponse allHeaderFields]);
        NSLog(@"Result : %@", result);
        NSLog(@"Error : %@", error?error:@"OK");
    }];
} 

  
(碰到狀況就頭大了)


c.  FBDialogs
    請先#import <FacebookSDK/FacebookSDK.h>。




- (IBAction)presentByFBSDK:(id)sender {

    

    NSString *shareWords = @"我很帥!";

    UIImage *shareImage = [UIImage imageNamed:@"google.jpg"];

    NSURL *shareURL = [NSURL URLWithString:@"http://https://www.google.com.tw/search?q=%E6%88%91%E5%BE%88%E5%B8%A5&ie=utf-8&oe=utf-8&rls=org.mozilla:zh-TW:official&client=firefox-a&channel=fflb&gws_rd=cr"];

    

    [FBDialogs presentOSIntegratedShareDialogModallyFrom:self

                                             initialText:shareWords

                                                   image:shareImage

                                                     url:shareURL

                                                 handler:^(FBOSIntegratedShareDialogResult result, NSError *error) {

        //  while complete, do this

        switch (result) {

            case FBOSIntegratedShareDialogResultSucceeded:

                NSLog(@"Share Success.");

                break;

            case FBOSIntegratedShareDialogResultCancelled:

                NSLog(@"Share Cancelled.");

                break;

            case FBOSIntegratedShareDialogResultError:

                NSLog(@"Share Error.");

                break;

        }

        NSLog(@"Share error : %@", error?error:@"OK");

        

    }];

    

} 






呼叫Facebook App的分享介面


請先#import <FacebookSDK/FacebookSDK.h>。




- (IBAction)presentByFBApp:(id)sender {

    NSString *shareWords = @"我很帥!";

    NSURL *shareURL = [NSURL URLWithString:@"http://https://www.google.com.tw/search?q=%E6%88%91%E5%BE%88%E5%B8%A5&ie=utf-8&oe=utf-8&rls=org.mozilla:zh-TW:official&client=firefox-a&channel=fflb&gws_rd=cr"];

    

    [FBDialogs presentShareDialogWithLink:shareURL

                                     name:shareWords

                                  handler:^(FBAppCall *call, NSDictionary *results, NSError *error) {

        //  while complete, do this

                                      NSLog(@"Share Result : %@", results);

                                      NSLog(@"Share error : %@", error?error:@"OK");

    }];

    

}



(這一定又有什麼誤會了...)


Ya!終於結束了!....


參考文件


Using the Share Dialog

FBDialogs

UIActivityViewController

Social framework



留言

張貼留言

這個網誌中的熱門文章

【給程式新手】陰魂不散的物件導向?

【教學】Facebook SDK 首部曲:登入Facebook(這是我的一小步...卻是人類...咦?這麼多人啊?)

【教學】Facebook SDK 前傳:準備Social一下