美文网首页
友盟分享集成只需5步

友盟分享集成只需5步

作者: pengshuangta | 来源:发表于2018-11-28 15:39 被阅读61次
    背景:

    友盟分享的文档太坑了,这里自己总结一下,只需5步。
    1、申请得到微信对应的appid和appSecrect,在友盟官网中创建app,得到友盟的对应的app 的key;
    2、pod导入分享的第三方平台:

     # 友盟基础
      pod 'UMCCommon'
      # 集成微信(精简版0.2M)
      pod 'UMCShare/Social/ReducedWeChat'
    

    3、在info.plist中添加
    (1)对应的第三方app 的白名单;
    (2)设置URL Type,跳转到第三方app需要添加


    image.png

    4、创建一个appdelegate的分类,在appdelegate的didFinishLaunchingWithOptions中注册友盟的key,和对应第三方app的id和secret;

    /**
     *  注册友盟分享
     */
    -(void)registerUmeng{
        [UMConfigure initWithAppkey:UMengShare_KEY channel:@"App Store"];
        // 第三方分享平台
        [self configUSharePlatforms];
    }
    /**
     *  友盟分享第三方appid和appSecret设置
     */
    -(void)configUSharePlatforms{
        /* 设置微信的appKey和appSecret */
        // 微信聊天
        [[UMSocialManager defaultManager] setPlaform:UMSocialPlatformType_WechatSession appKey:WeChatShare_APPID appSecret:WeChatShare_Secret redirectURL:@"http://mobile.umeng.com/social"];
        // 微信朋友圈
        [[UMSocialManager defaultManager] setPlaform:UMSocialPlatformType_WechatTimeLine appKey:WeChatShare_APPID appSecret:WeChatShare_Secret redirectURL:@"http://mobile.umeng.com/social"];
        /*
         * 移除相应平台的分享,如微信收藏
         */
        //[[UMSocialManager defaultManager] removePlatformProviderWithPlatformTypes:@[@(UMSocialPlatformType_WechatFavorite)]];
        /* 设置分享到QQ互联的appID
         * U-Share SDK为了兼容大部分平台命名,统一用appKey和appSecret进行参数设置,而QQ平台仅需将appID作为U-Share的appKey参数传进即可。
         */
        
        /* QQ
        [[UMSocialManager defaultManager] setPlaform:UMSocialPlatformType_QQ appKey:@"“  appSecret:@”“ redirectURL:@"http://mobile.umeng.com/social"];
         */
        
        
        /* 新浪微博
        [[UMSocialManager defaultManager] setPlaform:UMSocialPlatformType_Sina appKey:@""  appSecret:@"" redirectURL:@"https://sns.whalecloud.com/sina2/callback"];
         */
    }
    

    5、点击第三方app的图标,分享网页(注:建议所有的第三方库都自己在封装一层,不仅在多个页面中使用的时候方便,在新的app中也方便代码copy);

    - (void)shareWebPageToPlatformType:(UMSocialPlatformType)platformType
    {
        //创建分享消息对象
        UMSocialMessageObject *messageObject = [UMSocialMessageObject messageObject];
        //创建网页内容对象
        UMShareWebpageObject *shareObject = [UMShareWebpageObject shareObjectWithTitle:@"分享标题" descr:@"分享内容描述" thumImage:[UIImage imageNamed:@"icon"]];
        //设置网页地址
        shareObject.webpageUrl =@"http://mobile.umeng.com/social";
        //分享消息对象设置分享内容对象
        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);
            }
        }];
    }
    

    相关文章

      网友评论

          本文标题:友盟分享集成只需5步

          本文链接:https://www.haomeiwen.com/subject/hjtzqqtx.html