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[email protected]“这是分享";

    FBSDKShareDialog *dialog = [[FBSDKShareDialog alloc] init];
    dialog.fromViewController = self;
    dialog.delegate = self;
    [dialog setShareContent:content];
    dialog.mode = FBSDKShareDialogModeWeb;
    [dialog show];
}
时间: 2024-08-07 20:59:43

Facebook三种分享方式的相关文章

支付宝5月4日起将停止收款主页业务 保留三种收款方式

4月28日消息,支付宝近日发布公告称,将于5月4日起停止收款主页业务(产品功能将无法使用),但并未公布停止业务的具体信息. 据了解,收款主页业务是用户可以自己制作一个支付宝账号的链接,把这个链接发给付款人后,对方就可以输入金额给该用户付款. 支付宝收款主页截图 支付宝公告称,收款主页业务停止之后,用户可以有三种方式进行收款:生成专属支付宝收款账户码,将账户二维码图片分享出去:在电脑上使用我要收款:在手机上,使用支付宝钱包的我要收款. 以下为公告原文: 鉴于收款主页(https://me.alip

CSS的三种定位方式介绍(转载)

在CSS中一共有N种定位方式,其中,static ,relative,absolute三种方式是最基本最常用的三种定位方式.他们的基 本介绍如下. static默认定位方式relative相对定位,相对于原来的位置,但是原来的位置仍然保留absolute定位,相对于最近的非标准刘定位,原来的位置消失,被后边的位置所顶替 下面先演示相对定位的案例 [html] view plain copyprint? <!DOCTYPE html> <html> <head> <

MyEclipse中web服务器的三种配置方式

初学Javaweb开发的人们都会遇到一个问题,就是服务器环境的搭建配置问题.下面介绍三种服务器的搭建方式. 直接修改server.xml文件 当你写了一个web应用程序(jsp/servlet),想通过浏览器直接去访问这个页面,需要在Tomcat中配置相关路径: 找到Tomcat下conf目录,你会看到有个server.xml,即服务器配置文件.用文本编译器打开,拉到Host标签,在它结束前加上我们的应用程序路径: <Context path="/HelloWeb" docBas

1、打印二进制机器码,程序内存分析,大端序小端序,指针数组,数组指针,数组的三种访问方式,typedef,#if-0-#endif,求数组大小,括号表达式

 1.打印二进制机器码(分别表示32位的和64位的) #include <stdio.h> /*按照8位的长度打印一个数值*/ void dis8bit(char val) { int bit = 8; while(bit--) { if(1<<bit&val){ printf("1"); } else { printf("0"); } if(!(bit%4)) printf(" "); } putchar(1

SQL Server 中的三种分页方式

USE tempdb GO SET NOCOUNT ON --创建表结构 IF OBJECT_ID(N'ClassB', N'U') IS NOT NULL DROP TABLE ClassB GO CREATE TABLE ClassB(ID INT PRIMARY KEY, Name VARCHAR(16), CreateDate DATETIME, AID INT, Status INT) CREATE INDEX IDX_CreateDate ON ClassB(CreateDate)

Hibernate的Api以及三种查询方式

Hibernate  Api |-- Configuration       配置管理类对象 config.configure();    加载主配置文件的方法(hibernate.cfg.xml) 默认加载src/hibernate.cfg.xml config.configure("cn/config/hibernate.cfg.xml");   加载指定路径下指定名称的主配置文件 config.buildSessionFactory();   创建session的工厂对象 |--

使用JavaScript判断图片是否加载完成的三种实现方式

有时需要获取图片的尺寸,这需要在图片加载完成以后才可以.有三种方式实现,下面一一介绍. 一.load事件 <!DOCTYPE HTML> <html> <head> <meta charset="utf-8"> <title>img - load event</title> </head> <body> <img id="img1" src="http:/

数组的三种定义方式

// 数组的三种定义方式 var arr = ['zhangsan','lisi','wangwu']; var arr1 = new Array('zhangsan','lisi','wangwu'); var arr2 = new Array(3); arr2[0] = 'zhangsan'; //注意:下标一定要写,不像在php中,下标是可以不写,是一直追加 // console.log(arr2); // 说明:js中,没有关联数组一说,数组下标都是数字 // 如果一个数组的下标是自定义

垃圾回收(GC)的三种基本方式

垃圾(Garbage)就是程序需要回收的对象,如果一个对象不在被直接或间接地引用,那么这个对象就成为了「垃圾」,它占用的内存需要及时地释放,否则就会引起「内存泄露」.有些语言需要程序员来手动释放内存(回收垃圾),有些语言有垃圾回收机制(GC).本文就来讨论GC实现的三种基本方式. 其实这三种方式也可以大体归为两类:跟踪回收,引用计数.美国IBM的沃森研究中心David F.Bacon等人发布的「垃圾回收统一理论」一文阐述了一个理论:任何垃圾回收的思路,无非以上两种的组合,其中一种的改善和进步,必