友盟iOS微信登陸沒有回撥的原因

ihtcboy發表於2015-08-07

1、在友盟文件中這樣說:

連結

7.4  微信登入
新增配置檔案參考文件:新增微信及朋友圈,新增相關庫檔案,配置URL schemes及新增系統回撥

注意微信登入必須先在微信開放平臺申請微信登入許可權
在你的程式APPdelegate入口方法新增下面的程式碼

  #import "UMSocialWechatHandler.h"
    //設定微信AppId、appSecret,分享url
    [UMSocialWechatHandler setWXAppId:@"wxd930ea5d5a258f4f" appSecret:@"db426a9829e4b49a0dcac7b4162da6b6" url:@"http://www.umeng.com/social"];

在微信登入按鈕中實現下面的方法

  UMSocialSnsPlatform *snsPlatform = [UMSocialSnsPlatformManager getSocialPlatformWithName:UMShareToWechatSession];

        snsPlatform.loginClickHandler(self,[UMSocialControllerService defaultControllerService],YES,^(UMSocialResponseEntity *response){

            if (response.responseCode == UMSResponseCodeSuccess) {

                UMSocialAccountEntity *snsAccount = [[UMSocialAccountManager socialAccountDictionary]valueForKey:UMShareToWechatSession];

                NSLog(@"username is %@, uid is %@, token is %@ url is %@",snsAccount.userName,snsAccount.usid,snsAccount.accessToken,snsAccount.iconURL);

            }

 });

在授權完成後呼叫獲取使用者資訊的方法

//得到的資料在回撥Block物件形參respone的data屬性
[[UMSocialDataService defaultDataService] requestSnsInformation:UMShareToWechatsession  completion:^(UMSocialResponseEntity *response){
        NSLog(@"SnsInformation is %@",response.data);
    }];


2、實踐使用時:

#pragma mark - 微信登入
- (void)loginViewClickedWeiXiButton{
    
    [UMSocialData setAppKey:@"55bb141a67e58e9f80001801"];
    [UMSocialWechatHandler setWXAppId:@"wx2802d4e49f68996f" appSecret:@"f1bf8a16c5f87570ab07782d3e0ab223" url:@"http://www.umeng.com/social"];
    UMSocialSnsPlatform *snsPlatform = [UMSocialSnsPlatformManager getSocialPlatformWithName:UMShareToWechatSession];
    snsPlatform.loginClickHandler(self,[UMSocialControllerService defaultControllerService],YES,^(UMSocialResponseEntity *response){
        //  獲取微博使用者名稱、uid、token等
        if (response.responseCode == UMSResponseCodeSuccess) {
             UMSocialAccountEntity *snsAccount = [[UMSocialAccountManager socialAccountDictionary]valueForKey:UMShareToWechatSession];
            NSLog(@"username is %@, uid is %@, token is %@ iconUrl is %@",snsAccount.userName,snsAccount.usid,snsAccount.accessToken,snsAccount.iconURL);
        }
    });
    
}

3、【坑在這裡】沒有回撥時因為要在應用代理方法中增加:

//sdk
#import "UMSocialSnsService.h"

- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
{
    return  [UMSocialSnsService handleOpenURL:url wxApiDelegate:nil];
}

// 2018.01.05 更新: iOS 9.0 以上替換上面的代理回撥
- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<UIApplicationOpenURLOptionsKey,id> *)options
{
     return  [UMSocialSnsService handleOpenURL:url wxApiDelegate:nil];
}
- (void)applicationDidBecomeActive:(UIApplication *)application
{
    [UMSocialSnsService  applicationDidBecomeActive];
}

最後說明

  • 微信的openid對應友盟裡叫usid

下面是授權成功的回撥資料:

- UMSocial:  makeRequestWithPath is /share/token/53290df956240b6b4a0084b3/, parameters is {
    "access_token" = "OezXcEiiBSKSxW0eoylIeJDUKD6z6dmr42JANLPjNN56WRFnd-vVJO37ma0gCpKY1v5q8EVEMwdQqzvXoFFpe1jQXUzjqukub9-228J8sjvugvVVQYvzkhyvAgItvkYmMIGD_cE2hAWUm44_qCB-oQ";
    ak = 53290df956240b6b4a0084b3;
    de = "iPhone7,1";
    dt = 1438857527939;
    ek = 4b37315fd0421b0b3a682a4a5e4487e9;
    en = WiFi;
    "expires_in" = 7199;
    mac = "02:00:00:00:00:00";
    openid = oyAaTjrJMAb3qfCbyDRp3h0NKyew;
    openudid = 147ad485c3b80ffd54bfc8689c2d7db1ed88994b;
    os = iOS;
    pcv = "2.0";
    "refresh_token" = "OezXcEiiBSKSxW0eoylIeJDUKD6z6dmr42JANLPjNN56WRFnd-vVJO37ma0gCpKYHXgyDeQ4Xqm60G-omrGp2u-KbIy7zTP5gV5dnw3RVN-GzkjY8gCWIMC3_cCHcflH6IPexy2c-k46gGG0M07p6g";
    sdkv = "4.2.3";
    to = wxsession;
    tp = 0;
    uid = 462ae0edb57d53dfd2affb5e4ae36ad;
    usid = oyAaTjrJMAb3qfCbyDRp3h0NKyew;
} 
[18:38:48.197( 2055)] - UMSocial:  actionType is 15 , received json Dic is {
    data =     {
    };
    msg = "no error";
    st = 200;
} 

  • 如果有什麼疑問,可以在評論區一起討論;
  • 如果有什麼不正確的地方,歡迎指導!

2018.01.05 更新

iOS 9.0 以後,系統新增加代理回撥方法:

  • (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<UIApplicationOpenURLOptionsKey,id> *)options
    {
    return [UMSocialSnsService handleOpenURL:url wxApiDelegate:nil];
    }

試試增加這個後行不行,很久沒有使用友盟SDK了,現在都是做國外產品,如果還不行,大家問一下友盟客服啊。

注:本文首發於 iHTCboy`s blog,如若轉載,請註明來源。


相關文章