淺談iOS友盟分享SDK6.4.4

最近公司在做第三方登錄,遇到的坑那叫一個(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

集成SDK.png

至于開始怎么集成的步驟之類的我在這里就不講了,認(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ò)誤

2014錯(cuò)誤.png

需要在這樣操作

 /*
 * 關(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ò)誤

QQ空間分享不成功錯(cuò)誤.png

這個(gè)時(shí)候你需要在info.plist文件里面加入下面的這句話就可以了

 <string>mqqopensdkapiV4</string>
image.png

分享在這里就說完了。

接下來我們說一說登錄,登錄的也要在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)?,一點(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
};
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容