ios HTTP请求尝试通过API报告不需要的通信报告失败

ghg1uchk  于 2023-08-08  发布在  iOS
关注(0)|答案(2)|浏览(137)

我创建了一个不需要的通信报告扩展,如下所述:https://developer.apple.com/documentation/sms_and_call_reporting/sms_and_call_spam_reporting
按照说明进行操作,并能够在扩展类中获得报告回调,但当我尝试向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查找失败,由于沙箱的限制,但我不能允许应用程序沙箱访问,这似乎是一个OSX特定的能力。
下面是我负责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和domain.com,以及我的apple-app-site-association文件上传如下:

{
"classificationreport": {
    "apps": ["L***.com.b***.smsreporting.unwantedreporting","L***.com.b***.smsreporting"]
}


任何帮助和指导是赞赏,因为我被困在这一点上,缺乏文件,这是使事情变得更加困难。

编辑:我正在我自己的开发者帐户上测试,这是一个个人会员,而不是组织,如果这很重要的话。

g9icjywg

g9icjywg1#

您似乎在不需要的通信报告扩展中调用了reportToAPI()
但是,如果您正确地将apple-app-site-associationAssociation Domain添加到.entitlement中。不需要自己创建URLRequest。Apple将代表您发送API。(我认为这是出于隐私原因,比如Message Filter Extension
所以classificationResponse应该看起来像这样:

override func classificationResponse(for request:ILClassificationRequest) -> ILClassificationResponse {
    let payload: [String: AnyHashable] = [
        "data": "OK",
        "test": "payload"
    ]
    let action: ILClassificationAction = .reportJunk  // or .none, .reportNotJunk, .reportJunkAndBlockSender
    let response = ILClassificationResponse(action: action)
    response.userInfo = payload

    return response
}

字符串

f87krz0w

f87krz0w2#

错误消息表明DNS查找由于沙箱限制而失败。您遇到的错误“Sandbox does not allow access to com.apple.dnssd.service”表示您的应用被限制访问沙箱环境中的DNS服务。
您需要允许应用沙箱访问必要的功能。单个开发者帐户可能无法启用应用所需的功能。您可能需要组织成员资格。
关于你的代码,一切似乎都很正常。但是,我建议做一些改进来处理错误。下面是代码的更新版本:

func reportToAPI(for request: ILClassificationRequest) {
    let payload: [String: AnyHashable] = [
        "data": "OK",
        "test": "payload"
    ]
    
    guard let url = URL(string: "https://sub.domain.com/debug") else {
        print("Invalid URL")
        return
    }
    
    var request = URLRequest(url: url)
    request.httpMethod = "POST"
    request.setValue("application/json", forHTTPHeaderField: "Content-Type")
    
    do {
        request.httpBody = try JSONSerialization.data(withJSONObject: payload, options: .fragmentsAllowed)
    } catch {
        print("Failed to serialize payload")
        return
    }
    
    let task = URLSession.shared.dataTask(with: request) { data, response, error in
        if let error = error {
            print("Error: \(error.localizedDescription)")
            return
        }
        
        guard let data = data else {
            print("No data received")
            return
        }
        
        do {
            let json = try JSONSerialization.jsonObject(with: data, options: .allowFragments)
            print("API response: \(json)")
        } catch {
            print("Failed to parse API response: \(error.localizedDescription)")
        }
    }
    
    task.resume()
}

字符串
变更
1.改进的错误处理:

  • 在有效载荷的序列化周围添加了do-catch块,以捕获所有序列化错误。
  • 添加了针对未从API请求接收到数据的情况的错误处理。
  • 添加了用于解析API响应的错误处理。
  • 出现错误时打印特定的错误说明。

1.修改了print语句以提供更多信息反馈,例如打印API响应或指示失败原因。

相关问题