在 Apple Watch 上运行 Apple Shortcut 时,可能会遇到 HTTPs 请求失败的问题。这是因为 Apple Watch 上没有安装根证书,因此无法验证 SSL 证书。
解决这个问题的方法是将根证书添加到 Apple Watch 上。
以下是示例代码:
let cfData = NSData(contentsOfFile: "AddTrustExternalCARoot.crt")!
let cert = SecCertificateCreateWithData(nil, cfData)
let certs: Array = [cert!]
let certSet = NSSet(array: certs)
let sessionConfig = NSURLSessionConfiguration.defaultSessionConfiguration()
sessionConfig.URLCredentialStorage = NSURLCredentialStorage.sharedCredentialStorage()
sessionConfig.URLCredentialStorage.setCredential(URLCredential(trust:cert!), forProtectionSpace:protectionSpace)
let session = NSURLSession(configuration:sessionConfig)
需要'AddTrustExternalCARoot.crt”替换为你想要添加的根证书的名称。