1. 程式人生 > >iOS 關於友盟微信分享成功回撥不走的問題

iOS 關於友盟微信分享成功回撥不走的問題

不得不說,友盟的客服以及論壇服務很不周到,特別是最近要獲取友盟微信分享是否成功的狀態,在分享回撥里根本拿不到,回撥根本不走。於是檢視友盟論壇,提問的人很多,解答的完全沒有,再諮詢線上客服,回覆答不到點子上,或者就要你看文件或demo,然而你的demo也有問題啊。

問題描述本專案集成了友盟分享,微信支付等,要實現從APP跳轉到微信,再從微信返回APP,需要在AppDelegate裡實現-(BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<UIApplicationOpenURLOptionsKey,id> *)options (iOS9之後的方法);同時,在分享的時候呼叫友盟的分享API,其中有completion回撥,本來理應在這裡檢測到分享是否成功的,但是此completion卻根本不走。諮詢友盟客服,對方只提到要在前面說的AppDelegate代理方法裡處理,到底怎麼做,看文件。。。

系統回撥友盟文件:

- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<UIApplicationOpenURLOptionsKey, id> *)options
{
    //6.3的新的API呼叫,是為了相容國外平臺(例如:新版facebookSDK,VK等)的呼叫[如果用6.2的api呼叫會沒有回撥],對國內平臺沒有影響
    BOOL result = [[UMSocialManager defaultManager]  handleOpenURL:url options:options];
    if (!result) {
        // 其他如支付等SDK的回撥
    }
    return result;
}

demo裡也是一樣,因為他們只想到怎麼實現友盟的功能,關鍵我這裡還集成了微信支付,兩者都會走以上代理,所以要做到區分!

友盟分享調起的API:

//呼叫分享介面
    [[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);
            }
        }
    }];

問題關鍵:同時集成了友盟分享和微信支付,都走同一個代理方法,需要做到區分,否則分享回撥不走!

解決方法:

-(BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<UIApplicationOpenURLOptionsKey,id> *)options
{
//    DLog(@"url = %@ \n options = %@",url,options);
    if ([url.host isEqualToString:@"safepay"]) {
        //跳轉支付寶錢包進行支付,處理支付結果
        [[AlipaySDK defaultService] processOrderWithPaymentResult:url standbyCallback:^(NSDictionary *resultDic) {
            NSLog(@"result = %@",resultDic);
        }];
        return YES;
    }else if ([options[UIApplicationOpenURLOptionsSourceApplicationKey] isEqualToString:@"com.tencent.xin"] && [url.absoluteString containsString:@"pay"]) {
        return [WXApi handleOpenURL:url delegate:[WXApiManager sharedManager]];
    }else{
        return [[UMSocialManager defaultManager] handleOpenURL:url];
    }
}
關鍵就在於第二個if判斷中的[url.absoluteString containsString:@"pay"],之前沒寫這句,所有不管是微信支付還是微信分享,都會走到第二個return,導致分享的return(第三個)沒有走到。這裡出錯就導致分享時的回撥也沒有走。

雖然不管是微信支付還是微信分享,返回APP時都會帶有com.tencent.xin欄位,但是url是不同的,區分就在這裡。