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];
}

 

转载于:https://www.cnblogs.com/10-19-92/p/5380450.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值