【11】Apple第三方登陆

2020-07-22  本文已影响0人  风青殇
一、通用链接配置

1、在开发者中心进行配置:找到对应的App ID,在 Application Services 列表里将 Associated Domains 更改为 Enabled

2、打开工程配置 Capabilities 选项卡中的 Associated Domains 开关,在其中的 Domains 中填入域名,必须以"applinks:"为前缀;

例:applinks:test.universallinks.com          ---- 注意这里不需要写HTTPS

3、创建JSON 数据格式的名为apple-app-site-association 的文件(名字必须,不能添加后缀),内容为你的应用需关联的所有路径,建议用通配符*号,格式示例:

{
    "applinks": {
        "apps": [],
        "details": [
            {
                "appID": "9JA89QQLNQ.com.apple.wwdc",
                "paths": [ "/wwdc/news/", "/videos/wwdc/2015/*"]
            },
            {
                "appID": "ABCD1234.com.apple.wwdc",
                "paths": [ "*" ]
            }
        ]
    }
}
注:iOS 9.3.1或更高版本中运行的应用程序,apple-app-site-association 的文件大小必须≤128 KB(未验证)

4、上传apple-app-site-association 文件至你的网站服务器(需支持HTTPS 协议),并将其放置网站根目录或 .well-known 文件夹下。

5、在 AppDelegate 的 - (BOOL)application: continueUserActivity: restorationHandler: 方法中处理通用链接。

- (BOOL)application:(nonnull UIApplication *)application
continueUserActivity:(nonnull NSUserActivity *)userActivity
 restorationHandler:(nonnull void (^)(NSArray * __nullable))restorationHandler {
    if ([userActivity.activityType isEqualToString:NSUserActivityTypeBrowsingWeb]) {
        NSURL *webpageURL = userActivity.webpageURL;
        NSString *host = webpageURL.host;
        if ([host isEqualToString:@"test.universallinks.com"]) {
            // TODO:处理应用内跳转逻辑
        }else {
            [[UIApplication sharedApplication]openURL:webpageURL];
        }
        return YES;
    }
    return NO;
}

6、验证是否配置成功

在你的系统备忘录中写你配置的域名,点击打开,拉到最顶上,会出现你的APP图标则代表配置成功;注:页面可能会显示404,可不予理会,拉到最顶上有APP图标便可!

二、客户端配置

基础证书配置不再叙述,仅记录配置第三方登陆;

具体可参考此文章:https://www.wangquanwei.com/572.html

补充:在创建秘钥之前,需要先创建一个Apple登陆的serviceID;

1、创建services IDs

image.png

剩下的步骤一直确定便可;

三、服务端验证

1、为了方便验证,我这里先自己作为服务器进行验证,向https://appleid.apple.com/auth/token请求需要的几个参数:

client_id:传递App的BundleID即可
code:传递客户端获取到的authorizationCode
grant_type:传递authorization_code固定字符串即可
client_secret:可利用以下ruby脚本生成

编写以下脚本,后缀为rb,再从终端执行 ruby 文件名.rb,生成client_secret字符串;

ruby脚本:

require "jwt"
key_file = "xxxxx.p8"   #从Developer Center后台下载的key(p8后缀的文件)
team_id = "xxxxxx"      #开发者账号的teamID
client_id = "com.xxx.xxx"     #应用的BundleID
key_id = "xxxxxx"      #从Developer Center后台key_id
validity_period = 180  #有效期

private_key = OpenSSL::PKey::EC.new IO.read key_file

token = JWT.encode(
    {
        iss: team_id,
        iat: Time.now.to_i,
        exp: Time.now.to_i + 86400 * validity_period,
        aud: "https://appleid.apple.com",
        sub: client_id
    },
    private_key,
    "ES256",
    header_fields=
    {
        kid: key_id
    }
)
puts token
上一篇下一篇

猜你喜欢

热点阅读