苹果开发者

微信Universal Links

2020-02-07  本文已影响0人  喵喵粉

go

1. 添加文件

{
    "applinks": {
        "apps": [],
        "details": [
            {
                "appID": "TeamID.bundleID",
                "paths": ["*"]
            }
        ]
    }
}
https://aaa.com/apple-app-site-association

2. Xcode设置

填写applinks:+服务器的域名,如applinks:aaa.com 或者 applinks:https://aaa.com

Associated Domains LSApplicationQueriesSchemes aa

3. 开发者应用登记页面

image.png

在备忘录中输入https://aaa.com/apple-app-site-association,长按这个链接,出现下图提示则配置成功。

apple-app-site-association.png

4. 写代码


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(nullable NSDictionary<UIApplicationLaunchOptionsKey,id> *)launchOptions {
    [self thirdConfig];
    return YES;
}

- (void)thirdConfig {
    ///微信
    [WXApi registerApp:wxAppID universalLink:@"https://aa.com/"];
}

#pragma mark - Wechat Delegate
- (void)onReq:(BaseReq *)req {
}

- (void)onResp:(BaseResp *)resp {
    NSLog(@"%s resp:%@", __func__, resp);

    // 1.分享后回调类
    if ([resp isKindOfClass:[SendMessageToWXResp class]]) {
        NSLog(@"分享ok");
    }
    // 2.微信登录向微信请求授权回调类
    else if ([resp isKindOfClass:[SendAuthResp class]]) {
        SendAuthResp *rsp = (SendAuthResp *)resp;
        NSLog(@"收到code:%@", rsp.code);
        
        NSString *state = rsp.state;
        if ([state isEqualToString:@""]) {
        }
        
        if (resp.errCode == 0) {
            NSLog(@"登录ok");
        } else {
            NSLog(@"登录失败");
        }
    }
    // 3.支付后回调类
    else if ([resp isKindOfClass:[PayResp class]]) {
        NSLog(@"支付");
    }
}

- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<UIApplicationOpenURLOptionsKey,id> *)options {
    NSLog(@"%s url:%@", __func__, url);
    
    NSString *absoluteString = url.absoluteString;
    if ([absoluteString hasPrefix:wxAppID]) {
        ///微信登录
        return [WXApi handleOpenURL:url delegate:self];
    } else if ([absoluteString hasPrefix:@"xxx"]) {
    }
    
    return [WXApi handleOpenURL:url delegate:self];
}

- (BOOL)application:(UIApplication *)application continueUserActivity:(NSUserActivity *)userActivity restorationHandler:(void (^)(NSArray<id<UIUserActivityRestoring>> * _Nullable))restorationHandler {
    NSLog(@"%s", __func__);
    return [WXApi handleOpenUniversalLink:userActivity delegate:self];
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    SendAuthReq* req = [[SendAuthReq alloc] init];
    req.scope = @"snsapi_userinfo";
    req.state = @"bundleID";
    
    //第三方向微信终端发送一个SendAuthReq消息结构
    //发送请求到微信,等待微信返回onResp
    [WXApi sendReq:req completion:nil];
}

5. 踩坑

-canOpenURL: failed for URL: "weixinULAPI://" - error: "This app is not allowed to query for scheme weixinulapi"

解决:填写URL Types

6. 微信的scope说明

授权作用域(scope) 接口 接口说明
snsapi_base /sns/oauth2/access_token 通过code换取access_token、refresh_token和已授权scope
/sns/oauth2/refresh_token 刷新或续期access_token使用
/sns/auth 检查access_token有效性
snsapi_userinfo /sns/userinfo 获取用户个人信息

其中snsapi_base属于基础接口,若应用已拥有其它scope权限,则默认拥有snsapi_base的权限。使用snsapi_base可以让移动端网页授权绕过跳转授权登录页请求用户授权的动作,直接跳转第三方网页带上授权临时票据(code),但会使得用户已授权作用域(scope)仅为snsapi_base,从而导致无法获取到需要用户授权才允许获得的数据和基础功能。

上一篇下一篇

猜你喜欢

热点阅读