IOS 配置通用链接(Universal Link)服务端nginx配置apple-app-site-association文件

IOS接入微信支付配置通用链接,后端Nginx配置

背景表述

开发的项目中IOS客户端早期是通过短连接跳转safari中介页面的方式实现唤醒APP,这种方式需要中介网页通过JS 延时执行的方式判断 系统是否安装了APP。

执行流程

目前微信对Scheme 打开外部应用做了限制。IOS 要做到从微信直接打开APP 需要使用到 Universal Links 。IOS app 第一次启动时,系统默认从支持HTTPS的注册域名下获取 apple-app-site-association 文件Universal Links内容(https://xxx.com/apple-app-site-association,https://xxx.com/.well-known/apple-app-site-association)
服务端通过反向代理服务器Nginx 直接响应静态字符串的方式 向IOS客户端 提供 apple-app-site-association查询功能,这种方式既可以避免因系统升级造成apple-app-site-association 查询不可用,也可以提升请求响应速度减轻后端 web 服务器不必要的压力。

具体配置方式:在nginx 配置文件 server 节点下新增 两个location 配置

完整nginx配置

    server {
        listen    80;
        listen 443 ssl;
        server_name  www.daidu.com;
        ssl_certificate     /usr/local/ssl/baidu.com.crt;
        ssl_certificate_key /usr/local/ssl/baidu.com.key;
        ssl_session_timeout 5m;
        ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
        ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE;
        ssl_prefer_server_ciphers on;
        #charset utf-8;
	   # client_max_body_size 用来修改允许客户端上传文件的大小。默认为1m,如果设置为0,表示上传文件大小不受限制。
	   # 可以在以下模块设置: http, server, location 
	   client_max_body_size 100m;

		#此配置是ios接入微信支付时,供微信公共后台配置Domans:applinks:www.daidu.com; IOS端开发人员说微信要求只能是域名根目录下访问apple-app-site-association文件(可以和业务接口域名可以不同,只要能访问到文件就行),流程是:我们的app调起微信。 微信拿这个通用链接验证我们的app 然后结束后在返回我们的app
        location = / {
			        # 移除默认类型(避免冲突)
			        default_type "";
			        # 设置文件下载相关响应头
			        add_header Content-Type "application/octet-stream";  # 二进制流类型,通用下载格式
			        add_header Content-Disposition "attachment; filename=\"apple-app-site-association\"";
			        # 返回JSON内容(直接返回字符串或引用文件)
			        # 方式1:直接返回JSON字符串(适合内容固定场景)
			        return 200 '{
			                "applinks": {
			                    "apps": [],
			                    "details": [
			                        {
			                            "appID": "xxxxxx.com.xxxxx.xxxx",
			                            "paths": ["*", "/app/*"]
			                        }
			                    ]
			                }
			            }';
			}
	    #此配置是ios接入微信支付时,供apple的校验使用,验证时apple的CDN会在www.daidu.com域名后面拼上.well-known/apple-app-site-association文件名称 ,也就是https://app-site-association.cdn-apple.com/a/v1/www.daidu.com实际访问的
 #也就是指向服务器的https://www.daidu.com/.well-known/apple-app-site-association 访问到规则文件的json,苹果 CDN 对apple-app-site-association文件的取首次抓取存在24-48 小时的延迟,即使源服务器文件已部署,直接访问 CDN 路径仍可能返回404
	 location = /.well-known/apple-app-site-association {
            # 移除默认二进制类型(关键!)
        default_type "";  # 或直接注释掉default_type相关配置
        add_header Content-Type 'application/json';
        return 200 '{
            "applinks": {
                "apps": [],
                "details": [
                    {
                        "appID": "xxxxxx.com.xxxxx.xxxx",
                        "paths": ["*", "/app/*"]
                    }
                ]
            }
        }';
      }
      }

即配置成以后,有3个链接要前两个二选一(apple访问优先级 第二个链接>第一个链接),和 第三个链接都能访问成功才可以(有可能前面两个配置的链接可以访问,第三个访问Not Found,需要等待苹果的CDN缓存刷新,有可能1小时,也有可能几天):


https://你的域名.com/apple-app-site-association
https://你的域名.com/.well-known/apple-app-site-association
https://app-site-association.cdn-apple.com/a/v1/你的域名.com
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值