gpt4 book ai didi

ios - 无法通过api调用登录并且无法保存访问id

转载 作者:行者123 更新时间:2023-11-30 12:57:38 25 4
gpt4 key购买 nike

这是我正在开发的第一个应用程序。我有一个用于登录调用功能的 api url。当我输入用户名、密码时。它将生成一个客户 ID。我需要保存该 ID,并且我必须在所有屏幕上使用该特定 ID,直到用户注销为止。

但是当我执行 api 调用登录时。它不起作用。请帮帮我。

这是我的参数传递:

{
"username" : "u@gmail.com",
"password" : "u123"
}

登录 api 调用后我的 json 输出:

{
"status": 1,
"message": "Login success.",
"CustomerDetails": {
"CustomerId": "1",
"CustomerName": "u",
"CustomerEmail": "u@gmail.com",
"CustomerMobile": "901",
"CustomerAddress": "#45, 7th main road."
}
}

在此,我需要保存 CustomerId 并且我必须在我的所有屏幕上使用该 CustomerId

点击登录按钮时我的 api 调用功能:

  func getcartdetaildata () {
let headers = [
"cache-control": "no-cache",
"postman-token": "4c933910-0da0-b199-257b-28fb0b5a89ec"
]

let jsonObj:Dictionary<String, Any> = [

"username" : "\(UsernameEmail)",
"password" : "\(Password)"

]


if (!JSONSerialization.isValidJSONObject(jsonObj)) {
print("is not a valid json object")
return
}

if let postData = try? JSONSerialization.data(withJSONObject: jsonObj, options: JSONSerialization.WritingOptions.prettyPrinted) {
let request = NSMutableURLRequest(url: NSURL(string: "http://Login.php")! as URL,
cachePolicy: .useProtocolCachePolicy,timeoutInterval: 10.0)
request.httpMethod = "POST"
request.allHTTPHeaderFields = headers
request.httpBody = postData

let session = URLSession.shared
let dataTask = session.dataTask(with: request as URLRequest, completionHandler: { (data, response, error) -> Void in
if (error != nil) {
///print(error)
} else {

print("123.......... ")

DispatchQueue.main.async(execute: {

if let json = (try? JSONSerialization.jsonObject(with: data!, options: [])) as? Dictionary<String,AnyObject>
{

print(json)

let status = json["status"] as? Int;
if(status == 1)
{
print("asdasdasx.......... ")

// let access_token = json["CustomerId"]

//print(access_token)
DispatchQueue.main.async(execute: {

//
//
// //Set logged in to true
// UserDefaults.standard.set(true, forKey: "ISLOGGEDIN")
//
// //Set access token
// UserDefaults.standard.setValue(access_token, forKey: "CustomerId")
//
// UserDefaults.standard.synchronize()
//


})

}
}
})
}
})

dataTask.resume()
}
}

请帮帮我。

谢谢

最佳答案

func apicalling () {

let Username:NSString = EmailTextField.text! as NSString
let password:NSString = PasswordTextField.text! as NSString

let headers = [
"content-type": "application/json",
"cache-control": "no-cache",
"postman-token": "4c933910-0da0-b199-257b-28fb0b5a89ec"
]
let parameters = [
"username": "\(Username)",
"password": "\(password)"
]
do {
let postData = try JSONSerialization.data(withJSONObject: parameters, options :[])
let request = NSMutableURLRequest(url: NSURL(string: "http://Login.php")! as URL,cachePolicy: .useProtocolCachePolicy,timeoutInterval: 10.0)
request.httpMethod = "POST"
request.allHTTPHeaderFields = headers
request.httpBody = postData

let session = URLSession.shared
let dataTask = session.dataTask(with: request as URLRequest, completionHandler: { (data, response, error) -> Void in
if (error != nil) {
///print(error)
} else {

DispatchQueue.main.async(execute: {

if let json = (try? JSONSerialization.jsonObject(with: data!, options: [])) as? Dictionary<String,AnyObject>
{
let status = json["status"] as? Int;
if(status == 1)
{
print(json)
}

}
})
}
})

dataTask.resume()



} catch {
// print("JSON serialization failed: \(error)")
}
}

在 NSUserDefaults 中保存数据的代码。

let userDefaults = NSUserDefaults.standardUserDefaults()
userDefaults.setValue(YOUR_VALUE, forKey: "PASSKEY")
userDefaults.synchronize() // don't forgot this line

从 NSUserDefaults 检索数据

if let VARIABLE = userDefaults.valueForKey("PASSKEY") {
// do something here when a Data exists
}
else {
// no data exists
}

关于ios - 无法通过api调用登录并且无法保存访问id,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40240912/

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