友盟分享

未安装微信客户端

友盟U-Share 从6.0开始全面重构了社会化分享SDK,变化非常大。重构后的分享用起来舒服很多。为了便于维护旧项目同时尝试新东西,我都码在这做对比学习。(前半部分为以前5.0的内容,后半部分为6.0后的内容)

  • 第三帐号申请与绑定
    申请第三方帐号,审核通过后与友盟后台绑定。
    目前新浪微博,QQ空间需要在后台绑定,
    而微信 和 QQ 则直接在代码中设置
友盟分享_第1张图片
新浪微博安全域名:sns.whalecloud.com

友盟分享_第2张图片
新浪微博回调页:http://sns.whalecloud.com/sina2/callback

友盟分享_第3张图片
后台绑定
  • CocoaPods 导入
    pod 'UMengSocialCOM', '~> 5.2.1'

  • 添加SDK依赖的八个系统库文件
    Security.framework
    libiconv.dylib
    SystemConfiguration.framework
    CoreGraphics.Framework
    libsqlite3.dylib
    CoreTelephony.framework
    libstdc++.dylib
    libz.dylib

  1. 配置URL Scheme


    友盟分享_第4张图片
    微信微博URL Scheme

5.0分享

//appDelegate.m 可以写成类目
-(BOOL)application:(UIApplication * )application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    //设置友盟appKey
    [UMSocialData setAppKey:UMENG_APP_KEY];
    //设置新浪微博 #import "UMSocialSinaSSOHandler.h"
    //打开新浪微博的SSO开关,设置新浪微博回调地址,这里必须要和你在新浪微博后台设置的回调地址一致
    [UMSocialSinaSSOHandler openNewSinaSSOWithAppKey:WEIBO_APP_KEY
                                              secret:WEIBO_APP_KEY_SECRET
                                         RedirectURL:@"http://sns.whalecloud.com/sina2/callback"];
   // 微信设置
    [UMSocialWechatHandler setWXAppId:WEIXIN_APP_KEY
                            appSecret:WEIXIN_APP_KEY_SECRET
                                  url:@"http://www.umeng.com/social"];
    // 微信是否安装
    //[WXApi isWXAppInstalled];
    //隐藏微信未安装则不显示,微博是否安装都显示
    //[UMSocialConfig hiddenNotInstallPlatforms:@[UMShareToWechatSession, UMShareToWechatTimeline]];
}
//系统回调方法
//注意如果同时使用微信支付、支付宝等其他需要改写回调代理的SDK,
//请在if分支下做区分,否则会影响 分享、登录的回调
-(BOOL)application:(UIApplication *)application
            openURL:(NSURL *)url
  sourceApplication:(NSString *)sourceApplication
         annotation:(id)annotation
{
    BOOL result = [UMSocialSnsService handleOpenURL:url];
    if (result == FALSE) {
        
        //调用其他SDK,例如支付宝SDK等
    }
    return result;
}
-(void)shareAction:(UIButton*)sender{    

    //点击分享内容后跳转的 URL
    [UMSocialData defaultData].extConfig.wechatSessionData.url = @"http://baidu.com";
    [UMSocialData defaultData].extConfig.wechatTimelineData.url = @"http://baidu.com";
    //分享内容标题
    [UMSocialData defaultData].extConfig.wechatSessionData.title = @"微信好友title";
    [UMSocialData defaultData].extConfig.wechatTimelineData.title = @"微信朋友圈title";
    
    //微博上视频音频分享不成功,可能是因为帐号没通过审核

    //分享图片资源(url和UIImage同时存在时,前者有效)。
    [[UMSocialData defaultData].urlResource setResourceType:UMSocialUrlResourceTypeImage url:@"http://www.baidu.com/img/bdlogo.gif"];
    //分享音乐资源
    //[[UMSocialData defaultData].urlResource setResourceType:UMSocialUrlResourceTypeMusic url:@"http://music.huoxing.com/upload/20130330/1364651263157_1085.mp3"];
    //分享视频资源
    //[[UMSocialData defaultData].urlResource setResourceType:UMSocialUrlResourceTypeVideo url:@"http://v.youku.com/v_show/id_XNjQ1NjczNzEy.html?f=21207816&ev=2"];
    
    [UMSocialSnsService presentSnsIconSheetView:self
                                         appKey: UMENG_APP_KEY
                                      shareText:@"这是篇测试微博http://fanghoo.cn" //分享的文字
                                     shareImage:[UIImage imageNamed:@"0"]       //分享的图片
                                shareToSnsNames:@[UMShareToSina,            //新浪微博
                                                  UMShareToWechatSession,   //微信好友
                                                  UMShareToWechatTimeline,  //微信朋友圈
                                                  UMShareToSms]             //短信
                                       delegate:self];
    }
-(void)didFinishGetUMSocialDataInViewController:(UMSocialResponseEntity *)response
{
    //根据responseCode得到发送结果,如果分享成功
    if(response.responseCode == UMSResponseCodeSuccess)
    {
        //得到分享到的平台名
        NSLog(@"share to sns name is %@",[[response.data allKeys] objectAtIndex:0]);
    }
}

5.0登陆

第三方登陆主要用于简化用户登陆流程,获得第三方授权后(微博是后台绑定,默认授权,但微信授权需认证的开发者),即可获取第三方平台用户资料。
第三方登陆所需要的相关库文件,第三方AppID及相关配与分享完全一致,无需要额外配置

//微博登陆

UMSocialSnsPlatform *snsPlatform = [UMSocialSnsPlatformManager getSocialPlatformWithName:UMShareToSina];

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

        //微博用户名、uid、token等
            if (response.responseCode == UMSResponseCodeSuccess) {
               UMSocialAccountEntity *snsAccount = [[UMSocialAccountManager socialAccountDictionary] valueForKey:snsPlatform.UMShareToSina];
                NSLog(@"\nusername = %@,\n usid = %@,\n token = %@ iconUrl = %@,\n unionId = %@,\n thirdPlatformUserProfile = %@,\n thirdPlatformResponse = %@ \n, message = %@",snsAccount.userName,snsAccount.usid,snsAccount.accessToken,snsAccount.iconURL, snsAccount.unionId, response.thirdPlatformUserProfile, response.thirdPlatformResponse, response.message);

            }});

//微信登陆 

UMSocialSnsPlatform *snsPlatform = [UMSocialSnsPlatformManager getSocialPlatformWithName:UMShareToWechatSession];
snsPlatform.loginClickHandler(self,[UMSocialControllerService defaultControllerService],YES,^(UMSocialResponseEntity *response){

        //获取微信用户名、uid、token等
            if (response.responseCode == UMSResponseCodeSuccess) {

                UMSocialAccountEntity *snsAccount = [[UMSocialAccountManager socialAccountDictionary] valueForKey:snsPlatform.UMShareToWechatSession];
                NSLog(@"\nusername = %@,\n usid = %@,\n token = %@ iconUrl = %@,\n unionId = %@,\n thirdPlatformUserProfile = %@,\n thirdPlatformResponse = %@ \n, message = %@",snsAccount.userName,snsAccount.usid,snsAccount.accessToken,snsAccount.iconURL, snsAccount.unionId, response.thirdPlatformUserProfile, response.thirdPlatformResponse, response.message);

            }});

===========多彩分割线=============

6.0分享

6.0实测最好不要用cocoapods,还是手动集成最好。

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

    //设置友盟 appKey
    [[UMSocialManager defaultManager] setUmSocialAppkey:UMENG_APP_KEY];
    
    //设置微信 appKey 和 appSecret
    [[UMSocialManager defaultManager] setPlaform:UMSocialPlatformType_WechatSession
                                          appKey:WECHATAPP_KEY
                                       appSecret:WECHATAPP_SECRET
                                     redirectURL:nil];
    return YES;
}

//设置系统回调
- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url{
    
    BOOL result = [[UMSocialManager defaultManager] handleOpenURL:url];
    if (!result) {
        
    }
    
    return result;
}
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation{
    
    BOOL result = [[UMSocialManager defaultManager] handleOpenURL:url];
    if (!result) {
        
    }
    return result;
    
}
//分享
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    
    __weak typeof(self) weakSelf = self;
    //显示分享面板
    [UMSocialUIManager showShareMenuViewInView:nil sharePlatformSelectionBlock:^(UMSocialShareSelectionView *shareSelectionView, NSIndexPath *indexPath, UMSocialPlatformType platformType) {
        [UMSocialUIManager dismissShareMenuView];
        [weakSelf shareTextToPlatformType:platformType];
        
    }];

}


- (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);
        }
    }];
*/
    
    //创建分享消息对象
    UMSocialMessageObject *messageObject = [UMSocialMessageObject messageObject];
    //创建分享基本类型 UMShareObject 是所有多媒体类型的基类
    UMShareWebpageObject *shareObject = [UMShareWebpageObject shareObjectWithTitle:@"房狐" descr:@"我这只是一个测试,没嘛意思" thumImage:@"wx"];
    shareObject.webpageUrl = @"www.baidu.com";
    messageObject.shareObject = shareObject;
    
    //调用分享接口
    [[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);
        }
    }];
    
}

6.0登陆

//获取用户信息
- (void)getUserInfoForPlatform:(UMSocialPlatformType)platformType
{
    [[UMSocialManager defaultManager] getUserInfoWithPlatform:platformType currentViewController:nil completion:^(id result, NSError *error) {
        if (error) {
            return ;
        }
        UMSocialUserInfoResponse *userinfo =result;
        NSString *message = [NSString stringWithFormat:@"name: %@\n icon: %@\n gender: %@\n",userinfo.name,userinfo.iconurl,userinfo.gender];
        NSLog(@"%@",message);
    }];
}
//获取授权
-(void)authWithPlatform:(UMSocialPlatformType)platformType
{
    [[UMSocialManager defaultManager]  authWithPlatform:platformType currentViewController:self completion:^(id result, NSError *error) {
     
        UMSocialAuthResponse *authresponse = result;
        NSString *authMessage = [NSString stringWithFormat:@"result: %d\n uid: %@\n accessToken: %@\n",(int)error.code,authresponse.uid,authresponse.accessToken];
        NSLog(@"%@",authMessage);
        
        UMSocialResponse *response = result;
        NSString *responseMessage = [NSString stringWithFormat:@"result:%d\n uid: %@\n openID:%@\n",(int)error.code,response.uid,response.openid];
        NSLog(@"%@",responseMessage);
        
    }];
}
//UMSocialUserInfoResponse 和 UMSoialAuthResponse 都是UMSocialResponse 的子类。

你可能感兴趣的:(友盟分享)