I have this strange problem in which the newly created one is instantly canceled URLSessionUploadTask. I'm not sure if this is a bug with the current beta version of Xcode 8.
I suspect that this may be a mistake, because the code I'm going to publish went through exactly exactly once. After that, no changes were made, and then it just stopped working. Yes, he literally ran once, and then he stopped working. I will send an error at the end.
I will post the code below, but first I will describe how this logic works.
My test or user API (IE for use in playgrounds or directly in applications) calls the method authorize. This method authorize, in turn, will call buildPOSTTaskwhich will build a valid URL and return URLSessionUploadTaskwhich will be used by the method authorize.
With that said, the code below:
Session:
internal let urlSession = URLSession(configuration: .default)
Function to create a download task:
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
}
An authentication function that uses the task created by the above function:
public func authorize(withCode code: String?, completion: AccessTokenExchangeCompletionHandler) {
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()
}
I caught this error in a test:
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)
}
}
An error always occurs with this error:
= NSURLErrorDomain Code = -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 & = 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}
, :
, :
- , , , reset . .
- Mac...
- , - , ,
cancel, authorize, , buildPOSTTask, . .
, ( , ):
- . iOS 10 iPad, iOS 10. EDIT: , .
, . , , .
EDIT:
. , , API, , .
ALCKit