淺談iOS友盟分享SDK6.4.4

最近公司在做第三方登錄,遇到的坑那叫一個多啊,所以我一定要寫個東西記錄下來,畢竟我在做的時候,網上關于友盟最新SDK的資料實在太少了。
在我嘗試了用QQ,微信,微博SDK后最終還是決定用友盟的,因為之前項目里集成了分享,也是用的友盟的SDK,里面也已經有了現成的也不用在集成了。可是QQ的unionId在友盟SDK5.2.1獲取不到怎么都獲取不到,那個坎坷啊,最終走向了升級的道路。下面進入正題。。
首先刪掉原有的SDK,我是用的cocoapods,在Podfilel里面把原有的SDK刪掉,加上最新的要下載的SDK

集成SDK.png

至于開始怎么集成的步驟之類的我在這里就不講了,認真仔細的看集成文檔,上面都是有的。

接下來直接上代碼
首先在AppDelegate加上下面的代碼

 #import <UMSocialCore/UMSocialCore.h>   //友盟頭文件
- (BOOL)application:(UIApplication *)application
didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
  /* 設置友盟appkey */
[[UMSocialManager defaultManager] setUmSocialAppkey:YM_Share_App_Key];

/* 設置微信的appKey和appSecret */
[[UMSocialManager defaultManager] setPlaform:UMSocialPlatformType_Sina appKey:wbAPPKey appSecret:wbAPPSecret redirectURL:@"http://sns.whalecloud.com/sina2/callback"];

/* 設置微信的appKey和appSecret */
[[UMSocialManager defaultManager] setPlaform:UMSocialPlatformType_WechatSession appKey:wxAPPID appSecret:wxAPPSecret redirectURL:@"http://mobile.umeng.com/social"];

/* 設置分享到QQ互聯的appID
 * U-Share SDK為了兼容大部分平臺命名,統一用appKey和appSecret進行參數設置,而QQ平臺僅需將appID作為U-Share的appKey參數傳進即可。
 */
[[UMSocialManager defaultManager] setPlaform:UMSocialPlatformType_QQ appKey:QQAPPID/*設置QQ平臺的appID*/  appSecret:nil redirectURL:@"http://mobile.umeng.com/social"];

//允許HTTP傳輸,分享圖片
[UMSocialGlobal shareInstance].isUsingHttpsWhenShareContent = NO;
}

接著創建一個分享面板,也可以用友盟自帶的,分享面板這里我已經創建好了
直接在按鈕點擊事件里面加上下面的代碼這里是分享的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
  {
//創建分享消息對象
UMSocialMessageObject *messageObject = [UMSocialMessageObject messageObject];

//創建網頁內容對象 這里給了圖片
UIImage *thumbURL = self.image;
UMShareWebpageObject *shareObject = [UMShareWebpageObject shareObjectWithTitle:self.artTiltle descr:self.artTiltle thumImage:thumbURL];
//設置網頁地址 文章的url
shareObject.webpageUrl = self.url;

//分享消息對象設置分享內容對象
messageObject.shareObject = shareObject;

//調用分享接口
[[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;
            //分享結果消息
            UMSocialLogInfo(@"response message is %@",resp.message);
            //第三方原始返回的數據
            UMSocialLogInfo(@"response originalResponse data is %@",resp.originalResponse);
            
        }else{
            UMSocialLogInfo(@"response data is %@",data);
        }
    }
    [self alertWithError:error]; //分享調試錯誤彈框
}];
}

pragma mark -- 分享調試信息打印

- (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里面已經都必須用https了,如果報下面的這個錯誤

2014錯誤.png

需要在這樣操作

 /*
 * 關閉強制驗證https,可允許http圖片分享,但需要在info.plist設置安全域名
 <key>NSAppTransportSecurity</key>
 <dict>
 <key>NSAllowsArbitraryLoads</key>
 <true/>
 </dict>
 */

光上面的配置了還不可以,這句代碼一定一定要在AppDelegate加上,否則這個錯誤是不會消失的

 [UMSocialGlobal shareInstance].isUsingHttpsWhenShareContent = NO;

加上這句代碼就可以分享網絡圖片了,下面附上分享網絡圖片的代碼

//分享圖片
- (void)shareImageToPlatformType:(UMSocialPlatformType)platformType
{
//創建分享消息對象
UMSocialMessageObject *messageObject = [UMSocialMessageObject messageObject];

//創建圖片內容對象
UMShareImageObject *shareObject = [[UMShareImageObject alloc] init];
//如果有縮略圖,則設置縮略圖
shareObject.thumbImage = self.imageURL;//這里是圖片的鏈接
[shareObject setShareImage:self.imageURL];
messageObject.shareObject = shareObject;

BLLog(@"ImageURL = %@",self.imageURL);
    //調用分享接口
    [[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;
                //分享結果消息
                UMSocialLogInfo(@"response message is %@",resp.message);
                //第三方原始返回的數據
                UMSocialLogInfo(@"response originalResponse data is %@",resp.originalResponse);
                
                NSLog(@"2 ---");
                
            }else{
                UMSocialLogInfo(@"response data is %@",data);
                NSLog(@"; ---");
            }
        }
        [self alertWithError:error];
    }];
}

分享圖片的時候有可能會遇到微信,QQ,新浪都可以分享,但是唯獨QQ空間分享不成功,報下面的這個錯誤

QQ空間分享不成功錯誤.png

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

 <string>mqqopensdkapiV4</string>
image.png

分享在這里就說完了。

接下來我們說一說登錄,登錄的也要在AppDelegate注冊,但是只需要注冊一遍就可以了,你分享的時候注冊了,登錄就不用了,它們是一起的。在登錄界面創建三個登錄按鈕,微信,QQ,微博。三個的按鈕點擊事件里面寫上下面的代碼。別忘記導入頭文件。

pragma mark--QQ登錄

  -(void)qqButtonClick:(UIButton *)button{
BLLog(@"QQ");

[[UMSocialManager defaultManager] getUserInfoWithPlatform:UMSocialPlatformType_QQ currentViewController:self completion:^(id result, NSError *error) {
     if (error) {
         //授權失敗
    } else {
    UMSocialUserInfoResponse *resp = result;
    
    // 第三方登錄數據(為空表示平臺未提供)
    // 授權數據
    NSLog(@" uid: %@", resp.uid);
    NSLog(@" openid: %@", resp.openid);
    NSLog(@" accessToken: %@", resp.accessToken);
    NSLog(@" unionId: %@", resp.unionId);
  
    
    // 用戶數據
    NSLog(@" name: %@", resp.name);
    NSLog(@" iconurl: %@", resp.iconurl);
    NSLog(@" gender: %@", resp.unionGender);
    
    // 第三方平臺SDK原始數據
    NSLog(@" originalResponse: %@", resp.originalResponse);
}];
}

pragma mark -- 微信登錄

-(void)WxButtonClick:(UIButton *)button{

[[UMSocialManager defaultManager] getUserInfoWithPlatform:UMSocialPlatformType_WechatSession currentViewController:nil completion:^(id result, NSError *error) {
    if (error) {
        //授權失敗
    } else {
        UMSocialUserInfoResponse *resp = result;
        
        // 授權信息
        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);
        
        // 第三方平臺SDK源數據
        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) {
        //授權失敗
    } else {
        UMSocialUserInfoResponse *resp = result;
        
        // 授權信息
        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);
        
        // 第三方平臺SDK源數據
        NSLog(@"Sina originalResponse: %@", resp.originalResponse);
    }
}];

登錄會碰到微信有時候獲取不到東西的情況,把運行的APP卸載了在運行一下就沒問題了。配合官方的文檔,再看看我的,應該很容易就懂了。之前集成過友盟SDK的,升級的時候只需要改方法就可以了,其它地方不需要動,一點都不要動,否則你懂得。。有什么錯誤不知道我沒有說到的,然后搜了網上也沒有答案的,問友盟的人工客服吧,有的客服還是可以很好的幫忙解決問題的。
最后附上錯誤碼,集成文檔里面有的,我總覺得應該有人和我一樣粗心不會看錯誤碼的,也有可能是我想當然(捂臉)。。

  //平臺的失敗錯誤碼
/**
 *  U-Share返回錯誤類型
 */
typedef NS_ENUM(NSInteger, UMSocialPlatformErrorType) {
UMSocialPlatformErrorType_Unknow            = 2000,            // 未知錯誤
UMSocialPlatformErrorType_NotSupport        = 2001,            // 不支持(url scheme 沒配置,或者沒有配置-ObjC, 或則SDK版本不支持或則客戶端版本不支持)
UMSocialPlatformErrorType_AuthorizeFailed   = 2002,            // 授權失敗
UMSocialPlatformErrorType_ShareFailed       = 2003,            // 分享失敗
UMSocialPlatformErrorType_RequestForUserProfileFailed = 2004,  // 請求用戶信息失敗
UMSocialPlatformErrorType_ShareDataNil      = 2005,             // 分享內容為空
UMSocialPlatformErrorType_ShareDataTypeIllegal = 2006,          // 分享內容不支持
UMSocialPlatformErrorType_CheckUrlSchemaFail = 2007,            // schemaurl fail
UMSocialPlatformErrorType_NotInstall        = 2008,             // 應用未安裝
UMSocialPlatformErrorType_Cancel            = 2009,             // 取消操作
UMSocialPlatformErrorType_NotNetWork        = 2010,             // 網絡異常
UMSocialPlatformErrorType_SourceError       = 2011,             // 第三方錯誤

UMSocialPlatformErrorType_ProtocolNotOverride = 2013,   // 對應的    UMSocialPlatformProvider的方法沒有實現
UMSocialPlatformErrorType_NotUsingHttps      = 2014,   // 沒有用https的請求,@see UMSocialGlobal isUsingHttpsWhenShareContent
};
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容