按照官方集成有時(shí)可能會遇到各種BUG耕皮,官方文檔好像有點(diǎn)不全沈跨,你必須去官方的demo里去找相關(guān)的代碼!
下面介紹一下我的集成過程:
分享:
- 在AppDelegate.m里面注冊友盟的key
-(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
[UMSocialData setAppKey:umengAppKey];
}
-
在你分享按鈕的控制器里面注冊分享平臺
因?yàn)槲业氖莿討B(tài)的url库糠,所以需要注冊在分享頁面舷丹,如果是靜態(tài)的url可以直接在AppDelegate里面注冊;
如果用后手機(jī)沒有安裝相關(guān)的客戶端就隱藏相應(yīng)的分享按鈕
[UMSocialConfig hiddenNotInstallPlatforms:@[UMShareToQQ, UMShareToWechatSession, UMShareToWechatTimeline]];
設(shè)置微信分享類型:
/*
typedef enum{
UMSocialWXMessageTypeNone,
UMSocialWXMessageTypeText, //微信消息文本類型
UMSocialWXMessageTypeImage, //微信消息圖片類型
UMSocialWXMessageTypeApp, //微信消息應(yīng)用類型
UMSocialWXMessageTypeWeb, //微信消息網(wǎng)頁類型
UMSocialWXMessageTypeMusic, //微信消息音樂類型
UMSocialWXMessageTypeVideo, //微信消息視頻類型
UMSocialWXMessageTypeEmotion, //微信消息表情類型
UMSocialWXMessageTypeOther //微信消息其他多媒體類型
}UMSocialWXMessageType;
*/
[UMSocialData defaultData].extConfig.wxMessageType = UMSocialWXMessageTypeWeb;
QQ分享類型:
[UMSocialData defaultData].extConfig.qqData.qqMessageType = UMSocialQQMessageTypeDefault;
微博分享類型:
[UMSocialData defaultData].extConfig.sinaData.shareText = [NSString stringWithFormat:@"%@翘魄,%@",bz,socialUrl];
或(分享網(wǎng)頁):
UMSocialUrlResource *urlRes = [[UMSocialUrlResource alloc] initWithSnsResourceType:UMSocialUrlResourceTypeWeb url:socialUrl];
[UMSocialData defaultData].extConfig.sinaData.urlResource = urlRes;
注意:
這種方式也可以設(shè)置博分享類型鼎天,但是它會影響到QQ 分享的類型,導(dǎo)致QQ分享只能分享文字暑竟;這個(gè)問題我問過友盟官方的客服斋射,給我的解答是他們沒有提供這樣的方法設(shè)置分享類型,我也是醉啦但荤,難道是我自己發(fā)明的嘛罗岖?
[[UMSocialData defaultData].urlResource setResourceType:UMSocialUrlResourceTypeWeb url:socialUrl];
分享成功之后:在分享成功之后需要做一下其他操作需要實(shí)現(xiàn)下面方法
#pragma mark - UMSocialUIDelegate
-(void)didFinishGetUMSocialDataInViewController:(UMSocialResponseEntity *)response
{
//根據(jù)`responseCode`得到發(fā)送結(jié)果,如果分享成功
if(response.responseCode == UMSResponseCodeSuccess)
{}
}
-
配置URL scheme
補(bǔ)充:
如果分享的URL里面包含中文就會出現(xiàn)某些分享平臺無法打開的情況,要將URL轉(zhuǎn)碼;
[socialUrl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
不轉(zhuǎn)碼會出現(xiàn)類似如下錯(cuò)誤:
-QQAPI- QQApi.m:252 param error: url is nil
sent to qq result is 5
第三方登錄
-
在AppDelegate里面注冊第三方平臺
-
登錄按鈕
UMSocialSnsPlatform *snsPlatform = [UMSocialSnsPlatformManager getSocialPlatformWithName:UMShareToWechatSession];
snsPlatform.loginClickHandler(self,[UMSocialControllerService defaultControllerService],YES,^(UMSocialResponseEntity *response){
if (response.responseCode == UMSResponseCodeSuccess) {
UMSocialAccountEntity *snsAccount = [[UMSocialAccountManager socialAccountDictionary]valueForKey:UMShareToWechatSession];
[self saveUserInfo:[NSString stringWithFormat:@"wechat_%@",snsAccount.usid] token:@"" headerImageUrl:snsAccount.iconURL name:snsAccount.userName type:@"wechat"];
}
});
- sina
UMSocialSnsPlatform *snsPlatform = [UMSocialSnsPlatformManager getSocialPlatformWithName:UMShareToSina];
snsPlatform.loginClickHandler(self,[UMSocialControllerService defaultControllerService],YES,^(UMSocialResponseEntity *response){
if (response.responseCode == UMSResponseCodeSuccess) {
UMSocialAccountEntity *snsAccount = [[UMSocialAccountManager socialAccountDictionary] valueForKey:UMShareToSina];
[self saveUserInfo:[NSString stringWithFormat:@"sina_%@",snsAccount.usid] token:@"" headerImageUrl:snsAccount.iconURL name:snsAccount.userName type:@"sina"];
}});
- QQ
UMSocialSnsPlatform *snsPlatform = [UMSocialSnsPlatformManager getSocialPlatformWithName:UMShareToQQ];
snsPlatform.loginClickHandler(self,[UMSocialControllerService defaultControllerService],YES,^(UMSocialResponseEntity *response){
if (response.responseCode == UMSResponseCodeSuccess) {
UMSocialAccountEntity *snsAccount = [[UMSocialAccountManager socialAccountDictionary] valueForKey:UMShareToQQ];
[self saveUserInfo:[NSString stringWithFormat:@"qq_%@",snsAccount.usid] token:@"" headerImageUrl:snsAccount.iconURL name:snsAccount.userName type:@"qq"];
}});
官方文檔是只說明到這步腹躁,你發(fā)現(xiàn)的你程序會崩潰桑包;那是因?yàn)樵贏ppDelegate少代碼,這是我通過官方的demo找到的纺非;
/**
這里處理新浪微博SSO授權(quán)之后跳轉(zhuǎn)回來哑了,和微信分享完成之后跳轉(zhuǎn)回來
*/
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
{
return [UMSocialSnsService handleOpenURL:url wxApiDelegate:nil];
}