淺談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)尤嗟郑稽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
};
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末催首,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子泄鹏,更是在濱河造成了極大的恐慌郎任,老刑警劉巖,帶你破解...
    沈念sama閱讀 218,284評(píng)論 6 506
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件备籽,死亡現(xiàn)場(chǎng)離奇詭異舶治,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)车猬,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,115評(píng)論 3 395
  • 文/潘曉璐 我一進(jìn)店門霉猛,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人珠闰,你說我怎么就攤上這事惜浅。” “怎么了伏嗜?”我有些...
    開封第一講書人閱讀 164,614評(píng)論 0 354
  • 文/不壞的土叔 我叫張陵坛悉,是天一觀的道長。 經(jīng)常有香客問我阅仔,道長吹散,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,671評(píng)論 1 293
  • 正文 為了忘掉前任八酒,我火速辦了婚禮空民,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘羞迷。我一直安慰自己界轩,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,699評(píng)論 6 392
  • 文/花漫 我一把揭開白布衔瓮。 她就那樣靜靜地躺著浊猾,像睡著了一般。 火紅的嫁衣襯著肌膚如雪热鞍。 梳的紋絲不亂的頭發(fā)上葫慎,一...
    開封第一講書人閱讀 51,562評(píng)論 1 305
  • 那天,我揣著相機(jī)與錄音薇宠,去河邊找鬼偷办。 笑死,一個(gè)胖子當(dāng)著我的面吹牛澄港,可吹牛的內(nèi)容都是我干的椒涯。 我是一名探鬼主播,決...
    沈念sama閱讀 40,309評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼回梧,長吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼废岂!你這毒婦竟也來了祖搓?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,223評(píng)論 0 276
  • 序言:老撾萬榮一對(duì)情侶失蹤湖苞,失蹤者是張志新(化名)和其女友劉穎拯欧,沒想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體袒啼,經(jīng)...
    沈念sama閱讀 45,668評(píng)論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡哈扮,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,859評(píng)論 3 336
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了蚓再。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片滑肉。...
    茶點(diǎn)故事閱讀 39,981評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖摘仅,靈堂內(nèi)的尸體忽然破棺而出靶庙,到底是詐尸還是另有隱情,我是刑警寧澤娃属,帶...
    沈念sama閱讀 35,705評(píng)論 5 347
  • 正文 年R本政府宣布六荒,位于F島的核電站,受9級(jí)特大地震影響矾端,放射性物質(zhì)發(fā)生泄漏掏击。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,310評(píng)論 3 330
  • 文/蒙蒙 一秩铆、第九天 我趴在偏房一處隱蔽的房頂上張望砚亭。 院中可真熱鬧,春花似錦殴玛、人聲如沸捅膘。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,904評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽寻仗。三九已至,卻和暖如春凡壤,著一層夾襖步出監(jiān)牢的瞬間署尤,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,023評(píng)論 1 270
  • 我被黑心中介騙來泰國打工亚侠, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留曹体,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 48,146評(píng)論 3 370
  • 正文 我出身青樓盖奈,卻偏偏與公主長得像混坞,于是被迫代替她去往敵國和親狐援。 傳聞我的和親對(duì)象是個(gè)殘疾皇子钢坦,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,933評(píng)論 2 355

推薦閱讀更多精彩內(nèi)容