代码改变世界

iOS快速集成友盟社会化分享功能(v6.1.1)

2016-12-20 11:48  暴暴  阅读(593)  评论(0编辑  收藏  举报

1.  U-Share SDK集成

1.1 下载U-Share SDK

通过iOS社会化组件选择所需的社交平台后进行下载,下载链接http://dev.umeng.com/social/ios/sdk-download?spm=0.0.0.0.1up3mE

SDK目录介绍

1. Document     - U-Share SDK文档
2. UMSocialDemo - U-Share SDK Demo(如点选下载)
3. UMSocial     - U-Share SDK核心目录
    UMSocialSDK       - U-Share SDK核心framework
    UMSocialUI        - U-Share 分享UI资源、分享面板framework
    SocialLibraries   - 所选择下载的第三方平台SDK及U-Share链接库

1.2  加入U-Share SDK

 将U-Share SDK添加到工程

添加项目配置

在Other Linker Flags加入-ObjC

加入依赖系统库

选中项目Target -> Linked Frameworks and Libraries列表中添加依赖库

SystemConfiguration.framework 

CoreTelephony.framework 

ImageIO.framework 

libsqlite3.tbd 

libc++.tbd 

libz.tbd

2.  U-Share SDK平台配置

从这一步骤就开始需要第三方appKey和appSecret等信息,可参考链接http://dev.umeng.com/social/ios/operation申请所需的平台账号。

2.1  配置各平台URL Scheme

2.1.1  添加URL Types

  • URL Scheme是通过系统找到并跳转对应app的一类设置,通过向项目中的info.plist文件中加入URL types可使用第三方平台所注册的appkey信息向系统注册你的app,当跳转到第三方应用授权或分享后,可直接跳转回你的app。
  • 添加URL Types有如下几处,都可进行设置
  • 通过工程设置面板
  • 通过info.plist文件编辑
  • 直接编辑info.plist中XML代码

2.1.2  配置第三方平台URL Scheme

未列出则不需设置

  格式 举例 备注
微信 微信appKey wxdc1e388c3822c80b  
QQ/Qzone 以下两项都需添加:
1、"tencent"+腾讯QQ互联应用appID 
2、“QQ”+腾讯QQ互联应用appID转换成十六进制(不足8位前面补0)
appID:100424468
1、tencent100424468 
2、QQ05fc5b14
100424468转十六进制为5fc5b14,因不足8位向前补0,结果为05fc5b14,加"QQ"前缀QQ05fc5b14
新浪微博 “wb”+新浪appKey wb3921700954  
钉钉 钉钉appkey dingoalmlnohc0wggfedpk URL Type中的identifier填"dingtalk"
支付宝 “ap”+appID ap2015111700822536 URL Type中的identifier填"alipayShare"
易信 易信appkey yx35664bdff4db42c2b7be1e29390c1a06  
点点虫 点点虫appID 8112117817424282305 URL Type中的identifier填"Laiwang"

2.2  适配iOS9/10系统

iOS9系统后Apple对HTTP请求及访问外部应用做了更加严格的要求,包括HTTP白名单、跳转第三方应用白名单等

2.2.1 HTTPS传输安全

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

在info.plist中加入安全域名白名单(右键info.plist用source code打开)<key>NSAppTransportSecurity</key>

<dict>
   <!-- 配置允许 http的任意网络Begin-->
    <key>NSAllowsArbitraryLoads</key>
    <true/>
    <!-- 配置允许 http的任意网络End-->
   <key>NSExceptionDomains</key>
   <dict>
       <!-- U-Share必须配置的HTTP白名单-->
       <key>log.umsns.com</key>
       <dict>
           <key>NSIncludesSubdomains</key>
           <true/>
           <key>NSTemporaryExceptionAllowsInsecureHTTPLoads</key>
           <true/>
           <key>NSTemporaryExceptionMinimumTLSVersion</key>
           <string>TLSv1.1</string>
       </dict>
       <key>sns.whalecloud.com</key>
       <dict>
           <key>NSIncludesSubdomains</key>
           <true/>
           <key>NSTemporaryExceptionAllowsInsecureHTTPLoads</key>
           <true/>
           <key>NSTemporaryExceptionMinimumTLSVersion</key>
           <string>TLSv1.1</string>
       </dict>
       <!-- U-Share-->
       <!-- 集成微信、QQ、Qzone、腾讯微博授权对应的HTTP白名单-->
       <key>qq.com</key>
       <dict>
           <key>NSIncludesSubdomains</key>
           <true/>
           <key>NSThirdPartyExceptionAllowsInsecureHTTPLoads</key>
           <true/>
           <key>NSThirdPartyExceptionRequiresForwardSecrecy</key>
           <false/>
       </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>
       <!-- 人人授权-->
       <!-- 集成Facebook授权对应的HTTP白名单-->
       <key>facebook.com</key>
       <dict>
           <key>NSExceptionRequiresForwardSecrecy</key>
           <false/>
           <key>NSIncludesSubdomains</key>
           <true/>
       </dict>
       <key>fbcdn.net</key>
       <dict>
           <key>NSExceptionRequiresForwardSecrecy</key>
           <false/>
           <key>NSIncludesSubdomains</key>
           <true/>
       </dict>
       <key>akamaihd.net</key>
       <dict>
           <key>NSIncludesSubdomains</key>
           <true/>
           <key>NSExceptionRequiresForwardSecrecy</key>
           <false/>
       </dict>
       <!-- Facebook授权-->
       <!-- 集成Twitter授权对应的HTTP白名单-->
       <key>twitter.com</key>
       <dict>
           <key>NSExceptionRequiresForwardSecrecy</key>
           <false/>
           <key>NSIncludesSubdomains</key>
           <true/>
       </dict>
       <!-- Twitter授权-->
       <!-- 集成Llinkedin授权对应的HTTP白名单-->
       <key>linkedin.com</key>
       <dict>
           <key>NSExceptionAllowsInsecureHTTPLoads</key>
           <true/>
           <key>NSIncludesSubdomains</key>
           <true/>
           <key>NSExceptionRequiresForwardSecrecy</key>
           <false/>
       </dict>
       <!-- Llinkedin授权-->

       <!-- 集成Kakao授权对应的HTTP白名单-->
        <key>kakao.co.kr</key>
        <dict>
            <key>NSExceptionAllowsInsecureHTTPLoads</key>
            <true/>
            <key>NSExceptionRequiresForwardSecrecy</key>
            <false/>
            <key>NSIncludesSubdomains</key>
            <true/>
        </dict>
        <key>kakao.com</key>
        <dict>
            <key>NSExceptionAllowsInsecureHTTPLoads</key>
            <true/>
            <key>NSExceptionRequiresForwardSecrecy</key>
            <false/>
            <key>NSIncludesSubdomains</key>
            <true/>
        </dict>
        <key>kakaocdn.net</key>
        <dict>
            <key>NSExceptionAllowsInsecureHTTPLoads</key>
            <true/>
            <key>NSExceptionRequiresForwardSecrecy</key>
            <false/>
            <key>NSIncludesSubdomains</key>
            <true/>
        </dict>
        <key>kakaocorp.com</key>
        <dict>
            <key>NSExceptionAllowsInsecureHTTPLoads</key>
            <true/>
            <key>NSExceptionRequiresForwardSecrecy</key>
            <false/>
            <key>NSIncludesSubdomains</key>
            <true/>
        </dict>
       <!-- Kakao授权-->
   </dict>
</dict>


2.2.2应用跳转(SSO等)

 

如果你的应用使用了如SSO授权登录或跳转分享功能,在iOS9/10下就需要增加一个可跳转的白名单,指定对应跳转App的URL Scheme,否则将在第三方平台判断是否跳转时用到的canOpenURL时返回NO,进而只进行webview授权或授权/分享失败。 同样在info.plist增加:

<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.1  初始化设置

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

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

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

    

    [[UMSocialManager defaultManager] openLog:YES];

    //设置友盟appkey

    [[UMSocialManager defaultManager] setUmSocialAppkey:@"57b432afe0f55a9832001a0a"];

//    NSLog(@"UMeng social version: %@", [UMSocialGlobal umSocialSDKVersion]);

    

    //各平台的详细配置

    //设置微信的appIdappKey

    [[UMSocialManager defaultManager] setPlaform:UMSocialPlatformType_WechatSession appKey:@"wxf84eca1790791ae0" appSecret:@"3baf1193c85774b3fd9d18447d76cab0" redirectURL:@"http://mobile.umeng.com/social"];

    

    //设置分享到QQ互联的appIdappKey

    [[UMSocialManager defaultManager] setPlaform:UMSocialPlatformType_QQ appKey:@"100424468"  appSecret:@"100424468" redirectURL:@"http://mobile.umeng.com/social"];

    

    //设置新浪的appIdappKey

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

    

    // 如果不想显示平台下的某些类型,可用以下接口设置

    [[UMSocialManager defaultManager] removePlatformProviderWithPlatformTypes:@[@(UMSocialPlatformType_WechatFavorite),@(UMSocialPlatformType_QQ)]];

    

    

    return YES;

}

3.1.2  设置系统回调

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

{

    BOOL result = [[UMSocialManager defaultManager] handleOpenURL:url];

    if (!result) {

        

    }

    return result;

}

 

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

{

    BOOL result = [[UMSocialManager defaultManager] handleOpenURL:url];

    if (!result) {

        

    }

    return result;

}

3.3  第三方平台分享

3.3.1  弹出分享面板

//点击分享按钮

- (IBAction)share:(id)sender {

    

     [UMSocialUIManager addCustomPlatformWithoutFilted:UMSocialPlatformType_UserDefine_Begin+2

                                     withPlatformIcon:[UIImage imageNamed:@"二维码图标"]

                                     withPlatformName:@"二维码"];

    

       [UMSocialShareUIConfig shareInstance].sharePageGroupViewConfig.sharePageGroupViewPostionType =   UMSocialSharePageGroupViewPositionType_Bottom;

       [UMSocialShareUIConfig shareInstance].sharePageScrollViewConfig.shareScrollViewPageItemStyleType =   UMSocialPlatformItemViewBackgroudType_None;

       [UMSocialUIManager showShareMenuViewInWindowWithPlatformSelectionBlock:^(UMSocialPlatformType   platformType, NSDictionary *userInfo) {

          if (platformType == UMSocialPlatformType_UserDefine_Begin+2) {

              //自定义图标的点击事件

          }

          else{

              [self shareWebPageToPlatformType:platformType];

          }

      }];

}

 

//网页分享

- (void)shareWebPageToPlatformType:(UMSocialPlatformType)platformType

{

    //创建分享消息对象

    UMSocialMessageObject *messageObject = [UMSocialMessageObject messageObject];

    //创建网页内容对象

    NSString* thumbURL = IMGAPI(self.imageStr);

    NSString *url = [NSString stringWithFormat:SHARE(self.msIdStr)];

    UMShareWebpageObject *shareObject = [UMShareWebpageObject shareObjectWithTitle:self.topic descr:url thumImage:thumbURL];

    //设置网页地址

    shareObject.webpageUrl =url;

    //分享消息对象设置分享内容对象

    messageObject.shareObject = shareObject;

    //调用分享接口

    [[UMSocialManager defaultManager] shareToPlatform:platformType messageObject:messageObject currentViewController:self completion:^(id data, NSError *error) {

        if (error) {

            UMSocialLogInfo(@"************Share fail with error %@*********",error);

        }else{

            if ([data isKindOfClass:[UMSocialShareResponse class]]) {

                UMSocialShareResponse *resp = data;

                //分享结果消息

                UMSocialLogInfo(@"response message is %@",resp.message);

                //第三方原始返回的数据

                UMSocialLogInfo(@"response originalResponse data is %@",resp.originalResponse);

            }else{

                UMSocialLogInfo(@"response data is %@",data);

            }

        }

//        [self alertWithError:error];

    }];

}

其他类型请参考http://dev.umeng.com/social/ios/u-share-api%E6%96%87%E6%A1%A3

介绍就到这里了,至于其他的一些分享大家可以好好看看友盟的官方文档很详细的介绍了如何使用自定义的

这个是链接: http://www.umeng.com/codecenter.html