Facebook三種分享方式


一、去Facebook開發者中心注冊APP,獲取APP ID  https://developers.facebook.com 

二、導入 FBSDKCoreKit.Framework, FBSDKLoginKit.Framework, FBSDKShareKit.Framework

三、在info.plist 文件中加入
     
<key>CFBundleURLTypes</key>
<array>
  <dict>
  <key>CFBundleURLSchemes</key>
  <array>
    <string>fb218334765200160</string>
  </array>
  </dict>
</array>
<key>FacebookAppID</key>
<string>218334765200160</string>
<key>FacebookDisplayName</key>
<string>AirPlane</string>

<key>LSApplicationQueriesSchemes</key>
<array>
  <string>fbapi</string>
  <string>fb-messenger-api</string>
  <string>fbauth2</string>
  <string>fbshareextension</string>
</array>

四、設置bundle id

     


五、AppDelegate中設置

#import <FBSDKCoreKit/FBSDKCoreKit.h>

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    [[FBSDKApplicationDelegate sharedInstance] application:application
                             didFinishLaunchingWithOptions:launchOptions];
    return YES;
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
    [FBSDKAppEvents activateApp];
}

- (BOOL)application:(UIApplication *)application
            openURL:(NSURL *)url
  sourceApplication:(NSString *)sourceApplication
         annotation:(id)annotation {
    return [[FBSDKApplicationDelegate sharedInstance] application:application
                                                          openURL:url
                                                sourceApplication:sourceApplication
                                                       annotation:annotation];
}


六、控制器中設置

#import "ViewController.h"
#import <FBSDKShareKit/FBSDKShareKit.h>
#import <Social/Social.h>
#import "UIToastUtil.h"

@interface ViewController ()<FBSDKSharingDelegate>
@property (nonatomic, strong) SLComposeViewController *mySLComposerSheet;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}

/** 首先調用facebook客戶端 */
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self shareToFacebook];
}

/** 有Facebook客戶端 */
- (void)shareToFacebook{
    FBSDKShareLinkContent *content1 = [[FBSDKShareLinkContent alloc] init];
    content1.contentURL = [NSURL URLWithString:@"https://developers.facebook.com"];
    content1.contentTitle=@"我就是分享";
   
    //    [FBSDKShareDialog showFromViewController:self withContent:content1 delegate:nil];
    //    [FBSDKMessageDialog showWithContent:content1 delegate:nil];
    FBSDKShareDialog *dialog = [[FBSDKShareDialog alloc] init];
    dialog.fromViewController = self;
    dialog.delegate = self;
    [dialog setShareContent:content1];
    dialog.mode = FBSDKShareDialogModeNative;
    [dialog show];
}

#pragma mark -FBSHARE DELEGATE
- (void)sharer:(id<FBSDKSharing>)sharer didCompleteWithResults:(NSDictionary *)results{
    NSLog(@"--->有Facebook客戶端,成功分享!");
}
- (void)sharer:(id<FBSDKSharing>)sharer didFailWithError:(NSError *)error{
    NSLog(@"--->分享失敗!, %@", error);
    if(error==nil||[[NSNull null] isEqual:error]){
        /** 沒有facebook客戶端,檢查手機是否綁定賬號 */
        [self shareFacebook];
    }
}

- (void)sharerDidCancel:(id<FBSDKSharing>)sharer{
    NSLog(@"--->取消分享!");
}

- (void)shareFacebook{
    if ([[[[UIDevice currentDevice] systemVersion] substringToIndex:1] intValue]>=6){
        if([SLComposeViewController isAvailableForServiceType:SLServiceTypeFacebook]){
            self.mySLComposerSheet = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeFacebook];
            //            [self.mySLComposerSheet setInitialText:self.productDetailModel.productName];//產品名
            //            NSURL *imageUrl = [NSURL URLWithString:self.productDetailModel.primaryThumbimage];
            //            NSData *imageData = [NSData dataWithContentsOfURL:imageUrl];
            //            [self.mySLComposerSheet addImage:[UIImage imageWithData:imageData scale:1]];//產品圖
            [self.mySLComposerSheet addURL:[NSURL URLWithString:@"https://developers.facebook.com"]];//產品地址
            [self presentViewController:self.mySLComposerSheet animated:YES completion:nil];
            [MBProgressHUD hideHUDForView:self.view animated:YES];
        }else{ /** 沒有安裝客戶端,並且手機也沒有綁定賬號,使用網頁分享  */
            [self shareWithWeb];
        }
        [self.mySLComposerSheet setCompletionHandler:^(SLComposeViewControllerResult result) {
            NSString *output;
            switch (result) {
                case SLComposeViewControllerResultCancelled:
                    output = @"點擊取消分享";
                    break;
                case SLComposeViewControllerResultDone:
                    output = @"點擊分享";
                    break;
                default:
                    break;
            }
            if ((result = SLComposeViewControllerResultCancelled)) {
                //        [UIToastUtil showToast:self.view message:@"you have not install facebook app."];
                //                UIAlertView *alert = [[UIAlertView alloc] initWithTitle:MBLocalizedString(kFacebookMessage) message:output delegate:nil cancelButtonTitle:MBLocalizedString(kFacebookOK) otherButtonTitles:nil];
                //                [alert show];
            }
        }];
    }
}

/** 沒有Facebook客戶端,手機Facebook也沒有綁定賬號,使用網頁分享 */
- (void)shareWithWeb{
    FBSDKShareLinkContent *content = [[FBSDKShareLinkContent alloc] init];
    content1.contentURL = [NSURL URLWithString:@"https://developers.facebook.com"];
    content1.contentTitle=@“這是分享";

    FBSDKShareDialog *dialog = [[FBSDKShareDialog alloc] init];
    dialog.fromViewController = self;
    dialog.delegate = self;
    [dialog setShareContent:content];
    dialog.mode = FBSDKShareDialogModeWeb;
    [dialog show];
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM