Share 分享/第三方登录

1、系统方式创建分享

  • 按照下图在 Info.plist 文件中将 Localization native development region 的值改为 China。如果不设置此项弹出的分享页面中显示的按钮为英文说明。

  • UIActivityViewController 方式创建

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    // 设置分享的内容
    NSString *textToShare = @"请大家登录《iOS云端与网络通讯》服务网站。";
    UIImage *imageToShare = [UIImage imageNamed:@"swift"];
    NSURL *urlToShare = [NSURL URLWithString:@"http://m.baidu.com"];

    // 创建分享视图控制器
    /*
    activityItems: 分享的内容
    applicationActivities: 分享的类型,默认(nil)时为 UIActivity
    */

    NSArray *items = @[textToShare, imageToShare, urlToShare];

    UIActivityViewController *activityVC = [[UIActivityViewController alloc] initWithActivityItems:items
    applicationActivities:nil];

    // 设置不出现的分享按钮
    /*
    Activity 类型又分为 “操作” 和 “分享” 两大类:

    UIActivityCategoryAction 操作:

    UIActivityTypeAirDrop AirDrop AirDrop
    UIActivityTypePrint 打印 Print

    UIActivityTypeSaveToCameraRoll 保存到相册 Save Image
    UIActivityTypeAssignToContact 添加到联系人 AssignToContact
    UIActivityTypeAddToReadingList 添加到 Safari 阅读列表 AddToReadingList
    UIActivityTypeCopyToPasteboard 复制到剪贴板 Copy
    UIActivityTypeOpenInIBooks 在 iBook 中打开

    UIActivityCategoryShare 分享:

    UIActivityTypeMail 邮箱 Mail
    UIActivityTypeMessage 短信 Message

    UIActivityTypePostToTwitter 分享到 Twitter
    UIActivityTypePostToFacebook 分享到 Facebook
    UIActivityTypePostToVimeo 分享到 Vimeo(视频媒体)
    UIActivityTypePostToFlickr 分享到 Flickr(网络相簿)
    UIActivityTypePostToWeibo 分享到 新浪微博
    UIActivityTypePostToTencentWeibo 分享到 腾讯微博
    */

    // 添加到此数组中的系统分享按钮项将不会出现在分享视图控制器中
    activityVC.excludedActivityTypes = @[UIActivityTypeAssignToContact, UIActivityTypePrint];

    // 显示分享视图控制器
    [self presentViewController:activityVC animated:YES completion:nil];

    // 分享完成
    activityVC.completionWithItemsHandler = ^(NSString *activityType,
    BOOL completed,
    NSArray *returnedItems,
    NSError *activityError) {
    // 分享完成或退出分享时调用该方法

    if (completed) {
    NSLog(@"分享完成");
    } else {
    NSLog(@"取消分享");
    }
    };
  • 效果

2、系统方式自定义分享

  • 按照下图在 Info.plist 文件中将 Localization native development region 的值改为 China。如果不设置此项弹出的分享页面中显示的按钮为英文说明。

  • 自定义按钮

    • myUIActivity.h

      1
      2
      3
      4
      5
      #import <UIKit/UIKit.h>

      @interface myUIActivity : UIActivity <UINavigationControllerDelegate>

      @end
    • myUIActivity.m

      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
      22
      23
      24
      25
      26
      27
      28
      29
      30
      31
      32
      33
      34
      35
      36
      37
      38
      39
      40
      41
      42
      43
      44
      45
      46
      47
      48
      49
      50
      51
      52
      53
      54
      55
      56
      57
      58
      59
      60
      61
      62
      63
      64
      65
      66
      67
      68
      69
      70
      #import "myUIActivity.h"

      /*
      自定义分享按钮
      */

      @implementation myUIActivity

      // 设置分享按钮的类型
      - (NSString *)activityType {

      // 在 completionWithItemsHandler 回调里可以用于判断,一般取当前类名
      return NSStringFromClass([myUIActivity class]);
      }

      // 设置分享按钮的标题
      - (NSString *)activityTitle {

      // 设置显示在分享框里的名称
      return @"自定义分享按钮";
      }

      // 设置分享按钮的图片
      - (UIImage *)activityImage {

      // 图片自定变为黑白色,默认尺寸为 56 * 56 像素
      return [UIImage imageNamed:@"JHQ0228"];
      }

      // 设置是否显示分享按钮
      - (BOOL)canPerformWithActivityItems:(NSArray *)activityItems {

      // 这里一般根据用户是否授权等来决定是否要隐藏分享按钮
      return YES;
      }

      // 预处理分享数据
      - (void)prepareWithActivityItems:(NSArray *)activityItems {

      // 解析分享数据时调用,可以进行一定的处理
      NSLog(@"prepareWithActivityItems");

      // 手动执行分享操作,保存到相册
      UIImageWriteToSavedPhotosAlbum(activityItems[1], nil, nil, nil);
      }

      // 执行分享
      - (UIViewController *)activityViewController {

      // 点击自定义分享按钮时调用,跳转到自定义的视图控制器
      NSLog(@"activityViewController");

      return nil;
      }

      // 执行分享
      - (void)performActivity {

      // 点击自定义分享按钮时调用
      NSLog(@"performActivity");
      }

      // 完成分享
      - (void)activityDidFinish:(BOOL)completed {

      // 分享视图控制器退出时调用
      NSLog(@"activityDidFinish");
      }

      @end
  • 使用自定义按钮

    • ViewController.m

      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
      22
      23
      24
      25
      26
      27
      28
      29
      30
      31
      32
      33
      34
      35
      36
      #import "myUIActivity.h"

      // 设置分享的内容
      NSString *textToShare = @"请大家登录《iOS云端与网络通讯》服务网站。";
      UIImage *imageToShare = [UIImage imageNamed:@"swift"];
      NSURL *urlToShare = [NSURL URLWithString:@"http://m.baidu.com"];

      // 设置分享的类型
      myUIActivity *myActivity = [[myUIActivity alloc] init];

      // 创建分享视图控制器
      NSArray *items = @[textToShare, imageToShare, urlToShare];
      NSArray *activities = @[myActivity];

      UIActivityViewController *activityVC = [[UIActivityViewController alloc] initWithActivityItems:items
      applicationActivities:activities];

      // 设置不出现的分享按钮
      activityVC.excludedActivityTypes = @[UIActivityTypeAirDrop];

      // 显示分享视图控制器
      [self presentViewController:activityVC animated:YES completion:nil];

      // 分享完成
      activityVC.completionWithItemsHandler = ^(NSString *activityType,
      BOOL completed,
      NSArray *returnedItems,
      NSError *activityError) {
      // 分享完成或退出分享时调用该方法

      if (completed) {
      NSLog(@"分享完成");
      } else {
      NSLog(@"取消分享");
      }
      };
  • 效果

3、友盟 登录/分享 集成

  • U-Share 快速集成多平台分享、登录功能。帮助应用或游戏快速具备国内外多平台分享、第三方登录功能,SDK 包最小,集成成本最低,平台覆盖最全,并基于友盟+大数据,提供最为权威、实时的用户画像、分享回流等数据分析,助力产品开发与推广。

  • 覆盖国内外近 30 家社交平台,支持文本、图片、音乐、视频、链接等多种内容类型的分享,并提供了主流游戏平台的 SDK。

    • 国内平台:微信、朋友圈、QQ、Qzone、新浪微博、腾讯微博、人人、豆瓣、易信、短信、邮件等。
    • 国外平台:Facebook、Twitter、Instagram、Google+、LINE、WhatsApp、Pinterest、Evernote、Pocket、LinkedIn、KakaoTalk 等。
  • U-Share 集成流程

  • U-Share 第三方账号申请及绑定

  • U-Share 快速集成文档

  • U-Share API 说明

  • U-Share SDK 下载

  • 集成友盟社会化组件流程

3.1 第三方账号申请及绑定

3.1.1 申请第三方账号

  • 进行分享、授权操作需要在第三方平台创建应用并提交审核,友盟默认提供了大多数平台的测试账号,但如果需要将分享、授权来源、分享到 QQ、Qzone 的 icon 更改为自己 APP 的应用,就需要自己申请第三方账号。

  • 新浪微博

    • 登录新浪微博开放平台,填写相关应用信息并上传 icon 图片。注意修改安全域名为 sns.whalecloud.com 同时设置授权回调页为 http://sns.whalecloud.com/sina2/callback 安全域名设置在应用信息 –> 基本信息,具体位置参考下

    • 授权回调页、取消授权回调页设置在应用信息 –> 高级信息,具体位置参考下图

    • 安全域名的修改需要二次审核通过才生效,授权回调页修改即时生效

  • 微信

    • 登录微信开放平台,填写相关应用信息,审核通过后获取到微信 AppID 及 AppSecret,如果需要微信登录功能,需要申请微信登录权限。
  • QQ 及 Qzone

    • QQ 及 Qzone 使用同一个 AppID 及 Appkey,登录腾讯开放平台,选择移动应用,填写相关应用信息并提交审核,未审核前通过只能使用测试账号,添加测试账号方法如下:选择用户能力 –> 进阶社交能力 –> 应用调试者,添加测试账号必须在申请者好友列表中,如下图

  • 人人网

    • 登录人人开放平台,填写相关应用信息,同时填写应用根域名为 sns.whalecloud.com 具体位置: 基本信息 –> 应用根域名 如图

  • 豆瓣

    • 登录豆瓣开放平台,创建应用并填写相关应用信息,注意权限必须选择广播,同时填写回调地址为 http://sns.whalecloud.com/douban/callback

3.1.2 绑定第三方账号到友盟后台

  • 目前需要在友盟后台绑定的第三方账号为:新浪微博、腾讯微博、人人网、豆瓣、Qzone,其余平台如微信、QQ 直接在代码中设置。

  • 绑定地址:http://umeng.com/apps,登录友盟网站 -> 左上角选择你们的产品 -> 组件 -> 社会化组件 -> 设置

  • 短链接开关

    • 短链接开关只对新浪微博、腾讯微博、人人网、豆瓣四个平台有效,开启短链接开关,分享文案中附加的链接会被转码,同时可以统计到分享回流率(点击链接的次数),关闭短链接开关则无法统计,短链接开关默认为关闭状态。
  • 文字截断开关

    • 文字截断开关只对新浪微博、腾讯微博、人人网、豆瓣四个平台有效,同时只对使用自定义分享编辑页或没有分享编辑页用户有效,当分享文案超出字数限制时自动截断,开关状态默认关闭。

3.2 U-Share SDK 集成

3.2.1 下载 U-Share SDK

  • 通过 iOS 社会化组件下载页面选择所需的社交平台后进行下载。

3.2.2 加入 U-Share SDK

  • 将 U-Share SDK 添加到工程

  • 添加项目配置,在 Other Linker Flags 加入 -ObjC

  • 加入依赖系统库 libsqlite3.tbd

  • 添加平台相应的依赖库,根据集成的不同平台加入相关的依赖库,未列出平台则不用添加。添加方式:选中项目 Target -> Linked Frameworks and Libraries 列表中添加

    • 注:Twitter 平台加入后需添加 TwitterKit.framework/Resources/TwitterKitResources.bundle。

3.3 U-Share SDK 平台配置

3.3.1 配置各平台 URL Scheme

  • 添加 URL Types

    • URL Scheme 是通过系统找到并跳转对应 app 的一类设置,通过向项目中的 info.plist 文件中加入 URL types 可使用第三方平台所注册的 appkey 信息向系统注册你的 app,当跳转到第三方应用授权或分享后,可直接跳转回你的 app。

    • 添加 URL Types 有如下几处,都可进行设置

      • 1、通过工程设置面板

      • 2、通过 info.plist 文件编辑

      • 3、直接编辑 info.plist 中 XML 代码

  • 配置第三方平台 URL Scheme

    • 未列出则不需设置

3.3.2 适配 iOS 9/10 系统

  • iOS9 系统后 Apple 对 HTTP 请求及访问外部应用做了更加严格的要求,包括 HTTP 白名单、跳转第三方应用白名单等,具体设置第三方平台参数请参照适配 iOS9/10 系统

  • HTTPS 传输安全

    • Apple 将从 2017 年开始执行 ATS(App Transport Security),所有进行审核的应用中网络请求全部支持 HTTPS,届时以下配置将会失效,请提前做好准备。

    • 以 iOS10 SDK 编译的工程会默认以 SSL 安全协议进行网络传输,即 HTTPS,如果依然使用 HTTP 协议请求网络会报系统异常并中断请求。目前可用如下两种方式保持用 HTTP 进行网络连接:

    • 在 info.plist 中加入安全域名白名单(右键 info.plist 用 source code 打开)

      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
      22
      23
      24
      25
      26
      27
      28
      29
      30
      31
      32
      33
      34
      35
      36
      37
      38
      39
      40
      41
      42
      43
      44
      45
      46
      47
      48
      49
      50
      51
      52
      53
      54
      55
      56
      57
      58
      59
      60
      61
      62
      63
      64
      65
      66
      67
      68
      69
      70
      71
      72
      73
      74
      75
      76
      77
      78
      79
      80
      <key>NSAppTransportSecurity</key>
      <dict>
      <!-- 配置允许 http 的任意网络End-->
      <key>NSExceptionDomains</key>
      <dict>
      <!-- 集成新浪微博对应的HTTP白名单-->
      <key>sina.com.cn</key>
      <dict>
      <key>NSIncludesSubdomains</key>
      <true/>
      <key>NSThirdPartyExceptionAllowsInsecureHTTPLoads</key>
      <true/>
      <key>NSThirdPartyExceptionRequiresForwardSecrecy</key>
      <false/>
      </dict>
      <key>sinaimg.cn</key>
      <dict>
      <key>NSIncludesSubdomains</key>
      <true/>
      <key>NSThirdPartyExceptionAllowsInsecureHTTPLoads</key>
      <true/>
      <key>NSThirdPartyExceptionRequiresForwardSecrecy</key>
      <false/>
      </dict>
      <key>sinajs.cn</key>
      <dict>
      <key>NSIncludesSubdomains</key>
      <true/>
      <key>NSThirdPartyExceptionAllowsInsecureHTTPLoads</key>
      <true/>
      <key>NSThirdPartyExceptionRequiresForwardSecrecy</key>
      <false/>
      </dict>
      <key>sina.cn</key>
      <dict>
      <!-- 适配iOS10 -->
      <key>NSExceptionMinimumTLSVersion</key>
      <string>TLSv1.0</string>
      <key>NSIncludesSubdomains</key>
      <true/>
      <key>NSThirdPartyExceptionRequiresForwardSecrecy</key>
      <false/>
      </dict>
      <key>weibo.cn</key>
      <dict>
      <!-- 适配iOS10 -->
      <key>NSExceptionMinimumTLSVersion</key>
      <string>TLSv1.0</string>
      <key>NSIncludesSubdomains</key>
      <true/>
      <key>NSThirdPartyExceptionRequiresForwardSecrecy</key>
      <false/>
      </dict>
      <key>weibo.com</key>
      <dict>
      <!-- 适配iOS10 -->
      <key>NSExceptionMinimumTLSVersion</key>
      <string>TLSv1.0</string>
      <key>NSIncludesSubdomains</key>
      <true/>
      <key>NSThirdPartyExceptionAllowsInsecureHTTPLoads</key>
      <true/>
      <key>NSThirdPartyExceptionRequiresForwardSecrecy</key>
      <false/>
      </dict>
      <!-- 新浪微博-->

      <!-- 集成人人授权对应的HTTP白名单-->
      <key>renren.com</key>
      <dict>
      <key>NSIncludesSubdomains</key>
      <true/>
      <key>NSThirdPartyExceptionAllowsInsecureHTTPLoads</key>
      <true/>
      <key>NSThirdPartyExceptionRequiresForwardSecrecy</key>
      <false/>
      </dict>
      <!-- 人人授权-->
      </dict>
      </dict>
    • 若新版 Xcode 控制台输出 “[] tcp_connection_xxx“ 等内容,可以在运行按钮旁的选择 target 选项内的 Edit Scheme - Run - Arguments - Enviroment variables 中增加 OS_ACTIVITY_MODE=disable,可将相关日志关闭。

  • 配置 ApplicationQueriesSchemes(应用间跳转)

    • 如果你的应用使用了如 SSO 授权登录或跳转到第三方分享功能,在 iOS9/10 下就需要增加一个可跳转的白名单,即 LSApplicationQueriesSchemes,否则将在 SDK 判断是否跳转时用到的 canOpenURL 时返回 NO,进而只进行 webview 授权或授权/分享失败。 在项目中的 info.plist 中加入应用白名单,右键 info.plist 选择 source code 打开(具体设置在 Build Setting -> Packaging -> Info.plist File 可获取 plist 路径)。

      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
      22
      23
      24
      25
      26
      27
      28
      29
      30
      31
      32
      33
      34
      35
      36
      37
      38
      39
      40
      41
      42
      43
      44
      45
      46
      47
      48
      49
      50
      51
      52
      53
      54
      55
      56
      57
      58
      59
      60
      61
      62
      63
      64
      65
      66
      67
      68
      69
      70
      71
      72
      73
      74
      75
      76
      77
      78
      79
      80
      81
      82
      83
      84
      85
      <key>LSApplicationQueriesSchemes</key>
      <array>
      <!-- 微信 URL Scheme 白名单-->
      <string>wechat</string>
      <string>weixin</string>

      <!-- 新浪微博 URL Scheme 白名单-->
      <string>sinaweibohd</string>
      <string>sinaweibo</string>
      <string>sinaweibosso</string>
      <string>weibosdk</string>
      <string>weibosdk2.5</string>

      <!-- QQ、Qzone URL Scheme 白名单-->
      <string>mqqapi</string>
      <string>mqq</string>
      <string>mqqOpensdkSSoLogin</string>
      <string>mqqconnect</string>
      <string>mqqopensdkdataline</string>
      <string>mqqopensdkgrouptribeshare</string>
      <string>mqqopensdkfriend</string>
      <string>mqqopensdkapi</string>
      <string>mqqopensdkapiV2</string>
      <string>mqqopensdkapiV3</string>
      <string>mqqopensdkapiV4</string>
      <string>mqzoneopensdk</string>
      <string>wtloginmqq</string>
      <string>wtloginmqq2</string>
      <string>mqqwpa</string>
      <string>mqzone</string>
      <string>mqzonev2</string>
      <string>mqzoneshare</string>
      <string>wtloginqzone</string>
      <string>mqzonewx</string>
      <string>mqzoneopensdkapiV2</string>
      <string>mqzoneopensdkapi19</string>
      <string>mqzoneopensdkapi</string>
      <string>mqqbrowser</string>
      <string>mttbrowser</string>

      <!-- 支付宝 URL Scheme 白名单-->
      <string>alipay</string>
      <string>alipayshare</string>

      <!-- 人人 URL Scheme 白名单-->
      <string>renrenios</string>
      <string>renrenapi</string>
      <string>renren</string>
      <string>renreniphone</string>

      <!-- 来往 URL Scheme 白名单-->
      <string>laiwangsso</string>

      <!-- 易信 URL Scheme 白名单-->
      <string>yixin</string>
      <string>yixinopenapi</string>

      <!-- instagram URL Scheme 白名单-->
      <string>instagram</string>

      <!-- whatsapp URL Scheme 白名单-->
      <string>whatsapp</string>

      <!-- line URL Scheme 白名单-->
      <string>line</string>

      <!-- Facebook URL Scheme 白名单-->
      <string>fbapi</string>
      <string>fb-messenger-api</string>
      <string>fbauth2</string>
      <string>fbshareextension</string>

      <!-- Kakao URL Scheme 白名单-->
      <!-- 注:以下第一个参数需替换为自己的kakao appkey-->
      <!-- 格式为 kakao + "kakao appkey"-->
      <string>kakaofa63a0b2356e923f3edd6512d531f546</string>
      <string>kakaokompassauth</string>
      <string>storykompassauth</string>
      <string>kakaolink</string>
      <string>kakaotalk-4.5.0</string>
      <string>kakaostory-2.9.0</string>

      <!-- pinterest URL Scheme 白名单-->
      <string>pinterestsdk.v1</string>
      </array>

3.4 调用 U-Share SDK

3.4.1 初始化设置

  • 初始化 U-Share 及第三方平台

    • app 启动后进行 U-Share 和第三方平台的初始化工作,以下代码将所有平台初始化示例放出,开发者根据平台需要选取相应代码,并替换为所属注册的 appKey 和 appSecret。

    • 在 AppDelegate.m 中设置如下代码

      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
      22
      23
      24
      25
      26
      27
      28
      29
      30
      31
      32
      33
      34
      35
      36
      37
      38
      39
      40
      41
      42
      43
      44
      45
      46
      47
      48
      49
      50
      51
      52
      53
      54
      55
      56
      57
      58
      59
      60
      61
      62
      63
      64
      65
      66
      67
      68
      69
      70
      71
      72
      #import <UMSocialCore/UMSocialCore.h>

      - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

      // 打开调试日志
      [[UMSocialManager defaultManager] openLog:YES];

      // 设置友盟 appkey
      [[UMSocialManager defaultManager] setUmSocialAppkey:@"57b432afe0f55a9832001a0a"];

      // 获取友盟 social 版本号
      // NSLog(@"UMeng social version: %@", [UMSocialGlobal umSocialSDKVersion]);

      // 设置微信的 appKey 和 appSecret
      [[UMSocialManager defaultManager] setPlaform:UMSocialPlatformType_WechatSession
      appKey:@"wxdc1e388c3822c80b"
      appSecret:@"3baf1193c85774b3fd9d18447d76cab0"
      redirectURL:@"http://mobile.umeng.com/social"];

      // 设置分享到 QQ 互联的 appKey 和 appSecret
      // U-Share SDK 为了兼容大部分平台命名,统一用 appKey 和 appSecret 进行参数设置,
      // 而 QQ 平台仅需将 appID 作为 U-Share 的 appKey 参数传进即可。
      [[UMSocialManager defaultManager] setPlaform:UMSocialPlatformType_QQ
      appKey:@"100424468"
      appSecret:nil
      redirectURL:@"http://mobile.umeng.com/social"];

      // 设置新浪的 appKey 和 appSecret
      [[UMSocialManager defaultManager] setPlaform:UMSocialPlatformType_Sina
      appKey:@"3921700954"
      appSecret:@"04b48b094faeb16683c32669824ebdad"
      redirectURL:@"http://sns.whalecloud.com/sina2/callback"];

      // 支付宝的 appKey
      [[UMSocialManager defaultManager] setPlaform:UMSocialPlatformType_AlipaySession
      appKey:@"2015111700822536"
      appSecret:nil
      redirectURL:@"http://mobile.umeng.com/social"];

      // 设置易信的 appKey
      [[UMSocialManager defaultManager] setPlaform:UMSocialPlatformType_YixinSession
      appKey:@"yx35664bdff4db42c2b7be1e29390c1a06"
      appSecret:nil
      redirectURL:@"http://mobile.umeng.com/social"];

      // 设置点点虫(原来往)的 appKey 和 appSecret
      [[UMSocialManager defaultManager] setPlaform:UMSocialPlatformType_LaiWangSession
      appKey:@"8112117817424282305"
      appSecret:@"9996ed5039e641658de7b83345fee6c9"
      redirectURL:@"http://mobile.umeng.com/social"];

      // 设置领英的 appKey 和 appSecret
      [[UMSocialManager defaultManager] setPlaform:UMSocialPlatformType_Linkedin
      appKey:@"81t5eiem37d2sc"
      appSecret:@"7dgUXPLH8kA8WHMV"
      redirectURL:@"https://api.linkedin.com/v1/people"];

      // 设置 Twitter 的 appKey 和 appSecret
      [[UMSocialManager defaultManager] setPlaform:UMSocialPlatformType_Twitter
      appKey:@"fB5tvRpna1CKK97xZUslbxiet"
      appSecret:@"YcbSvseLIwZ4hZg9YmgJPP5uWzd4zr6BpBKGZhf07zzh3oj62K"
      redirectURL:nil];

      // 如果不想显示平台下的某些类型,可用以下接口设置
      // [[UMSocialManager defaultManager] removePlatformProviderWithPlatformTypes:@[@(UMSocialPlatformType_WechatFavorite),
      @(UMSocialPlatformType_YixinTimeLine),
      @(UMSocialPlatformType_LaiWangTimeLine),
      @(UMSocialPlatformType_Qzone)]];
      ...

      return YES;
      }
  • 设置系统回调

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    // 支持所有 iOS 系统
    - (BOOL)application:(UIApplication *)application openURL:(NSURL *)url
    sourceApplication:(NSString *)sourceApplication annotation:(id)annotation {

    BOOL result = [[UMSocialManager defaultManager] handleOpenURL:url];
    if (!result) {
    // 其他如支付等 SDK 的回调
    }
    return result;
    }
    • 注:以上为建议使用的系统 openURL 回调,且 新浪 平台仅支持以上回调。还有以下两种回调方式,如果开发者选取以下回调,也请补充相应的函数调用。

      • 1、仅支持 iOS9 以上系统,iOS8 及以下系统不会回调

        1
        2
        3
        4
        5
        6
        7
        8
        9
        - (BOOL)application:(UIApplication *)app openURL:(NSURL *)url 
        options:(NSDictionary<UIApplicationOpenURLOptionsKey, id> *)options {

        BOOL result = [[UMSocialManager defaultManager] handleOpenURL:url];
        if (!result) {
        // 其他如支付等 SDK 的回调
        }
        return result;
        }
      • 2、支持目前所有 iOS 系统

        1
        2
        3
        4
        5
        6
        7
        8
        - (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url {

        BOOL result = [[UMSocialManager defaultManager] handleOpenURL:url];
        if (!result) {
        // 其他如支付等 SDK 的回调
        }
        return result;
        }

3.4.2 第三方平台登录

  • 支持登录的平台:微信、QQ、新浪微博、腾讯微博、人人网、豆瓣、Facebook、Twitter、Linkedin 领英、Kakao。
  • 支持登录并获取用户信息的平台:微信、QQ、新浪微博、Facebook、Twitter、Linkedin 领英、Kakao。

  • 授权并获取用户信息

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    // 在需要进行获取登录信息的 UIViewController 中加入如下代码
    #import <UMSocialCore/UMSocialCore.h>

    - (void)getUserInfoForPlatform:(UMSocialPlatformType)platformType {

    [[UMSocialManager defaultManager] getUserInfoWithPlatform:platformType
    currentViewController:self
    completion:^(id result, NSError *error) {

    UMSocialUserInfoResponse *resp = result;

    // 第三方登录数据(为空表示平台未提供)
    // 授权数据
    NSLog(@" uid: %@", resp.uid);
    NSLog(@" openid: %@", resp.openid);
    NSLog(@" accessToken: %@", resp.accessToken);
    NSLog(@" refreshToken: %@", resp.refreshToken);
    NSLog(@" expiration: %@", resp.expiration);

    // 用户数据
    NSLog(@" name: %@", resp.name);
    NSLog(@" iconurl: %@", resp.iconurl);
    NSLog(@" gender: %@", resp.gender);

    // 第三方平台 SDK 原始数据
    NSLog(@" originalResponse: %@", resp.originalResponse);
    }];
    }

3.4.3 第三方平台分享

  • 调用分享面板

    • 在分享按钮绑定如下触发代码

      1
      2
      3
      4
      5
      6
      #import <UShareUI/UShareUI.h>

      // 显示分享面板
      [UMSocialUIManager showShareMenuViewInWindowWithPlatformSelectionBlock:^(UMSocialPlatformType platformType, NSDictionary *userInfo) {
      // 根据获取的 platformType 确定所选平台进行下一步操作
      }];
  • 定制自己的分享面板预定义平台

    • 以下方法可设置平台顺序

      1
      2
      3
      4
      5
      6
      7
      #import <UShareUI/UShareUI.h>

      [UMSocialUIManager setPreDefinePlatforms:@[@(UMSocialPlatformType_Sina),@(UMSocialPlatformType_QQ),@(UMSocialPlatformType_WechatSession)]];

      [UMSocialUIManager showShareMenuViewInWindowWithPlatformSelectionBlock:^(UMSocialPlatformType platformType, NSDictionary *userInfo) {
      // 根据获取的 platformType 确定所选平台进行下一步操作
      }];
      • 为避免应用审核被拒,仅会对有效的平台进行显示,如平台应用未安装,或平台应用不支持等会进行隐藏。由于以上原因,在模拟器上部分平台会隐藏。

      • 如果遇到分享面板未显示,请参考分享面板无法弹出

  • 设置分享内容

    • 分享文本

      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
      - (void)shareTextToPlatformType:(UMSocialPlatformType)platformType {

      // 创建分享消息对象
      UMSocialMessageObject *messageObject = [UMSocialMessageObject messageObject];

      // 设置文本
      messageObject.text = @"社会化组件 UShare 将各大社交平台接入您的应用,快速武装 App。";

      // 调用分享接口
      [[UMSocialManager defaultManager] shareToPlatform:platformType
      messageObject:messageObject
      currentViewController:self
      completion:^(id data, NSError *error) {

      if (error) {
      NSLog(@"************ Share fail with error %@ *********", error);
      }else{
      NSLog(@"response data is %@", data);
      }
      }];
      }

3.5 技术支持

  • 访问:友盟开发者社区

  • 发邮件至 social-support@umeng.com

    • 为了能够尽快响应您的反馈,请提供您的 appkey 及 log 中的详细出错日志,您所提供的内容越详细越有助于我们帮您解决问题。
  • 开启友盟分享调试 log 方法:

    1
    2
    #import <UMSocialCore/UMSocialCore.h>
    [[UMSocialManager defaultManager] openLog:YES];
    • 在 console 中查看日志。
  • 效果

3.6 简单使用

  • AppDelegate.m

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    #import <UMSocialCore/UMSocialCore.h>

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

    // 打开日志
    [[UMSocialManager defaultManager] openLog:YES];

    // 打开图片水印
    //[UMSocialGlobal shareInstance].isUsingWaterMark = YES;

    // 获取友盟 social 版本号
    UMSocialLogInfo(@"UMeng social version: %@", [UMSocialGlobal umSocialSDKVersion]);

    // 设置友盟 appkey
    [[UMSocialManager defaultManager] setUmSocialAppkey:@"57b432afe0f55a9832001a0a"];

    // 设置微信的 appKey 和 appSecret
    [[UMSocialManager defaultManager] setPlaform:UMSocialPlatformType_WechatSession
    appKey:@"wxdc1e388c3822c80b"
    appSecret:@"3baf1193c85774b3fd9d18447d76cab0"
    redirectURL:@"http://mobile.umeng.com/social"];
    /*
    * 添加某一平台会加入平台下所有分享渠道,如微信:好友、朋友圈、收藏,QQ:QQ 和 QQ 空间
    * 以下接口可移除相应平台类型的分享,如微信收藏,对应类型可在枚举中查找
    */
    //[[UMSocialManager defaultManager] removePlatformProviderWithPlatformTypes:@[@(UMSocialPlatformType_WechatFavorite)]];

    // 设置分享到 QQ 互联的 appID
    // U-Share SDK为了兼容大部分平台命名,统一用 appKey 和 appSecret 进行参数设置,
    // 而 QQ 平台仅需将 appID 作为 U-Share 的 appKey 参数传进即可。
    [[UMSocialManager defaultManager] setPlaform:UMSocialPlatformType_QQ
    appKey:@"1105821097" /*设置 QQ 平台的 appID*/
    appSecret:nil
    redirectURL:@"http://mobile.umeng.com/social"];

    // 设置新浪的 appKey 和 appSecret
    [[UMSocialManager defaultManager] setPlaform:UMSocialPlatformType_Sina
    appKey:@"3921700954"
    appSecret:@"04b48b094faeb16683c32669824ebdad"
    redirectURL:@"https://sns.whalecloud.com/sina2/callback"];

    // 钉钉的 appKey
    [[UMSocialManager defaultManager] setPlaform:UMSocialPlatformType_DingDing
    appKey:@"dingoalmlnohc0wggfedpk"
    appSecret:nil
    redirectURL:nil];

    // 支付宝的 appKey
    [[UMSocialManager defaultManager] setPlaform:UMSocialPlatformType_AlipaySession
    appKey:@"2015111700822536"
    appSecret:nil
    redirectURL:@"http://mobile.umeng.com/social"];

    // 设置易信的 appKey
    [[UMSocialManager defaultManager] setPlaform:UMSocialPlatformType_YixinSession
    appKey:@"yx35664bdff4db42c2b7be1e29390c1a06"
    appSecret:nil
    redirectURL:@"http://mobile.umeng.com/social"];

    // 设置点点虫(原来往)的 appKey 和 appSecret
    [[UMSocialManager defaultManager] setPlaform:UMSocialPlatformType_LaiWangSession
    appKey:@"8112117817424282305"
    appSecret:@"9996ed5039e641658de7b83345fee6c9"
    redirectURL:@"http://mobile.umeng.com/social"];

    // 设置领英的 appKey 和 appSecret
    [[UMSocialManager defaultManager] setPlaform:UMSocialPlatformType_Linkedin
    appKey:@"81t5eiem37d2sc"
    appSecret:@"7dgUXPLH8kA8WHMV"
    redirectURL:@"https://api.linkedin.com/v1/people"];

    // 设置 Facebook 的 appKey 和 UrlString
    [[UMSocialManager defaultManager] setPlaform:UMSocialPlatformType_Facebook
    appKey:@"506027402887373"
    appSecret:nil
    redirectURL:@"http://www.umeng.com/social"];

    // 设置 Pinterest 的 appKey
    [[UMSocialManager defaultManager] setPlaform:UMSocialPlatformType_Pinterest
    appKey:@"4864546872699668063"
    appSecret:nil
    redirectURL:nil];

    // dropbox 的 appKey
    [[UMSocialManager defaultManager] setPlaform: UMSocialPlatformType_DropBox
    appKey:@"k4pn9gdwygpy4av"
    appSecret:@"td28zkbyb9p49xu"
    redirectURL:@"https://mobile.umeng.com/social"];

    // vk 的 appkey
    [[UMSocialManager defaultManager] setPlaform:UMSocialPlatformType_VKontakte
    appKey:@"5786123"
    appSecret:nil
    redirectURL:nil];

    return YES;
    }


    //#define __IPHONE_10_0 100000
    #if __IPHONE_OS_VERSION_MAX_ALLOWED > 100000
    - (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<UIApplicationOpenURLOptionsKey, id> *)options {

    BOOL result = [[UMSocialManager defaultManager] handleOpenURL:url];
    if (!result) {
    // 其他如支付等SDK的回调
    }
    return result;
    }

    #endif

    - (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation {

    BOOL result = [[UMSocialManager defaultManager] handleOpenURL:url];
    if (!result) {
    // 其他如支付等SDK的回调
    }
    return result;
    }

    - (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url {

    BOOL result = [[UMSocialManager defaultManager] handleOpenURL:url];
    if (!result) {
    // 其他如支付等SDK的回调
    }
    return result;
    }
  • ViewController.m

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    #import <UMSocialCore/UMSocialCore.h>
    #import <UShareUI/UShareUI.h>

    #pragma mark - 第三方平台登录

    - (IBAction)loginButtonClick:(UIButton *)sender {

    [self getUserInfoForPlatform:UMSocialPlatformType_WechatSession];
    }

    - (void)getUserInfoForPlatform:(UMSocialPlatformType)platformType {

    [[UMSocialManager defaultManager] getUserInfoWithPlatform:platformType
    currentViewController:self
    completion:^(id result, NSError *error) {

    UMSocialUserInfoResponse *resp = result;

    // 第三方登录数据(为空表示平台未提供)
    // 授权数据
    NSLog(@" uid: %@", resp.uid);
    NSLog(@" openid: %@", resp.openid);
    NSLog(@" accessToken: %@", resp.accessToken);
    NSLog(@" refreshToken: %@", resp.refreshToken);
    NSLog(@" expiration: %@", resp.expiration);

    // 用户数据
    NSLog(@" name: %@", resp.name);
    NSLog(@" iconurl: %@", resp.iconurl);
    NSLog(@" gender: %@", resp.gender);

    // 第三方平台 SDK 原始数据
    NSLog(@" originalResponse: %@", resp.originalResponse);
    }];
    }

    #pragma mark - 第三方平台分享

    - (IBAction)shareButtonClick:(UIButton *)sender {

    [self showShareMenuView];
    }

    - (void)showShareMenuView {

    // 设置平台顺序,只显示设置列表中的应用
    [UMSocialUIManager setPreDefinePlatforms:@[@(UMSocialPlatformType_Sina),
    @(UMSocialPlatformType_QQ),
    @(UMSocialPlatformType_WechatSession),
    @(UMSocialPlatformType_AlipaySession)]];


    // 设置分享面板位置,底部 默认
    [UMSocialShareUIConfig shareInstance].sharePageGroupViewConfig.sharePageGroupViewPostionType =
    UMSocialSharePageGroupViewPositionType_Bottom;

    // 设置分享按钮背景形状,有图片 没有圆背景
    [UMSocialShareUIConfig shareInstance].sharePageScrollViewConfig.shareScrollViewPageItemStyleType =
    UMSocialPlatformItemViewBackgroudType_None;

    // 添加自定义分享按钮
    [UMSocialUIManager addCustomPlatformWithoutFilted:UMSocialPlatformType_UserDefine_Begin+2
    withPlatformIcon:[UIImage imageNamed:@"icon_circle"]
    withPlatformName:@"演示 icon"];

    // 显示分享面板
    [UMSocialUIManager showShareMenuViewInWindowWithPlatformSelectionBlock:^(UMSocialPlatformType platformType,
    NSDictionary *userInfo) {

    // 根据获取的 platformType 确定所选平台进行下一步操作

    [self shareTextToPlatformType:platformType];

    // 在回调里面获得点击的
    if (platformType == UMSocialPlatformType_UserDefine_Begin+2) {

    NSLog(@"点击演示添加 Icon 后该做的操作");
    dispatch_async(dispatch_get_main_queue(), ^{

    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"添加自定义 icon"
    message:@"具体操作方法请参考 UShareUI 内接口文档"
    delegate:nil
    cancelButtonTitle:NSLocalizedString(@"确定", nil)
    otherButtonTitles:nil];
    [alert show];
    });
    } else {

    }
    }];
    }

    // 设置分享内容

    - (void)shareTextToPlatformType:(UMSocialPlatformType)platformType {

    // 创建分享消息对象
    UMSocialMessageObject *messageObject = [UMSocialMessageObject messageObject];

    // 设置文本
    messageObject.text = @"社会化组件 UShare 将各大社交平台接入您的应用,快速武装 App。";

    // 调用分享接口,进行分享
    [[UMSocialManager defaultManager] shareToPlatform:platformType
    messageObject:messageObject
    currentViewController:self
    completion:^(id data, NSError *error) {

    if (error) {
    NSLog(@"************ Share fail with error %@ *********",error);
    }else{
    NSLog(@"response data is %@",data);
    }
    }];
    }
文章目录
  1. 1. 1、系统方式创建分享
  2. 2. 2、系统方式自定义分享
  3. 3. 3、友盟 登录/分享 集成
    1. 3.1. 3.1 第三方账号申请及绑定
      1. 3.1.1. 3.1.1 申请第三方账号
      2. 3.1.2. 3.1.2 绑定第三方账号到友盟后台
    2. 3.2. 3.2 U-Share SDK 集成
      1. 3.2.1. 3.2.1 下载 U-Share SDK
      2. 3.2.2. 3.2.2 加入 U-Share SDK
    3. 3.3. 3.3 U-Share SDK 平台配置
      1. 3.3.1. 3.3.1 配置各平台 URL Scheme
      2. 3.3.2. 3.3.2 适配 iOS 9/10 系统
    4. 3.4. 3.4 调用 U-Share SDK
      1. 3.4.1. 3.4.1 初始化设置
      2. 3.4.2. 3.4.2 第三方平台登录
      3. 3.4.3. 3.4.3 第三方平台分享
    5. 3.5. 3.5 技术支持
    6. 3.6. 3.6 简单使用
隐藏目录