友盟分享、统计、推送的使用

统计基于SDK5.5.2版本
消息推送基于SDK3.2.3版本
分享基于SDK6.9.5版本

利用cocoapods导入相关依赖库

     #######   友盟基础库     #######
    pod 'UMCCommon'
    pod 'UMCSecurityPlugins' #pod ‘UMCSecurityPlugins’为安全组件,不需要开发者显式调用,为开发者提供安全的数据环境,能有效的防止刷量和反作弊等行为,属于可选项,如果对App的数据安全性要求不高的话,可以去掉此pod。


    pod 'UMCCommonLog' #开发阶段进行调试SDK及相关功能使用,可在发布 App 前移除


        #######   下面是分享的平台,用到哪个平台导入哪个平台,避免项目包过大    ######
    # 集成微信(精简版0.2M)
    pod 'UMCShare/Social/ReducedWeChat'
    # 集成QQ/QZone/TIM(精简版0.5M)
    pod 'UMCShare/Social/ReducedQQ'
    # 集成新浪微博(精简版1M)
    pod 'UMCShare/Social/ReducedSina'

    ######    友盟统计       #######
    pod 'UMCAnalytics'#统计 SDK



    #######    友盟推送        #######
    pod 'UMCPush' #Push SDK

如果分享还需要其他平台的,请参考https://developer.umeng.com/docs/66632/detail/67204#h2--sdk5

一、友盟分享

导入上面的依赖库后需要做以下几件事情

1.1.配置白名单
<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>
<string>tim</string>
<string>timapi</string>
<string>timopensdkfriend</string>
<string>timwpa</string>
<string>timgamebindinggroup</string>
<string>timapiwallet</string>
<string>timOpensdkSSoLogin</string>
<string>wtlogintim</string>
<string>timopensdkgrouptribeshare</string>
<string>timopensdkapiV4</string>
<string>timgamebindinggroup</string>
<string>timopensdkdataline</string>
<string>wtlogintimV1</string>
<string>timapiV1</string>

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

<!-- 钉钉 URL Scheme 白名单-->
  <string>dingtalk</string>
  <string>dingtalk-open</string>
  </array>

太多不列了,可参考https://developer.umeng.com/docs/66632/detail/66825

1.2.配置URL Scheme

qq去这里申请
微信去这里申请
微博去这里申请
这些弄完了编译一遍,不报错,就可以写下面的代码了。
为了避免Appdelegate代码过多,创建一个工具类,用于初始化友盟、配置一些参数。

.h文件(之所以在.h文件中导入头文件,是为了appdelegate中再次重复导入这些头文件,也方便直接拖拽至其他项目中使用)

#import <Foundation/Foundation.h>

//需要的头文件在这里导入,appdelegate中就不需要重新导入了
#import <UMCommon/UMCommon.h>
#import <UMShare/UMShare.h>

#import <UMPush/UMessage.h>

#import <UMAnalytics/MobClick.h>

#import <UMCommonLog/UMCommonLogHeaders.h>

#import <UserNotifications/UserNotifications.h>

@interface XBUMManager : NSObject
+(void)initUmManagerWithLaunchOptions:(NSDictionary *)launchOptions Delegate:(id)delegate;
@end

.m文件

    #import "XBUMManager.h"



    @interface XBUMManager ()<UNUserNotificationCenterDelegate>

    @end

    @implementation XBUMManager

    +(void)initUmManagerWithLaunchOptions:(NSDictionary *)launchOptions Delegate:(id)delegate
    {
        
        // Override point for customization after application launch.
        //开发者需要显式的调用此函数,日志系统才能工作
        [UMCommonLogManager setUpUMCommonLogManager];
        [UMConfigure setLogEnabled:YES];//设置为YES, 输出可供调试参考的log信息. 发布产品时必须设置为NO.
        
        [UMConfigure initWithAppkey:@"5b039399f29d98151c000098" channel:@"App Store"];
        
        
        
        
        // Share's setting
        [self setupUSharePlatforms];   // required: setting platforms on demand
        
        
        [MobClick setScenarioType:E_UM_GAME|E_UM_DPLUS];
        [MobClick setCrashReportEnabled:YES];
        
        
        
        
        // Push's basic setting
        UMessageRegisterEntity * entity = [[UMessageRegisterEntity alloc] init];
        //type是对推送的几个参数的选择,可以选择一个或者多个。默认是三个全部打开,即:声音,弹窗,角标
        entity.types = UMessageAuthorizationOptionBadge|UMessageAuthorizationOptionAlert;
        if (@available(iOS 10.0, *)) {
            [UNUserNotificationCenter currentNotificationCenter].delegate=delegate;
        } else {
            // Fallback on earlier versions
        }
        [UMessage registerForRemoteNotificationsWithLaunchOptions:launchOptions Entity:entity completionHandler:^(BOOL granted, NSError * _Nullable error) {
            if (granted) {
            }else
            {
            }
        }];
        
       
        
    }


    + (void)setupUSharePlatforms{
        /*
         设置微信的appKey和appSecret
         [微信平台从U-Share 4/5升级说明]http://dev.umeng.com/social/ios/%E8%BF%9B%E9%98%B6%E6%96%87%E6%A1%A3#1_1
         */
        [[UMSocialManager defaultManager] setPlaform:UMSocialPlatformType_WechatSession appKey:@"wx0688566e181a28b3" appSecret:@"c72a48c823b3a0589cf360bfb10a6fcf" redirectURL:nil];
        /*
         * 移除相应平台的分享,如微信收藏
         */
        //[[UMSocialManager defaultManager] removePlatformProviderWithPlatformTypes:@[@(UMSocialPlatformType_WechatFavorite)]];
        
        /* 设置分享到QQ互联的appID
         * U-Share SDK为了兼容大部分平台命名,统一用appKey和appSecret进行参数设置,而QQ平台仅需将appID作为U-Share的appKey参数传进即可。
         100424468.no permission of union id
         [QQ/QZone平台集成说明]http://dev.umeng.com/social/ios/%E8%BF%9B%E9%98%B6%E6%96%87%E6%A1%A3#1_3
         */
        [[UMSocialManager defaultManager] setPlaform:UMSocialPlatformType_QQ appKey:@"1106906541"/*设置QQ平台的appID*/  appSecret:@"CDHQsmIgZ7Nu6nPj" redirectURL:nil];
        
        /*
         设置新浪的appKey和appSecret
         [新浪微博集成说明]http://dev.umeng.com/social/ios/%E8%BF%9B%E9%98%B6%E6%96%87%E6%A1%A3#1_2
         */
        [[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:nil];
        
        /* 设置易信的appKey */
        [[UMSocialManager defaultManager] setPlaform: UMSocialPlatformType_YixinSession appKey:@"yx35664bdff4db42c2b7be1e29390c1a06" appSecret:nil redirectURL:@"http://mobile.umeng.com/social"];
        
        /* 设置领英的appKey和appSecret */
        [[UMSocialManager defaultManager] setPlaform:UMSocialPlatformType_Linkedin appKey:@"81lcv9le14dpqi"  appSecret:@"Po7OB9LxOaxhR9M3" redirectURL:@"https://api.linkedin.com/v1/people"];
        
        /* 设置Twitter的appKey和appSecret */
        [[UMSocialManager defaultManager] setPlaform:UMSocialPlatformType_Twitter appKey:@"fB5tvRpna1CKK97xZUslbxiet"  appSecret:@"YcbSvseLIwZ4hZg9YmgJPP5uWzd4zr6BpBKGZhf07zzh3oj62K" redirectURL:nil];
        
        /* 设置Facebook的appKey和UrlString */
        [[UMSocialManager defaultManager] setPlaform:UMSocialPlatformType_Facebook appKey:@"506027402887373"  appSecret:nil redirectURL:nil];
        
        /* 设置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];
        
    }

    @end

之后我们在Appdelegate中只需要做很少的操作来初始化友盟了


初始化完了之后,需要在appdelegate中,对于分享回调需要做以下的几件事


然后就可以在我们需要做分享操作的界面,处理业务逻辑了。


到此,分享的功能就完成了。

其他分享样式请详细拜读官方文档

二、友盟统计

友盟统计,一般项目只做最简单的统计,注册用户、活跃用户、崩溃日志收集这些基本就差不多了,如果需要收集用户每个页面点击次数,需要定制化收集这些信息,项目中的控制器最好都集成同一个baseViewController这样方便操作,切记。


三、友盟推送

推送的方式比较多,推送所有设备、推送指定设备、推送指定用户等,这就需要根据业务需求来分门别类的处理。
下面说一下通用的代码,不管哪种推送业务,都需要做的。
首先去https://mobile.umeng.com/apps/new这个地址,注册你的应用,然后去苹果开发者中心网站,将你项目的生产证书、发布证书的p12文件,放进去,之后打开你项目的推送消息接收开关

完成了上面重要的步骤之后,就可以写下面的这些代码了。



然后在appdelegate中做消息处理


如果以上内容对你有帮助,请不要吝啬你的star。如有不对的地方,还请指出来。
Demo集合

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章