gpt4 book ai didi

ios - NSURLSession.sharedSession().dataTaskWithRequest(request) 最后执行

转载 作者:行者123 更新时间:2023-11-28 11:04:41 27 4
gpt4 key购买 nike

我在使用 NSURLSession.sharedSession().dataTaskWithRequest(request) 时遇到问题。我想根据某些 API 对我的应用程序进行身份验证,如果身份验证成功,则该应用程序将允许用户授权健康工具包并最终从那里获取一些数据。

我的 viewDidLoad() 中有这个

print ("AUTHORIZING APIGEE!!!")
authorizeApigee()
if (self.errorApigee == 0) {
print ("APIGEE AUTHORIZED!")
// We cannot access the user's HealthKit data without specific permission.
print ("AUTHORIZING HEALTHKIT!!")
getHealthKitPermission()
print ("HEALTHKIT AUTORIZED!")
} else {
print ("APIGEE UNAUTHORIZED!")
}

这是授权函数:

// Authenticating app with Apigee Health APIx
func authorizeApigee(){
// Send HTTP GET Request


let scriptUrl = "https://fhirsandbox-prod.apigee.net/oauth/v2"
let urlWithParams = scriptUrl + "/accesstoken?grant_type=client_credentials"
let myUrl = NSURL(string: urlWithParams);

let request = NSMutableURLRequest(URL:myUrl!);
request.HTTPMethod = "POST"

// Add Basic Authorization

let username = "****"
let password = "****"
let loginString = NSString(format: "%@:%@", username, password)
let loginData: NSData = loginString.dataUsingEncoding(NSUTF8StringEncoding)!
let base64LoginString = loginData.base64EncodedStringWithOptions(NSDataBase64EncodingOptions())
request.setValue(base64LoginString, forHTTPHeaderField: "Authorization")

let task = NSURLSession.sharedSession().dataTaskWithRequest(request) {
data, response, error in

// Check for error
if error != nil
{
self.errorApigee = 1
print("error=\(error)")
return
}

// Print out response string
let responseString = NSString(data: data!, encoding: NSUTF8StringEncoding)
print("responseString = \(responseString)")
}
task.resume()
}

问题是,即使 authorizeApigee() 被调用,应用程序也会停止在 NSURLSession.sharedSession().dataTaskWithRequest(request) 处,退出函数,显然 errorApigee 变量没有更新,因此它进入下一步授权健康工具包而不授权 Apigee。最后,当健康套件获得授权后,它会返回并授权 Apigee。

知道为什么会发生这种情况吗?任何提示将不胜感激!

最佳答案

这是一个如何使用 completion 的简单示例:

func authorizeApigee(completion: (auth: Bool) -> Void) {
// Send HTTP GET Request
let scriptUrl = "https://fhirsandbox-prod.apigee.net/oauth/v2"
let urlWithParams = scriptUrl + "/accesstoken?grant_type=client_credentials"
let myUrl = NSURL(string: urlWithParams);

let request = NSMutableURLRequest(URL:myUrl!);
request.HTTPMethod = "POST"

// Add Basic Authorization

let username = "****"
let password = "****"
let loginString = NSString(format: "%@:%@", username, password)
let loginData: NSData = loginString.dataUsingEncoding(NSUTF8StringEncoding)!
let base64LoginString = loginData.base64EncodedStringWithOptions(NSDataBase64EncodingOptions())
request.setValue(base64LoginString, forHTTPHeaderField: "Authorization")

let task = NSURLSession.sharedSession().dataTaskWithRequest(request) {
data, response, error in

// Check for error
if error != nil
{
self.errorApigee = 1
print("error=\(error)")
completion(auth: false)
}

// Print out response string
let responseString = NSString(data: data!, encoding: NSUTF8StringEncoding)
print("responseString = \(responseString)")
completion(auth: true)
}
task.resume()
}

以及如何使用:

 self.authorizeApigee { (auth) -> Void in
if (auth) {
getHealthKitPermission()
} else {
//print errors
}
}

关于ios - NSURLSession.sharedSession().dataTaskWithRequest(request) 最后执行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38639216/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com