And*_*nez 6 ios nsurlsession nsurlsessionuploadtask swift3 xcode8
我有这个奇怪的问题,其中新创建的内容URLSessionUploadTask立即被取消.我不确定它是否是目前Xcode 8测试版的一个错误.
我怀疑这可能是一个错误,因为我即将发布的代码只运行一次.之后没有对它进行任何更改,然后它就停止了工作.是的,它实际上只运行一次,然后停止工作.我会在最后发布错误.
我将在下面发布代码,但首先我将总结这里的逻辑是如何工作的.
我的测试或用户公开的API(用于Playgrounds或直接在应用程序中使用的IE)调用该authorize方法.此authorize方法将依次调用buildPOSTTask,该方法将构造有效的URL并返回URLSessionUploadTask该authorize方法使用的URL .
话虽如此,代码如下:
会议:
internal let urlSession = URLSession(configuration: .default)
Run Code Online (Sandbox Code Playgroud)
创建上传任务的功能:
internal func buildPOSTTask(onURLSession urlSession: URLSession, appendingPath path: String, withPostParameters postParams: [String : String]?, getParameters getParams: [String : String]?, httpHeaders: [String : String]?, completionHandler completion: URLSessionUploadTaskCompletionHandler) -> URLSessionUploadTask {
let fullURL: URL
if let gets = getParams {
fullURL = buildURL(appendingPath: path, withGetParameters: gets)
} else {
fullURL = URL(string: path, relativeTo: baseURL)!
}
var request = URLRequest(url: fullURL)
request.httpMethod = "POST"
var postParameters: Data? = nil
if let posts = postParams {
do {
postParameters = try JSONSerialization.data(withJSONObject: posts, options: [])
} catch let error as NSError {
fatalError("[\(#function) \(#line)]: Could not build POST task: \(error.localizedDescription)")
}
}
let postTask = urlSession.uploadTask(with: request, from: postParameters, completionHandler: completion)
return postTask
}
Run Code Online (Sandbox Code Playgroud)
身份验证功能,使用上述功能创建的任务:
public func authorize(withCode code: String?, completion: AccessTokenExchangeCompletionHandler) {
// I have removed a lot of irrelevant code here, such as the dictionary building code, to make this snippet shorter.
let obtainTokenTask = buildPOSTTask(onURLSession: self.urlSession, appendingPath: "auth/access_token", withPostParameters: nil, getParameters: body, httpHeaders: nil) { (data, response, error) in
if let err = error {
completion(error: err)
} else {
print("Response is \(response)")
completion(error: nil)
}
}
obtainTokenTask.resume()
}
Run Code Online (Sandbox Code Playgroud)
我在测试中发现了这个错误:
let testUser = Anilist(grantType: grant, name: "Test Session")
let exp = expectation(withDescription: "Waiting for authorization")
testUser.authorize(withCode: "a valid code") { (error) in
if let er = error {
XCTFail("Authentication error: \(er.localizedDescription)")
}
exp.fulfill()
}
self.waitForExpectations(withTimeout: 5) { (err) in
if let error = err {
XCTFail(error.localizedDescription)
}
}
Run Code Online (Sandbox Code Playgroud)
它始终会因此错误而立即失败:
误差区域= NSURLErrorDomain代码= -999 "取消"的UserInfo = {NSErrorFailingURLKey = https://anilist.co/api/auth/access_token?client_secret=REMOVED&grant_type=authorization_code&redirect_uri=genericwebsitethatshouldntexist.bo&client_id=ibanez-hod6w&code=REMOVED,NSLocalizedDescription =取消, NSErrorFailingURLStringKey = https://anilist.co/api/auth/access_token?client_secret=REMOVED&grant_type=authorization_code&redirect_uri=genericwebsitethatshouldntexist.bo&client_id=ibanez-hod6w&code=REMOVED }
以下是一些要记住的事项:
这是我尝试的一系列事项:
cancel,我还重写authorize了返回创建的任务buildPOSTTask并将其分配给我的测试中的变量.任务仍然被取消.我还没有尝试的事情(但是当我解决这些问题时,我会接受任何其他想法):
我不知道该尝试什么.生成的日志似乎没有任何有用的信息.
我决定在这里发布整个项目.完成时,事情将是开源的,我获得的API凭据是针对测试应用程序的.
经过6天不间断的挣扎,以及谷歌搜索不停的解决方案后,我很高兴地说我终于明白了.
事实证明,无论出于什么神秘的原因,from:参数uploadTask(with:from:completionHandler)都不能为零.尽管参数被标记为可选参数Data,但它在缺失时会立即被取消.这可能是Apple方面的一个错误,当我无法使其工作时我打开了一个错误,因此我将使用这些新信息更新我的错误报告.
话虽如此,我所要做的就是更新我的buildPOSTTask方法,以解释传递的字典为零的可能性.有了它,它现在工作正常:
internal func buildPOSTTask(onURLSession urlSession: URLSession, appendingPath path: String, withPostParameters postParams: [String : String]?, getParameters getParams: [String : String]?, httpHeaders: [String : String]?, completionHandler completion: URLSessionUploadTaskCompletionHandler) -> URLSessionUploadTask {
let fullURL: URL
if let gets = getParams {
fullURL = buildURL(appendingPath: path, withGetParameters: gets)
} else {
fullURL = URL(string: path, relativeTo: baseURL)!
}
var request = URLRequest(url: fullURL)
request.httpMethod = "POST"
var postParameters: Data
if let posts = postParams {
do {
postParameters = try JSONSerialization.data(withJSONObject: posts, options: [])
} catch let error as NSError {
fatalError("[\(#function) \(#line)]: Could not build POST task: \(error.localizedDescription)")
}
} else {
postParameters = Data()
}
let postTask = urlSession.uploadTask(with: request, from: postParameters, completionHandler: completion)
return postTask
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
10649 次 |
| 最近记录: |