統計基於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集合