最近公司在做第三方登錄珊膜,遇到的坑那叫一個(gè)多啊,所以我一定要寫個(gè)東西記錄下來宣脉,畢竟我在做的時(shí)候车柠,網(wǎng)上關(guān)于友盟最新SDK的資料實(shí)在太少了。
在我嘗試了用QQ塑猖,微信竹祷,微博SDK后最終還是決定用友盟的,因?yàn)橹绊?xiàng)目里集成了分享萌庆,也是用的友盟的SDK溶褪,里面也已經(jīng)有了現(xiàn)成的也不用在集成了〖眨可是QQ的unionId在友盟SDK5.2.1獲取不到怎么都獲取不到猿妈,那個(gè)坎坷啊,最終走向了升級(jí)的道路巍虫。下面進(jìn)入正題彭则。。
首先刪掉原有的SDK占遥,我是用的cocoapods俯抖,在Podfilel里面把原有的SDK刪掉,加上最新的要下載的SDK
至于開始怎么集成的步驟之類的我在這里就不講了瓦胎,認(rèn)真仔細(xì)的看集成文檔芬萍,上面都是有的。
接下來直接上代碼
首先在AppDelegate加上下面的代碼
#import <UMSocialCore/UMSocialCore.h> //友盟頭文件
- (BOOL)application:(UIApplication *)application
didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
/* 設(shè)置友盟appkey */
[[UMSocialManager defaultManager] setUmSocialAppkey:YM_Share_App_Key];
/* 設(shè)置微信的appKey和appSecret */
[[UMSocialManager defaultManager] setPlaform:UMSocialPlatformType_Sina appKey:wbAPPKey appSecret:wbAPPSecret redirectURL:@"http://sns.whalecloud.com/sina2/callback"];
/* 設(shè)置微信的appKey和appSecret */
[[UMSocialManager defaultManager] setPlaform:UMSocialPlatformType_WechatSession appKey:wxAPPID appSecret:wxAPPSecret redirectURL:@"http://mobile.umeng.com/social"];
/* 設(shè)置分享到QQ互聯(lián)的appID
* U-Share SDK為了兼容大部分平臺(tái)命名搔啊,統(tǒng)一用appKey和appSecret進(jìn)行參數(shù)設(shè)置柬祠,而QQ平臺(tái)僅需將appID作為U-Share的appKey參數(shù)傳進(jìn)即可。
*/
[[UMSocialManager defaultManager] setPlaform:UMSocialPlatformType_QQ appKey:QQAPPID/*設(shè)置QQ平臺(tái)的appID*/ appSecret:nil redirectURL:@"http://mobile.umeng.com/social"];
//允許HTTP傳輸负芋,分享圖片
[UMSocialGlobal shareInstance].isUsingHttpsWhenShareContent = NO;
}
接著創(chuàng)建一個(gè)分享面板漫蛔,也可以用友盟自帶的,分享面板這里我已經(jīng)創(chuàng)建好了
直接在按鈕點(diǎn)擊事件里面加上下面的代碼這里是分享的web頁面
// 分享按鈕
[button addActionWithTouchUpInside:^{
switch (i) {
case 0 :
//微信
[self shareWebPageToPlatformType:UMSocialPlatformType_WechatSession];
break;
case 1 :
//朋友圈
[self shareWebPageToPlatformType:UMSocialPlatformType_WechatTimeLine];
break;
case 2 :
//QQ
[self shareWebPageToPlatformType:UMSocialPlatformType_QQ];
break;
case 3 :
//QQ空間
[self shareWebPageToPlatformType:UMSocialPlatformType_Qzone];
break;
case 4 :
//新浪
[self shareWebPageToPlatformType:UMSocialPlatformType_Sina];
break;
default:
break;
}
}];
- (void)shareWebPageToPlatformType:(UMSocialPlatformType)platformType
{
//創(chuàng)建分享消息對(duì)象
UMSocialMessageObject *messageObject = [UMSocialMessageObject messageObject];
//創(chuàng)建網(wǎng)頁內(nèi)容對(duì)象 這里給了圖片
UIImage *thumbURL = self.image;
UMShareWebpageObject *shareObject = [UMShareWebpageObject shareObjectWithTitle:self.artTiltle descr:self.artTiltle thumImage:thumbURL];
//設(shè)置網(wǎng)頁地址 文章的url
shareObject.webpageUrl = self.url;
//分享消息對(duì)象設(shè)置分享內(nèi)容對(duì)象
messageObject.shareObject = shareObject;
//調(diào)用分享接口
[[UMSocialManager defaultManager] shareToPlatform:platformType messageObject:messageObject currentViewController:self completion:^(id data, NSError *error) {
if (error) {
UMSocialLogInfo(@"************Share fail with error %@*********",error);
}else{
if ([data isKindOfClass:[UMSocialShareResponse class]]) {
UMSocialShareResponse *resp = data;
//分享結(jié)果消息
UMSocialLogInfo(@"response message is %@",resp.message);
//第三方原始返回的數(shù)據(jù)
UMSocialLogInfo(@"response originalResponse data is %@",resp.originalResponse);
}else{
UMSocialLogInfo(@"response data is %@",data);
}
}
[self alertWithError:error]; //分享調(diào)試錯(cuò)誤彈框
}];
}
pragma mark -- 分享調(diào)試信息打印
- (void)alertWithError:(NSError *)error
{
NSString *result = nil;
if (!error) {
result = [NSString stringWithFormat:@"分享成功"];
}
else{
NSMutableString *str = [NSMutableString string];
if (error.userInfo) {
for (NSString *key in error.userInfo) {
[str appendFormat:@"%@ = %@\n", key, error.userInfo[key]];
}
}
if (error) {
result = [NSString stringWithFormat:@"Share fail with error code: %d\n%@",(int)error.code, str];
}
else{
result = [NSString stringWithFormat:@"Share fail"];
}
}
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"share"
message:result
delegate:nil
cancelButtonTitle:NSLocalizedString(@"sure", @"確定")
otherButtonTitles:nil];
[alert show];
}
接下來講分享圖片的旧蛾,iOS里面已經(jīng)都必須用https了氮凝,如果報(bào)下面的這個(gè)錯(cuò)誤
需要在這樣操作
/*
* 關(guān)閉強(qiáng)制驗(yàn)證https矾缓,可允許http圖片分享何荚,但需要在info.plist設(shè)置安全域名
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
</dict>
*/
光上面的配置了還不可以缔恳,這句代碼一定一定要在AppDelegate加上,否則這個(gè)錯(cuò)誤是不會(huì)消失的
[UMSocialGlobal shareInstance].isUsingHttpsWhenShareContent = NO;
加上這句代碼就可以分享網(wǎng)絡(luò)圖片了病袄,下面附上分享網(wǎng)絡(luò)圖片的代碼
//分享圖片
- (void)shareImageToPlatformType:(UMSocialPlatformType)platformType
{
//創(chuàng)建分享消息對(duì)象
UMSocialMessageObject *messageObject = [UMSocialMessageObject messageObject];
//創(chuàng)建圖片內(nèi)容對(duì)象
UMShareImageObject *shareObject = [[UMShareImageObject alloc] init];
//如果有縮略圖奶镶,則設(shè)置縮略圖
shareObject.thumbImage = self.imageURL;//這里是圖片的鏈接
[shareObject setShareImage:self.imageURL];
messageObject.shareObject = shareObject;
BLLog(@"ImageURL = %@",self.imageURL);
//調(diào)用分享接口
[[UMSocialManager defaultManager] shareToPlatform:platformType messageObject:messageObject currentViewController:self completion:^(id data, NSError *error) {
if (error) {
UMSocialLogInfo(@"************Share fail with error %@*********",error);
NSLog(@"1 ---");
}else{
if ([data isKindOfClass:[UMSocialShareResponse class]]) {
UMSocialShareResponse *resp = data;
//分享結(jié)果消息
UMSocialLogInfo(@"response message is %@",resp.message);
//第三方原始返回的數(shù)據(jù)
UMSocialLogInfo(@"response originalResponse data is %@",resp.originalResponse);
NSLog(@"2 ---");
}else{
UMSocialLogInfo(@"response data is %@",data);
NSLog(@"; ---");
}
}
[self alertWithError:error];
}];
}
分享圖片的時(shí)候有可能會(huì)遇到微信迟赃,QQ,新浪都可以分享厂镇,但是唯獨(dú)QQ空間分享不成功纤壁,報(bào)下面的這個(gè)錯(cuò)誤
這個(gè)時(shí)候你需要在info.plist文件里面加入下面的這句話就可以了
<string>mqqopensdkapiV4</string>
分享在這里就說完了。
接下來我們說一說登錄捺信,登錄的也要在AppDelegate注冊(cè)酌媒,但是只需要注冊(cè)一遍就可以了,你分享的時(shí)候注冊(cè)了迄靠,登錄就不用了秒咨,它們是一起的。在登錄界面創(chuàng)建三個(gè)登錄按鈕掌挚,微信雨席,QQ,微博吠式。三個(gè)的按鈕點(diǎn)擊事件里面寫上下面的代碼陡厘。別忘記導(dǎo)入頭文件。
pragma mark--QQ登錄
-(void)qqButtonClick:(UIButton *)button{
BLLog(@"QQ");
[[UMSocialManager defaultManager] getUserInfoWithPlatform:UMSocialPlatformType_QQ currentViewController:self completion:^(id result, NSError *error) {
if (error) {
//授權(quán)失敗
} else {
UMSocialUserInfoResponse *resp = result;
// 第三方登錄數(shù)據(jù)(為空表示平臺(tái)未提供)
// 授權(quán)數(shù)據(jù)
NSLog(@" uid: %@", resp.uid);
NSLog(@" openid: %@", resp.openid);
NSLog(@" accessToken: %@", resp.accessToken);
NSLog(@" unionId: %@", resp.unionId);
// 用戶數(shù)據(jù)
NSLog(@" name: %@", resp.name);
NSLog(@" iconurl: %@", resp.iconurl);
NSLog(@" gender: %@", resp.unionGender);
// 第三方平臺(tái)SDK原始數(shù)據(jù)
NSLog(@" originalResponse: %@", resp.originalResponse);
}];
}
pragma mark -- 微信登錄
-(void)WxButtonClick:(UIButton *)button{
[[UMSocialManager defaultManager] getUserInfoWithPlatform:UMSocialPlatformType_WechatSession currentViewController:nil completion:^(id result, NSError *error) {
if (error) {
//授權(quán)失敗
} else {
UMSocialUserInfoResponse *resp = result;
// 授權(quán)信息
NSLog(@"Wechat uid: %@", resp.uid);
NSLog(@"Wechat openid: %@", resp.openid);
NSLog(@"Wechat accessToken: %@", resp.accessToken);
NSLog(@"Wechat refreshToken: %@", resp.refreshToken);
NSLog(@"Wechat expiration: %@", resp.expiration);
// 用戶信息
NSLog(@"Wechat name: %@", resp.name);
NSLog(@"Wechat iconurl: %@", resp.iconurl);
NSLog(@"Wechat gender: %@", resp.unionGender);
// 第三方平臺(tái)SDK源數(shù)據(jù)
NSLog(@"Wechat originalResponse: %@", resp.originalResponse);
}
}];
pragma mark--微博登錄
-(void)SinaButtonClick:(UIButton *)button{
[[UMSocialManager defaultManager] getUserInfoWithPlatform:UMSocialPlatformType_Sina currentViewController:nil completion:^(id result, NSError *error) {
if (error) {
//授權(quán)失敗
} else {
UMSocialUserInfoResponse *resp = result;
// 授權(quán)信息
NSLog(@"Sina uid: %@", resp.uid);
NSLog(@"Sina accessToken: %@", resp.accessToken);
NSLog(@"Sina refreshToken: %@", resp.refreshToken);
NSLog(@"Sina expiration: %@", resp.expiration);
// 用戶信息
NSLog(@"Sina name: %@", resp.name);
NSLog(@"Sina iconurl: %@", resp.iconurl);
NSLog(@"Sina gender: %@", resp.unionGender);
// 第三方平臺(tái)SDK源數(shù)據(jù)
NSLog(@"Sina originalResponse: %@", resp.originalResponse);
}
}];
登錄會(huì)碰到微信有時(shí)候獲取不到東西的情況特占,把運(yùn)行的APP卸載了在運(yùn)行一下就沒問題了糙置。配合官方的文檔,再看看我的是目,應(yīng)該很容易就懂了谤饭。之前集成過友盟SDK的,升級(jí)的時(shí)候只需要改方法就可以了懊纳,其它地方不需要?jiǎng)尤嗟郑稽c(diǎn)都不要?jiǎng)樱駝t你懂得嗤疯。冤今。有什么錯(cuò)誤不知道我沒有說到的,然后搜了網(wǎng)上也沒有答案的身弊,問友盟的人工客服吧,有的客服還是可以很好的幫忙解決問題的列敲。
最后附上錯(cuò)誤碼阱佛,集成文檔里面有的,我總覺得應(yīng)該有人和我一樣粗心不會(huì)看錯(cuò)誤碼的戴而,也有可能是我想當(dāng)然(捂臉)凑术。。
//平臺(tái)的失敗錯(cuò)誤碼
/**
* U-Share返回錯(cuò)誤類型
*/
typedef NS_ENUM(NSInteger, UMSocialPlatformErrorType) {
UMSocialPlatformErrorType_Unknow = 2000, // 未知錯(cuò)誤
UMSocialPlatformErrorType_NotSupport = 2001, // 不支持(url scheme 沒配置所意,或者沒有配置-ObjC淮逊, 或則SDK版本不支持或則客戶端版本不支持)
UMSocialPlatformErrorType_AuthorizeFailed = 2002, // 授權(quán)失敗
UMSocialPlatformErrorType_ShareFailed = 2003, // 分享失敗
UMSocialPlatformErrorType_RequestForUserProfileFailed = 2004, // 請(qǐng)求用戶信息失敗
UMSocialPlatformErrorType_ShareDataNil = 2005, // 分享內(nèi)容為空
UMSocialPlatformErrorType_ShareDataTypeIllegal = 2006, // 分享內(nèi)容不支持
UMSocialPlatformErrorType_CheckUrlSchemaFail = 2007, // schemaurl fail
UMSocialPlatformErrorType_NotInstall = 2008, // 應(yīng)用未安裝
UMSocialPlatformErrorType_Cancel = 2009, // 取消操作
UMSocialPlatformErrorType_NotNetWork = 2010, // 網(wǎng)絡(luò)異常
UMSocialPlatformErrorType_SourceError = 2011, // 第三方錯(cuò)誤
UMSocialPlatformErrorType_ProtocolNotOverride = 2013, // 對(duì)應(yīng)的 UMSocialPlatformProvider的方法沒有實(shí)現(xiàn)
UMSocialPlatformErrorType_NotUsingHttps = 2014, // 沒有用https的請(qǐng)求,@see UMSocialGlobal isUsingHttpsWhenShareContent
};