我想从我的tvOS应用程序请求youtube-data-api v3。为此,我编写了以下课程:
class YoutubeSearch {
static let sharedClient = YoutubeSearch()
private var task: NSURLSessionDataTask!
func getVideoNSURLsForTitle(title: String, completionHandler: ([String], NSError?) -> Void ) -> NSURLSessionTask {
// URL
let urlString = makeURL(title)
let url = NSURL(string: youtubeApi)
// cancel task, if there is already one
task?.cancel()
// setup new request
let request = NSURLRequest(URL: url!)
let config = NSURLSessionConfiguration.defaultSessionConfiguration()
let session = NSURLSession(configuration: config)
task = session.dataTaskWithRequest(request) { (data, response, error) -> Void in
// No errors occured
do {
// some JSON processing
// complitionHandler is returned here
} catch let parseError {
// Some other error
print(parseError)
let jsonStr = NSString(data: data!, encoding: NSUTF8StringEncoding)
print("YoutubeSearch Error: '\(jsonStr)'")
}
}
// execute request
task.resume()
return task
}
}
代码在模拟器中工作正常。我得到一个有效的JSON。但是在tvOS上它会抛出一个错误,告诉我证书无效:
NSUnderlyingError = 0x13e207d70 {错误域= kCFErrorDomainCFNetwork代码= -1202"(null)" UserInfo = {_ kCFStreamPropertySSLClientCertificateState = 0,kCFStreamPropertySSLPeerTrust =,_ kCFNetworkCFStreamSSLErrorOriginalValue = -9814,_kCFStreamErrorDomainKey = 3,_kCFStreamErrorCodeKey = -9814,kCFStreamPropertySSLPeerCertificates = {type = immutable,count = 3,values =( 0: 1: 2: }}}},NSLocalizedDescription =此服务器的证书无效。您可能连接到假冒“www.googleapis.com”的服务器,这可能会使您的机密信息面临风险。,NSErrorFailingURLKey = https://www.googleapis.com/youtube/v3/search?videoEmbeddable=true&videoType=any&videoDefinition=any&order=relevance&part=snippet&videoDimension=any&q=lets+play+berlin&videoCaption=closedCaption&videoLicense=any&videoSyndicated=true&type=video&videoDuration=short&maxResults=10&key= {API-KEY},NSErrorFailingURLStringKey = https://www.googleapis.com/youtube/v3/search?videoEmbeddable=true&videoType=any&videoDefinition=any&order=relevance&part=snippet&videoDimension=any&q=lets+play+berlin&videoCaption=closedCaption&videoLicense=any&videoSyndicated=true&type=video&videoDuration=short&maxResults=10&key= { API-KEY},NSErrorClientCertificateStateKey = 0})
然而,我无法解决这个问题。我在HTTPS服务器信任评估上找到了苹果技术说明TN2232,但坦率地说了很多工作,而我不确定我是否只是忘记了一个简单的附加内容。
我了解到我可以禁用服务器信任。但这对我来说不是一个选择,因为该应用程序应该在Apple Store中应用。
有人遇到同样的问题吗?或者有人知道该怎么做以解决问题?
亲切的问候!