iOS垃圾通讯上报扩展API请求失败问题求助
Unwanted Communication Reporting Extension 沙箱限制导致DNS解析失败问题
我按照苹果官方文档创建了Unwanted Communication Reporting Extension,已成功获取上报回调,但调用HTTPS服务器API时,控制台出现以下错误:
nw_resolver_can_use_dns_xpc_block_invoke Sandbox does not allow access to com.apple.dnssd.service
以及:
Task <C31ADB51-B5C3-4E85-800E-405781F6A9ED>.<1> finished with error [-1003] Error Domain=NSURLErrorDomain Code=-1003 "A server with the specified hostname could not be found." UserInfo={_kCFStreamErrorCodeKey=-72000, NSUnderlyingError=0x282d5c600 {Error Domain=kCFErrorDomainCFNetwork Code=-1003 "(null)" UserInfo={_NSURLErrorNWPathKey=satisfied (Path is satisfied), interface: en0[802.11], ipv4, dns, _kCFStreamErrorCodeKey=-72000, _kCFStreamErrorDomainKey=10}}, _NSURLErrorFailingURLSessionTaskErrorKey=LocalDataTask <C31ADB51-B5C3-4E85-800E-405781F6A9ED>.<1>, _NSURLErrorRelatedURLSessionTaskErrorKey=("LocalDataTask <C31ADB51-B5C3-4E85-800E-405781F6A9ED>.<1>"), NSLocalizedDescription=A server with the specified hostname could not be found., NSErrorFailingURLStringKey=https://sub.domain.com/debug, NSErrorFailingURLKey=https://sub.domain.com/debug, _kCFStreamErrorDomainKey=10}
本质是沙箱限制导致DNS解析失败,但无法为应用沙箱开启相关权限(该权限似乎为macOS专属)。
上报API代码示例
func reportToAPI(for request: ILClassificationRequest) { let payload: [String: AnyHashable] = [ "data": "OK", "test": "payload" ] guard let url = URL(string: "https://sub.domain.com/debug") else { return } var request = URLRequest(url: url) request.httpMethod = "POST" request.setValue("application/json", forHTTPHeaderField: "Content-Type") request.httpBody = try? JSONSerialization.data(withJSONObject: payload, options: .fragmentsAllowed) let task = URLSession.shared.dataTask(with: request) { data, _, error in guard let data = data, error == nil else { return } do { let response = try JSONSerialization.jsonObject(with: data, options: .allowFragments) print(response) } catch { } } task.resume() }
已完成的配置
- 在扩展和主应用目标中配置关联域名:
classificationreport:sub.domain.com和classificationreport:domain.com - 已上传
apple-app-site-association文件,内容如下:
{ "classificationreport": { "apps": ["L***.com.b***.smsreporting.unwantedreporting","L***.com.b***.smsreporting"] } }
补充信息
使用个人开发者账号进行测试,而非企业账号。
内容的提问来源于stack exchange,提问作者Bartu
相关产品推荐
相关产品推荐

