iOS友盟做第三方登录和分享注意事项

本人使用友盟SDK的真实经历,全,比官方文档详细

1.下载友盟SDK及开发文档

2. 去开放中心注册微信/QQ/微博的AppKey,注意,必须添加测试的QQ号,才能在测试,否则QQ返回登录授权失败

3. http传输安全设置

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

<key>NSAppTransportSecuritykey>
<dict>
    <key>NSExceptionDomainskey>
    <dict>
        <key>log.umsns.comkey>
        <dict>
            <key>NSIncludesSubdomainskey>
            <true/>
            <key>NSTemporaryExceptionAllowsInsecureHTTPLoadskey>
            <true/>
            <key>NSTemporaryExceptionMinimumTLSVersionkey>
            <string>TLSv1.1string>
        dict>
        <key>sns.whalecloud.comkey>
        <dict>
            <key>NSIncludesSubdomainskey>
            <true/>
            <key>NSTemporaryExceptionAllowsInsecureHTTPLoadskey>
            <true/>
            <key>NSTemporaryExceptionMinimumTLSVersionkey>
            <string>TLSv1.1string>
        dict>

        
        <key>sina.cnkey>
        <dict>
            <key>NSIncludesSubdomainskey>
            <true/>
            <key>NSThirdPartyExceptionRequiresForwardSecrecykey>
            <false/>
        dict>
        <key>weibo.cnkey>
        <dict>
            <key>NSIncludesSubdomainskey>
            <true/>
            <key>NSThirdPartyExceptionRequiresForwardSecrecykey>
            <false/>
        dict>
        <key>weibo.comkey>
        <dict>
            <key>NSIncludesSubdomainskey>
            <true/>
            <key>NSThirdPartyExceptionAllowsInsecureHTTPLoadskey>
            <true/>
            <key>NSThirdPartyExceptionRequiresForwardSecrecykey>
            <false/>
        dict>
        <key>sinaimg.cnkey>
        <dict>
            <key>NSIncludesSubdomainskey>
            <true/>
            <key>NSThirdPartyExceptionAllowsInsecureHTTPLoadskey>
            <true/>
            <key>NSThirdPartyExceptionRequiresForwardSecrecykey>
            <false/>
        dict>
        <key>sinajs.cnkey>
        <dict>
            <key>NSIncludesSubdomainskey>
            <true/>
            <key>NSThirdPartyExceptionAllowsInsecureHTTPLoadskey>
            <true/>
            <key>NSThirdPartyExceptionRequiresForwardSecrecykey>
            <false/>
        dict>
        <key>sina.com.cnkey>
        <dict>
            <key>NSIncludesSubdomainskey>
            <true/>
            <key>NSThirdPartyExceptionAllowsInsecureHTTPLoadskey>
            <true/>
            <key>NSThirdPartyExceptionRequiresForwardSecrecykey>
            <false/>
        dict>
        

        
        <key>qq.comkey>
        <dict>
            <key>NSIncludesSubdomainskey>
            <true/>
            <key>NSThirdPartyExceptionAllowsInsecureHTTPLoadskey>
            <true/>
            <key>NSThirdPartyExceptionRequiresForwardSecrecykey>
            <false/>
        dict>
        

        
        <key>renren.comkey>
        <dict>
            <key>NSIncludesSubdomainskey>
            <true/>
            <key>NSThirdPartyExceptionAllowsInsecureHTTPLoadskey>
            <true/>
            <key>NSThirdPartyExceptionRequiresForwardSecrecykey>
            <false/>
        dict>
        

        
        <key>facebook.comkey>
        <dict>
            <key>NSIncludesSubdomainskey>
            <true/>                
            <key>NSExceptionRequiresForwardSecrecykey>
            <false/>
        dict>
        <key>fbcdn.netkey>
        <dict>
            <key>NSIncludesSubdomainskey>
            <true/>
            <key>NSExceptionRequiresForwardSecrecykey>
            <false/>
        dict>
        <key>akamaihd.netkey>
        <dict>
            <key>NSIncludesSubdomainskey>
            <true/>
            <key>NSExceptionRequiresForwardSecrecykey>
            <false/>
        dict>
        

        
        <key>twitter.comkey>
        <dict>
            <key>NSIncludesSubdomainskey>
            <true/>                
            <key>NSExceptionRequiresForwardSecrecykey>
            <false/>
        dict>
        
    dict>
dict>

B、在info.plist的NSAppTransportSecurity下新增NSAllowsArbitraryLoads并设置为YES,指定所有HTTP连接都可正常请求 http://dev.umeng.com/social/ios/ios9

<key>NSAppTransportSecuritykey>
<dict>
    <key>NSAllowsArbitraryLoadskey>
    <true/>
dict>
4. 应用跳转

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

<key>LSApplicationQueriesSchemeskey>
<array>
    
    <string>wechatstring>
    <string>weixinstring>

    
    <string>sinaweibohdstring>
    <string>sinaweibostring>
    <string>sinaweibossostring>
    <string>weibosdkstring>
    <string>weibosdk2.5string>

    
    <string>mqqapistring>
    <string>mqqstring>
    <string>mqqOpensdkSSoLoginstring>
    <string>mqqconnectstring>
    <string>mqqopensdkdatalinestring>
    <string>mqqopensdkgrouptribesharestring>
    <string>mqqopensdkfriendstring>
    <string>mqqopensdkapistring>
    <string>mqqopensdkapiV2string>
    <string>mqqopensdkapiV3string>
    <string>mqzoneopensdkstring>
    <string>wtloginmqqstring>
    <string>wtloginmqq2string>
    <string>mqqwpastring>
    <string>mqzonestring>
    <string>mqzonev2string>
    <string>mqzonesharestring>
    <string>wtloginqzonestring>
    <string>mqzonewxstring>
    <string>mqzoneopensdkapiV2string>
    <string>mqzoneopensdkapi19string>
    <string>mqzoneopensdkapistring>
    <string>mqqbrowserstring>
    <string>mttbrowserstring>

    
    <string>renreniosstring>
    <string>renrenapistring>
    <string>renrenstring>
    <string>renreniphonestring>

    
    <string>laiwangssostring>

    
    <string>yixinstring>
    <string>yixinopenapistring>

    
    <string>instagramstring>

    
    <string>whatsappstring>

    
    <string>linestring>

    
    <string>fbapistring>
    <string>fb-messenger-apistring>
    <string>fbauth2string>
    <string>fbshareextensionstring>
array>

如何设置 URL schemes

在你的工程设置项,targets 一栏下,选中自己的 target,在 Info->URL Types 中添加 URL Schemes。如果使用的是Xcode3或更低版本,则需要在plist文件中添加。获取各个平台appkey或者appid的方法可以参考分享详细文档绑定各个平台部分,各个平台的url scheme格式如下表:

平台  url scheme设置格式
新浪微博 “wb”+新浪appkey,例如“wb126663232”
微信 微信应用appId,例如“wxd9a39c7122aa6516”,微信详细集成步骤参考微信集成方法
人人网 “rm”+你的应用bundle id,例如“rmcom.umeng.SocialSDK”
QQ、QQ空间 需要添加两个URL schemes 1. “QQ”+腾讯QQ互联应用appId转换成十六进制(不足8位前面补0),例如“QQ05FC5B14”。生成十六进制方法:点击链接。2.“tencent“+腾讯QQ互联应用Id,例如“tencent100424468" ,QQ及Qzone详细集成步骤参考手机QQ集成方法
来往 Identifier填“Laiwang”,URL Schemes填来往AppId.注意使用来往SDK后,Xcode工程other linker flags需要添加-ObjC参数
易信 易信Appkey,例如“yx35664bdff4db42c2b7be1e29390c1a06”
Facebook 集成最新的Facebook SDK在iOS7.0以上有效,若要使用我们提供的facebook分享需要设置“fb”+facebook AppID,例如“fb1440390216179601”,详细集成方法见集成facebook


5. 设置登录点击事件 App delegate里面设置AppKey等不再赘述,参考友盟接口文档
在登录页面三方登录点击事件

#import "UMSocial.h"



-(void)loginWinxin:(UIButton *)sender{

    

    UMSocialSnsPlatform *snsPlatform = [UMSocialSnsPlatformManager getSocialPlatformWithName:UMShareToWechatSession];

    

    snsPlatform.loginClickHandler(self,[UMSocialControllerService defaultControllerService],YES,^(UMSocialResponseEntity *response){

        

        if (response.responseCode == UMSResponseCodeSuccess) {

            

            UMSocialAccountEntity *snsAccount = [[UMSocialAccountManager socialAccountDictionary]valueForKey:UMShareToWechatSession];


            NSLog(@"username is %@, uid is %@,  token is %@ url is %@, unionId is %@, openId is %@",snsAccount.userName,snsAccount.usid,snsAccount.accessToken,snsAccount.iconURL,snsAccount.unionId,snsAccount.openId);


//            MZPhoneNumViewController *phoneVC = [MZPhoneNumViewController new];

//            [self.navigationController pushViewController:phoneVC animated:YES];

            //获取到用户信息之后调后台接口进行登录的方法(自己写的,参考)

            [self loginWithType:@"1" Username:snsAccount.userName openId:snsAccount.openId iconUrl:snsAccount.iconURL];

        }

        

    });

    

}

//实现回调方法(可选):

-(void)didFinishGetUMSocialDataInViewController:(UMSocialResponseEntity *)response

{

    //根据`responseCode`得到发送结果,如果分享成功

    if(response.responseCode == UMSResponseCodeSuccess)

    {

        //得到分享到的微博平台名

        NSLog(@"share to sns name is %@",[[response.data allKeys] objectAtIndex:0]);

    }

}



6. 设置分享

#import "UMSocial.h"


遵守协议

UMSocialUIDelegate

在分享的点击事件里写

    MZProductModel *model = _productArr[sender.tag-2700];

    [UMSocialConfig setSupportedInterfaceOrientations:UIInterfaceOrientationMaskLandscape];

    [UMSocialSnsService presentSnsIconSheetView:self

                                         appKey:@"56a5e460e0f55a1668001535"

                                      shareText:[NSString stringWithFormat:@"%@",model.name]

                                     shareImage:[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:model.image]]]

                                 shareToSnsNames:[NSArray arrayWithObjects:UMShareToSina,UMShareToWechatSession,UMShareToWechatTimeline,UMShareToWechatFavorite,UMShareToQQ,nil]

                                       delegate:self];

//设置分享内容的URL

    [UMSocialData defaultData].extConfig.qqData.url = [NSString stringWithFormat:@"%@",model.shareUrl];

    [UMSocialData defaultData].extConfig.qzoneData.url = [NSString stringWithFormat:@"%@%@",kLocalhost,model.shareUrl];


    //设置预览的标题
[UMSocialData defaultData].extConfig.qqData.title = [NSString stringWithFormat:@"作品-%@",model.name];
    [UMSocialData defaultData].extConfig.qzoneData.title = [NSString stringWithFormat:@"作品-%@",model.name];
    [UMSocialData defaultData].extConfig.wechatSessionData.title = [NSString stringWithFormat:@"作品-%@",model.name];
    [UMSocialData defaultData].extConfig.wechatTimelineData.title = [NSString stringWithFormat:@"作品-%@",model.name];
    [UMSocialData defaultData].extConfig.sinaData.shareText = [NSString stringWithFormat:@"作品-%@ %@",_proModel.name,_proModel.shareUrl];

    //设置预览的内容
    if (model.introduction.length==0) {
        [UMSocialData defaultData].extConfig.qqData.shareText = @"该作品暂无简介";
        [UMSocialData defaultData].extConfig.qzoneData.shareText = @"该作品暂无简介";
        [UMSocialData defaultData].extConfig.wechatSessionData.shareText = @"该作品暂无简介";
        [UMSocialData defaultData].extConfig.wechatTimelineData.shareText = @"该作品暂无简介";
        
    }else{
        
        [UMSocialData defaultData].extConfig.qqData.shareText = model.introduction;
        [UMSocialData defaultData].extConfig.qzoneData.shareText = model.introduction;
        [UMSocialData defaultData].extConfig.wechatSessionData.shareText = model.introduction;
        [UMSocialData defaultData].extConfig.wechatTimelineData.shareText = model.introduction;
    }

注:有一点需要注意,如果在一个类里分别设置了title,在别的类里也一定要设置,因为不设置友盟会取上次设置的title
例如:[UMSocialData defaultData].extConfig.qqData.title = [NSString stringWithFormat:@"活动%@",_titleName];
设置了之后每个类都要设置,而且有图片的时候字数不能过多,否则分享到微信会失败,图片大小也应该保持在500KB以内,血与泪的教训,友盟最坑




你可能感兴趣的:(Objective-C)