友盟分享、統計、推送的使用
統計基於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
如果分享還需要其他平臺的,請參考 ofollow,noindex">https://developer.umeng.com/docs/66632/detail/67204#h2--sdk5
一、友盟分享
匯入上面的依賴庫後需要做以下幾件事情
1.1.配置白名單

image.png
<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

image.png
微博去這裡申請
這些弄完了編譯一遍,不報錯,就可以寫下面的程式碼了。
為了避免Appdelegate程式碼過多,建立一個工具類,用於初始化友盟、配置一些引數。

image.png
.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中只需要做很少的操作來初始化友盟了

image.png
初始化完了之後,需要在appdelegate中,對於分享回撥需要做以下的幾件事

image.png
然後就可以在我們需要做分享操作的介面,處理業務邏輯了。

image.png

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

image.png
二、友盟統計
友盟統計,一般專案只做最簡單的統計,註冊使用者、活躍使用者、崩潰日誌收集這些基本就差不多了,如果需要收集使用者每個頁面點選次數,需要定製化收集這些資訊,專案中的控制器最好都整合同一個baseViewController這樣方便操作,切記。

image.png

image.png
三、友盟推送
推送的方式比較多,推送所有裝置、推送指定裝置、推送指定使用者等,這就需要根據業務需求來分門別類的處理。
下面說一下通用的程式碼,不管哪種推送業務,都需要做的。
首先去 https://mobile.umeng.com/apps/new 這個地址,註冊你的應用,然後去蘋果開發者中心網站,將你專案的生產證書、釋出證書的p12檔案,放進去,之後開啟你專案的推送訊息接收開關

image.png
完成了上面重要的步驟之後,就可以寫下面的這些程式碼了。

image.png
然後在appdelegate中做訊息處理

image.png
如果以上內容對你有幫助,請不要吝嗇你的star。如有不對的地方,還請指出來。
Demo集合