如何使用 Swift 将带有 JSON 数据的 POST 请求发送到外部数据库以供 IOS 中的登录应用程序使用?
How to send POST request with JSON data to external database for login app in IOS using Swift?
我是 Swift 和 iOS 的新人。请将 POST
请求的示例代码发送给我 JSON
数据到外部数据库,以便在 iOS 中使用 Swift
登录应用程序
您可以像下面的代码那样调用post api,
试试这个
func callPostApi()
{
let baseUrl : NSString = NSString(format: "YOUR_BASE_URL") //"http://at.webby.com/php.api"
let request = NSMutableURLRequest(URL: NSURL(string: baseUrl as String)!)
let session = NSURLSession.sharedSession()
request.HTTPMethod = "POST"
let stringPost = "username=test&password=12345" // Key and Value param as string
let data = stringPost.dataUsingEncoding(NSUTF8StringEncoding)
request.timeoutInterval = 60
request.HTTPBody=data
let task = session.dataTaskWithRequest(request, completionHandler: {data, response, error -> Void in
let err1: NSError? = nil
do
{
let json = try NSJSONSerialization.JSONObjectWithData(data!, options: NSJSONReadingOptions.MutableContainers)
print(json)
}
catch
{
print(err1)
}
})
task.resume()
}
希望对您有所帮助。
class ViewController: UIViewController {
@IBOutlet weak var tblView: UITableView!
var arrData = NSArray()
override func viewDidLoad() {
super.viewDidLoad()
postAPICall()
}
func postAPICall() {
let urlString = "http://18.220.215.90/backend/web/index.php/site/getstylistlist"
let dictParameter = ["Salon_Latitude": "23.0387822",
"Register_User_ID": "0",
"page": "0",
"Filter_Keyword": "",
"Register_User_Year_Of_Experience": "0",
"Salon_Longitude": "72.514567",
"pagesize": "20",
"api_type": "iphone",
"Register_User_Login_ID": "0",
"Cost": "0",
"Version": "1",
"api_userid": "",
"Distance_KM": "",
"Register_User_Professional_Sub_Type": "All"
]
let param = JsonStringFromDictionary(parameter: dictParameter as AnyObject, type: "json")
Alamofire.request(urlString, method: .post, parameters: param, encoding: URLEncoding.httpBody, headers: nil).responseJSON { (response:DataResponse!) in
switch response.result{
case.success:
print("Sucess")
if let JSON = response.result.value {
self.arrData = JSON as! NSArray
print(self.arrData)
}
case.failure(let Error):
print("error\(Error)")
}
}
}
我是 Swift 和 iOS 的新人。请将 POST
请求的示例代码发送给我 JSON
数据到外部数据库,以便在 iOS 中使用 Swift
您可以像下面的代码那样调用post api,
试试这个
func callPostApi()
{
let baseUrl : NSString = NSString(format: "YOUR_BASE_URL") //"http://at.webby.com/php.api"
let request = NSMutableURLRequest(URL: NSURL(string: baseUrl as String)!)
let session = NSURLSession.sharedSession()
request.HTTPMethod = "POST"
let stringPost = "username=test&password=12345" // Key and Value param as string
let data = stringPost.dataUsingEncoding(NSUTF8StringEncoding)
request.timeoutInterval = 60
request.HTTPBody=data
let task = session.dataTaskWithRequest(request, completionHandler: {data, response, error -> Void in
let err1: NSError? = nil
do
{
let json = try NSJSONSerialization.JSONObjectWithData(data!, options: NSJSONReadingOptions.MutableContainers)
print(json)
}
catch
{
print(err1)
}
})
task.resume()
}
希望对您有所帮助。
class ViewController: UIViewController {
@IBOutlet weak var tblView: UITableView!
var arrData = NSArray()
override func viewDidLoad() {
super.viewDidLoad()
postAPICall()
}
func postAPICall() {
let urlString = "http://18.220.215.90/backend/web/index.php/site/getstylistlist"
let dictParameter = ["Salon_Latitude": "23.0387822",
"Register_User_ID": "0",
"page": "0",
"Filter_Keyword": "",
"Register_User_Year_Of_Experience": "0",
"Salon_Longitude": "72.514567",
"pagesize": "20",
"api_type": "iphone",
"Register_User_Login_ID": "0",
"Cost": "0",
"Version": "1",
"api_userid": "",
"Distance_KM": "",
"Register_User_Professional_Sub_Type": "All"
]
let param = JsonStringFromDictionary(parameter: dictParameter as AnyObject, type: "json")
Alamofire.request(urlString, method: .post, parameters: param, encoding: URLEncoding.httpBody, headers: nil).responseJSON { (response:DataResponse!) in
switch response.result{
case.success:
print("Sucess")
if let JSON = response.result.value {
self.arrData = JSON as! NSArray
print(self.arrData)
}
case.failure(let Error):
print("error\(Error)")
}
} }