Not working - Registering User in Api Programatically in Swift 3/4 - ios

I'm trying to register a user in my API. When I do it in Postman, it registers the user and returns the status as true and message as User Created, but when I try to create a new user from swift programatically it always shows User already exists (giving false response), even though it is not registered. I'm Using Swift 4 Xcode 9.
The almost same code work works for Sign In Api, but not working for User Registration. Also, after I register that user through postman in Api, it works perfectly fine for Login. So I'm not getting what the problem is with RegisterUser. Code shows no error.
Here is my code for registeringUser:
import UIKit
import SwiftyJSON
import Alamofire
import SwiftKeychainWrapper
class RegisterUserViewController: UIViewController {
#IBOutlet weak var firstNameTextField: UITextField!
#IBOutlet weak var lastNameTextField: UITextField!
#IBOutlet weak var emailAddressTextField: UITextField!
#IBOutlet weak var passwordTextField: UITextField!
#IBOutlet weak var repeatPasswordTextField: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
#IBAction func cancelButtonTapped(_ sender: Any) {
print("Cancel button tapped")
self.dismiss(animated: true, completion: nil)
}
#IBAction func signupButtonTapped(_ sender: Any) {
print("Sign up button tapped")
// Validate required fields are not empty
if (firstNameTextField.text?.isEmpty)! ||
(lastNameTextField.text?.isEmpty)! ||
(emailAddressTextField.text?.isEmpty)! ||
(passwordTextField.text?.isEmpty)!
{
// Display Alert message and return
displayMessage(userMessage: "All fields are quired to fill in")
return
}
// Validate password
if ((passwordTextField.text?.elementsEqual(repeatPasswordTextField.text!))! != true)
{
// Display alert message and return
displayMessage(userMessage: "Please make sure that passwords match")
return
}
let userdefault = UserDefaults.standard
userdefault.set(emailAddressTextField.text, forKey: "email_id")
//userdefault.set(emailAddressTextField, forKey: "email_id")
print("email_id",emailAddressTextField.text)
//print("email_address",userdefault.string(forKey: "email_id"))
var request = URLRequest(url: URL(string: "http://horn.hostingduty.com/api/v1/app_adduser")!)
request.httpMethod = "POST"
print("its working")
let postString = "first_name=\(firstNameTextField.text)";
"last_name=\(lastNameTextField.text)";
"email_id=\(emailAddressTextField.text)";
"password=\(passwordTextField.text)"
request.httpBody = postString.data(using: .utf8)
let task = URLSession.shared.dataTask(with: request) { data, response, error in
guard let data = data, error == nil else { // check for fundamental networking error
print("error=\(error)")
return
}
if let httpStatus = response as? HTTPURLResponse, httpStatus.statusCode != 200 { // check for http errors
print("statusCode should be 200, but is \(httpStatus.statusCode)")
print("response = \(response)")
}
var responseString = String(data: data, encoding: .utf8)
print("responseString = \(responseString)")
if(responseString?.contains("true"))!{
DispatchQueue.main.async
{
let homePage = self.storyboard?.instantiateViewController(withIdentifier: "HomePageViewController") as! SWRevealViewController
let appDelegate = UIApplication.shared.delegate
appDelegate?.window??.rootViewController = homePage
}
print("status = true")
}
else{
DispatchQueue.main.async
{
self.showToast(message: "Already exists !! ")
}
print("Status = false")
}
}
task.resume()
}
func displayMessage(userMessage:String) -> Void {
DispatchQueue.main.async
{}
let alertController = UIAlertController(title: "Alert", message: userMessage, preferredStyle: .alert)
let OKAction = UIAlertAction(title: "OK", style: .default) { (action:UIAlertAction!) in
// Code in this block will trigger when OK button tapped.
print("Ok button tapped")
DispatchQueue.main.async
{
self.dismiss(animated: true, completion: nil)
}
}
alertController.addAction(OKAction)
self.present(alertController, animated: true, completion:nil)
}
}

func loginApi() {
let userName = userNameText?.text ?? ""
print(userName)
let password = passwordText?.text ?? ""
print(password)
let params = [
"emailId" : userName as Any,
"password" : password as Any,
]
Alamofire.SessionManager.default.request("your url", method: .post, parameters: params, encoding: URLEncoding(destination: .methodDependent))
.validate(statusCode: [200, 201])
.responseJSON {
[unowned self] (response) in
switch(response.result) {
case .success:
guard let json = response.result.value as!
[String:Any]? else{ return}
print("Response \(json)")
if let data = json["data"] as! [String:Any]?{
let email_id : String = email_id
userdefault.set(emailAddressTextField.text, forKey: "email_id")
print(email_id)
UserDefaults.standard.synchronize()
let homePage = self.storyboard?.instantiateViewController(withIdentifier: "HomePageViewController") as! SWRevealViewController
let appDelegate = UIApplication.shared.delegate
appDelegate?.window??.rootViewController = homePage
}
}
}
case .failure(let err):
self.showNormalAlertWithTitle(NSLocalizedString("Alert!", comment: ""), message:NSLocalizedString("Please enter valid username or password", comment: ""))
print("\(err.localizedDescription)")
break
}
}
}

You should add header to your request and send proper url encoded key/values
request.setValue("application/x-www-form-urlencoded; charset=utf-8", forHTTPHeaderField: "Content-Type");
And change your post string as
let postString = "first_name=\(firstNameTextField.text)&" +
"last_name=\(lastNameTextField.text)&" +
"email_id=\(emailAddressTextField.text)&" +
"password=\(passwordTextField.text)"
request.httpBody = postString.data(using: .utf8)
UPDATE
func callApi(){
var request = URLRequest(url: URL(string: "http://horn.hostingduty.com/api/v1/app_adduser")!)
request.httpMethod = "post"
request.setValue("application/x-www-form-urlencoded; charset=utf-8", forHTTPHeaderField: "Content-Type")
print("its working")
let postString = "first_name=test&" +
"last_name=test&" +
"email_id=test#abc.vom&" +
"password=123&"
request.httpBody = postString.data(using: .utf8)
let task = URLSession.shared.dataTask(with: request) { data, response, error in
guard let data = data, error == nil else { // check for fundamental networking error
print("error=\(error?.localizedDescription)")
return
}
if let httpStatus = response as? HTTPURLResponse, httpStatus.statusCode != 200 { // check for http errors
print("statusCode should be 200, but is \(httpStatus.statusCode)")
print("response = \(response)")
}
var responseString = String(data: data, encoding: .utf8)
print("responseString = \(responseString)")
if(responseString?.contains("true"))!{
print("status = true")
}
else{
print("Status = false")
}
}
task.resume()
}

Related

calling a web service with httpMethod (POST) in swift 4.1

Let me describe view controller (page design) first. I have added one button and one label. And a function is created by me for the button. Here is the ViewController.swift file:
import UIKit
class ViewController: UIViewController {
#IBOutlet weak var btnHitWebService: UIButton!
#IBOutlet weak var lblResult: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
}
#IBAction func btnHitWebServiceClick(_ sender: Any) {
if (getTokenWithPostMethod() == true)
{
lblResult.text = "yes, we get token: " + g_token
}
else
{
lblResult.text = "there is an error, please try again later"
}
}
func getTokenWithPostMethod() -> Bool{
var funcResult = false
var request = URLRequest(url: URL(string: "https://apiABCDEFG.com/token")!)
request.httpMethod = "POST"
let postString = "grant_type=password&username=ABCDEF&password=ABCDEF"
request.httpBody = postString.data(using: .utf8)
request.addValue("application/x-www-form-urlencoded", forHTTPHeaderField: "Content-Type")
request.addValue("application/json", forHTTPHeaderField: "Accept")
let task = URLSession.shared.dataTask(with: request)
{
data, response, error in
guard let data = data, error == nil else
{
// check for fundamental networking error
print("error=\(String(describing: error))")
return
}
if let httpStatus = response as? HTTPURLResponse, httpStatus.statusCode != 200
{
// check for http errors
print("statusCode should be 200, but is \(httpStatus.statusCode)")
print("response = \(String(describing: response))")
}
do
{
if let jsonObject = try JSONSerialization.jsonObject(with: data, options: []) as? [String:AnyObject]
{
g_token = (jsonObject["access_token"] as? String)!
print(g_token)
g_token_type = (jsonObject["token_type"] as? String)!
print("\n")
print(g_token_type)
funcResult = true ;
}
}
catch let error as NSError
{
print(error)
}
}
task.resume()
return funcResult
}
}
Here is my problem: When I click button, it gives me a token in output screen in Xcode (print) but getTokenWithPostMethod() method returns false. Why does it return false? During the debug process, it jumps from let task = to task.resume(). Thanks! (by the way I can't share correct link, username and password. I hope these 3 are not necessary to fix this problem)
You are making an API call. It takes time to return the value. Here return statement will not wait for response. you should use either closure or delegate. You can try the given code:
func getTokenWithPostMethod(handler: #escaping (_ funcResult: Bool) -> Void){
var request = URLRequest(url: URL(string: "https://apiABCDEFG.com/token")!)
request.httpMethod = "POST"
let postString = "grant_type=password&username=ABCDEF&password=ABCDEF"
request.httpBody = postString.data(using: .utf8)
request.addValue("application/x-www-form-urlencoded", forHTTPHeaderField: "Content-Type")
request.addValue("application/json", forHTTPHeaderField: "Accept")
let task = URLSession.shared.dataTask(with: request)
{
data, response, error in
var funcResult = false
if error == nil, let data = data {
do
{
if let jsonObject = try JSONSerialization.jsonObject(with: data, options: []) as? [String:AnyObject]
{
g_token = (jsonObject["access_token"] as? String)!
print(g_token)
g_token_type = (jsonObject["token_type"] as? String)!
print("\n")
print(g_token_type)
funcResult = true
}
}
catch let error as NSError
{
print(error)
}
}
// edited
DispatchQueue.main.async {
handler(funcResult)
}
}
task.resume()
}
This will return after the response comes. Replace the calling of that method with :
#IBAction func btnHitWebServiceClick(_ sender: Any) {
getTokenWithPostMethod { (flag) in
if flag {
lblResult.text = "yes, we get token: " + g_token
} else {
lblResult.text = "there is an error, please try again later"
}
}
}
For more details please visit
It is returning false because, you have set var funcResult = false in starting, so before URLSession request to server and return some data in response only, your function returns the funcResult value.
you can add below code inside URLSession task body, immediately after try catch, instead of adding it inside btnHitWebServiceClick :
if (getTokenWithPostMethod() == true)
{
lblResult.text = "yes, we get token: " + g_token
}
else
{
lblResult.text = "there is an error, please try again later"
}

My function returns String but I can not parse it ? any idea why?

I have craeted a class called db_conn and inside this calass I have the following functions :
public func login (email : String , password : String ) -> String {
var result = ""
let url = URL(string: API.url_login.rawValue)!
var request = URLRequest(url: url)
request.setValue("application/x-www-form-urlencoded", forHTTPHeaderField: "Content-Type")
request.httpMethod = "POST"
let postString = "email=\(email)&password=\(password)"
request.httpBody = postString.data(using: .utf8)
let task = URLSession.shared.dataTask(with: request) { data, response, error in
guard let data = data, error == nil else {
print("error=\(String(describing: error))")
return
}
if let httpStatus = response as? HTTPURLResponse, httpStatus.statusCode != 200 {
print("statusCode should be 200, but is \(httpStatus.statusCode)")
print("\(String(describing: response))")
}
let responseString = String(data: data, encoding: .utf8)
result = String(responseString!)
//print("responseString = \(responseString!)")
}
task.resume()
return result
}
and then in my login viewController class I try to use it like ==>
#IBAction func login_button_action(_ sender: UIButton) {
let email = email_text.text
let passw0rd = password_text.text
if (email != "" && passw0rd != ""){
let db_connect = db_conn()
if (db_connect.login(email: email!, password: passw0rd!) == nil ){
print("it's empty ")
}else {
print("not empty \(db_connect.login(email: email!, password: passw0rd!))")
}
}else {
let alert = UIAlertController(title: "Error", message: "Dont leave any of the spaces blank , they all must be filled up , try again ! ", preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "Ok", style: UIAlertActionStyle.default, handler: nil))
self.present(alert, animated: true, completion: nil)
}
when I run the project it says that the value for db_connect.login(email: email!, password: passw0rd!) is not empty but it doesnt print it out .
(However in my db_conn calss I can easily print the responseString and I get the returned JSON in String format .
Any idea where am I making my mistake ? And how can I possibly fix it ?
Making a network request with URLSession runs asynchronously, which means that iOS put it onto a background thread and continues the current thread immediately before the download is finished. Therefore, when you return result, it's still an empty string. What you probably want to do is pass a completion closure to your login method, and call that with the relevant data when it's finished downloading

Unable to Json response is not working

Swift 3.0 (Xcode 8.3)
I'm trying to make a small program, that send a username to a web data base. I have found out how to send first_name, last_name,dob,owner_mobile,owner_email,
owner_password,choice_for_verification, by using in POST method. I obtain a JSON string earlier, then when I try to parse it, I get the above error on the try NSJSONSerialization line, on the as keyword. What did I do wrong? Thanks for your answers.
#IBAction func Login_Action(_ sender: Any)
{
var responseString : String!
var request = URLRequest(url: URL(string: "http://dev.justpick2go.com/cpanel/api/owner/ownerregistration.php")!)
request.setValue("Application/x-www.ownerregistration.php.com", forHTTPHeaderField: "Content-Type")
request.httpMethod = "POST"
let postString = "first_name=\(txtFirstName.text!)&last_name=\(txtLastName.text!)&dob=\(txtDOB.text!)&owner_mobile=\(txtMobileNo.text!)&owner_email=\(txtEmailID.text!)&owner_password=\(txtPassword.text!)" //&choice_for_verification=\(email)" // sending a parameters
print("\(postString)")
request.httpBody = postString.data(using: .utf8)
let task = URLSession.shared.dataTask(with: request) { (data, response, error) in
guard let data = data, error != nil else { //checking for fundamental error
print("Error is =\(String(describing: error))")
return
}
if let httpStatus = response as? HTTPURLResponse, httpStatus.statusCode != 200
{ // checking for http errors
print("statusCode should be 200 , but is\(httpStatus.statusCode)")
print("response is =\(String(describing: response))")
}
responseString = String(data: data, encoding: .utf8)
print("ResponseString=\(responseString!)")
do {
let json : NSDictionary! = try! JSONSerialization.jsonObject(with: data, options: .mutableContainers) as! NSDictionary
self.parseTheJSonData(JsonData: json)
}
catch
{
print(error)
}
}
task.resume()
}
func parseTheJSonData(JsonData : NSDictionary)
{
var successMessage : String = String()
var sampleCode : Int = Int()
let verificationAlert = UIAlertController()
if ((JsonData.value(forKey: "success") as! Int) == 1)
{
successMessage = "Login is Successful"
sampleCode = JsonData.value(forKey: "success") as! Int
verificationAlert.addAction(UIAlertAction(title: "No", style: .cancel, handler: nil))
verificationAlert.addAction(UIAlertAction(title: "Yes", style: .default, handler: { (Relogin) in
let Log = self.storyboard?.instantiateViewController(withIdentifier: "") as! LoginViewController
self.navigationController?.pushViewController(Log, animated: true)
self.present(Log, animated: true, completion: nil)
}))
}
else if ((JsonData.value(forKey: "success") as! Int) == 0)
{
sampleCode = JsonData.value(forKey: "success") as! Int
successMessage = "Please try again"
verificationAlert.addAction(UIAlertAction(title: "OK", style: .cancel, handler: nil))
}
verificationAlert.title = successMessage
OperationQueue.main.addOperation
{
self.present(verificationAlert, animated: true, completion: nil)
}
}

how to get JSON data from login?

I am able to fetch data using Oauth in Alamofire for login successfully but how to get JSON data in Another viewController.
func loginAccess(){
let headers = [
"Content-Type": "application/x-www-form-urlencoded"
]
let parameters = [
"UserName": username_textfield.text! as String,
"Password": password_textfield.text! as String,
"grant_type": "password",
]
// let url = NSURL(string: "http://192.168.100.5:84/Token")!
Alamofire.request("http://192.168.100.5:84/Token", method: .post, parameters: parameters, encoding: URLEncoding.httpBody, headers: headers).responseJSON { (response:DataResponse<Any>) in
switch(response.result) {
case .success(_):
if response.result.value != nil{
let statusCode = (response.response?.statusCode)!
print("...HTTP code: \(statusCode)")
if let JSON = response.result.value {
print("JSON: \(JSON)")
// }
}
if statusCode == 200{
self.view.makeToast(message: "Welcome !!")
self.performSegue(withIdentifier: "mainview", sender: self)
}else{
self.view.makeToast(message: "Username or password invalid")
}
}
break
case .failure(_):
print(response.result.error!)
break
}
}
}
modal class
class Login{
var access_token = String()
var token_type = String()
var expire_in = String()
var username = String()
var masterid = String()
var name = String()
var access = String()
var issued = String()
var expries = String()
init(loginJson:JSON){
self.access_token = loginJson["access_token"].stringValue
self.token_type = loginJson["token_type"].stringValue
self.expire_in = loginJson["expires_in"].stringValue
self.username = loginJson["userName"].stringValue
self.masterid = loginJson["MasterID"].stringValue
self.name = loginJson["Name"].stringValue
self.access = loginJson["Access"].stringValue
self.issued = loginJson[".issued"].stringValue
self.expries = loginJson[".expires"].stringValue
}
}
is this able to access JSON data in Another ViewController or we need another function? if this is possible in same function then how
it could be solved?
Replace this with your code
func getlogin(){
let headers = [
"Content-Type": "application/x-www-form-urlencoded"
]
let parameters = [
"UserName": username_textfield.text! as String,
"Password": password_textfield.text! as String,
"grant_type": "password",
]
// let url = NSURL(string: "http://192.168.100.5:84/Token")!
Alamofire.request("http://192.168.100.5:84/Token", method: .post, parameters: parameters, encoding: URLEncoding.httpBody, headers: headers).responseJSON { (response:DataResponse<Any>) in
switch(response.result) {
case.success(let data):
print("success",data)
let statusCode = (response.response?.statusCode)!
if statusCode == 200{
self.view.makeToast(message: "Welcome !!")
// self.performSegue(withIdentifier: "mainview", sender: self)
}else{
self.view.makeToast(message: "Username or password invalid")
}
self.myResponse = JSON(data)
let login = Login(loginJson: self.myResponse)
DispatchQueue.main.async(execute: { () -> Void in
self.performSegue(withIdentifier: "pass_data", sender: login)
})
case.failure(let error):
print("Not Success",error)
}
}
}
you can add this also
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "pass_data" {
if let eventsVC = segue.destination as? secondViewController,
let loginData = sender as? Login {
eventsVC.login_details = loginData
}
}
}
in your second view controller you shuold add variable like this var
login_details : Login? to get JSON data
You can pass value using closure .
Create Method
func loginAccess(callBack: (_ responseObject : [Any]) -> Void) -> Void{
if let JSON = response.result.value {
callBack(JSON as Any) // here data passed
}
}
if statusCode == 200{
self.view.makeToast(message: "Welcome !!")
self.performSegue(withIdentifier: "mainview", sender: self)
}else{
self.view.makeToast(message: "Username or password invalid")
callBack([:] as Any)
}
}
Calling method
self.loginAccess { (responseObject) in
print(responseObject) // this is your data
//create your model here
}
How to get data from this login?
func onlinecheckin(){
self.password.resignFirstResponder()
let password1 = password.text;
if let user1 = user.text {
// To trim the characters to remove the empty space
let username = user1.trimmingCharacters(in: CharacterSet.whitespaces)
let passwordnya = password1!.trimmingCharacters(in: CharacterSet.whitespaces)
//Send user data to server side
let myUrl = URL(string: "https://link/jgcm/login.php")!
var request = URLRequest(url: myUrl)
request.httpMethod = "POST"
let postString = "user=\(username) & password=\(passwordnya)"
request.httpBody = postString.data(using: String.Encoding.utf8)
task = URLSession.shared.dataTask(with: request as URLRequest) { data, response, error in
print("response online checking =\(String(describing: response))")
if error != nil {
task.cancel() // To cancel uploading the task.
self.AlertMessage("Error. Please press Login button again.");
print("error=\(String(describing: error))")
return
}
if let datadownload = data {
resultlogin = NSString(data: datadownload, encoding: String.Encoding.utf8.rawValue)!.replacingOccurrences(of: " ", with: "");
print("result of online checking:\(resultlogin)")
}
DispatchQueue.main.async(execute: { () -> Void in
if resultlogin.contains("already") {
let secondViewController = self.storyboard?.instantiateViewController(withIdentifier: "menu")
self.present(secondViewController!,animated: true,completion: nil)
} else {
self.AlertMessage("User atau Password Salah");
}
})
}
task.resume()
}}

Make REST API call in Swift

I'm trying to use Swift to make a GET call to a REST API, and have tried to follow numerous tutorials, but can't figure it out. Either because I cannot figure out how to translate all the Obj-C to Swift, or because half of the methods n' such are deprecated. Does anyone know how to make the call, and parse returned JSON data?
Swift 5 & 4
let params = ["username":"john", "password":"123456"] as Dictionary<String, String>
var request = URLRequest(url: URL(string: "http://localhost:8080/api/1/login")!)
request.httpMethod = "POST"
request.httpBody = try? JSONSerialization.data(withJSONObject: params, options: [])
request.addValue("application/json", forHTTPHeaderField: "Content-Type")
let session = URLSession.shared
let task = session.dataTask(with: request, completionHandler: { data, response, error -> Void in
print(response!)
do {
let json = try JSONSerialization.jsonObject(with: data!) as! Dictionary<String, AnyObject>
print(json)
} catch {
print("error")
}
})
task.resume()
You can do like this :
var url : String = "http://google.com?test=toto&test2=titi"
var request : NSMutableURLRequest = NSMutableURLRequest()
request.URL = NSURL(string: url)
request.HTTPMethod = "GET"
NSURLConnection.sendAsynchronousRequest(request, queue: NSOperationQueue(), completionHandler:{ (response:NSURLResponse!, data: NSData!, error: NSError!) -> Void in
var error: AutoreleasingUnsafeMutablePointer<NSError?> = nil
let jsonResult: NSDictionary! = NSJSONSerialization.JSONObjectWithData(data, options:NSJSONReadingOptions.MutableContainers, error: error) as? NSDictionary
if (jsonResult != nil) {
// process jsonResult
} else {
// couldn't load JSON, look at error
}
})
EDIT : For people have problem with this maybe your JSON stream is an
array [] and not an object {} so you have to change jsonResult to
NSArray instead of NSDictionary
I think the NSURLSession api fits better in this situation. Because if you write swift code your project target is at least iOS 7 and iOS 7 supports NSURLSession api. Anyway here is the code
let url = "YOUR_URL"
NSURLSession.sharedSession().dataTaskWithURL(NSURL(string: url)) { data, response, error in
// Handle result
}.resume()
Here is the complete code for REST API requests using NSURLSession in swift
For GET Request
let configuration = NSURLSessionConfiguration .defaultSessionConfiguration()
let session = NSURLSession(configuration: configuration)
let urlString = NSString(format: "your URL here")
print("get wallet balance url string is \(urlString)")
//let url = NSURL(string: urlString as String)
let request : NSMutableURLRequest = NSMutableURLRequest()
request.URL = NSURL(string: NSString(format: "%#", urlString) as String)
request.HTTPMethod = "GET"
request.timeoutInterval = 30
request.addValue("application/json", forHTTPHeaderField: "Content-Type")
request.addValue("application/json", forHTTPHeaderField: "Accept")
let dataTask = session.dataTaskWithRequest(request) {
(let data: NSData?, let response: NSURLResponse?, let error: NSError?) -> Void in
// 1: Check HTTP Response for successful GET request
guard let httpResponse = response as? NSHTTPURLResponse, receivedData = data
else {
print("error: not a valid http response")
return
}
switch (httpResponse.statusCode)
{
case 200:
let response = NSString (data: receivedData, encoding: NSUTF8StringEncoding)
print("response is \(response)")
do {
let getResponse = try NSJSONSerialization.JSONObjectWithData(receivedData, options: .AllowFragments)
EZLoadingActivity .hide()
// }
} catch {
print("error serializing JSON: \(error)")
}
break
case 400:
break
default:
print("wallet GET request got response \(httpResponse.statusCode)")
}
}
dataTask.resume()
For POST request ...
let configuration = NSURLSessionConfiguration .defaultSessionConfiguration()
let session = NSURLSession(configuration: configuration)
let params = ["username":bindings .objectForKey("username"), "provider":"walkingcoin", "securityQuestion":securityQuestionField.text!, "securityAnswer":securityAnswerField.text!] as Dictionary<String, AnyObject>
let urlString = NSString(format: “your URL”);
print("url string is \(urlString)")
let request : NSMutableURLRequest = NSMutableURLRequest()
request.URL = NSURL(string: NSString(format: "%#", urlString)as String)
request.HTTPMethod = "POST"
request.timeoutInterval = 30
request.addValue("application/json", forHTTPHeaderField: "Content-Type")
request.addValue("application/json", forHTTPHeaderField: "Accept")
request.HTTPBody = try! NSJSONSerialization.dataWithJSONObject(params, options: [])
let dataTask = session.dataTaskWithRequest(request)
{
(let data: NSData?, let response: NSURLResponse?, let error: NSError?) -> Void in
// 1: Check HTTP Response for successful GET request
guard let httpResponse = response as? NSHTTPURLResponse, receivedData = data
else {
print("error: not a valid http response")
return
}
switch (httpResponse.statusCode)
{
case 200:
let response = NSString (data: receivedData, encoding: NSUTF8StringEncoding)
if response == "SUCCESS"
{
}
default:
print("save profile POST request got response \(httpResponse.statusCode)")
}
}
dataTask.resume()
I hope it works.
edited for swift 2
let url = NSURL(string: "http://www.test.com")
let task = NSURLSession.sharedSession().dataTaskWithURL(url!) {(data, response, error) in
print(NSString(data: data!, encoding: NSUTF8StringEncoding))
}
task.resume()
Swift 4 - GET request
var request = URLRequest(url: URL(string: "http://example.com/api/v1/example")!)
request.httpMethod = "GET"
URLSession.shared.dataTask(with: request, completionHandler: { data, response, error -> Void in
do {
let jsonDecoder = JSONDecoder()
let responseModel = try jsonDecoder.decode(CustomDtoClass.self, from: data!)
print(responseModel)
} catch {
print("JSON Serialization error")
}
}).resume()
Don't forget to configure App Transport Security Settings to add your domain to the exceptions and allow insecure http requests if you're hitting endpoints without using HTTPS.
You can use a tool like http://www.json4swift.com/ to autogenerate your Codeable Mappings from your JSON responses.
In swift 3.3 and 4. I crated APIManager class with two public methods. Just pass required parameter, api name and request type. You will get response then pass it to the closure.
import UIKit
struct RequestType {
static let POST = "POST"
static let GET = "GET"
}
enum HtttpType: String {
case POST = "POST"
case GET = "GET"
}
class APIManager: NSObject {
static let sharedInstance: APIManager = {
let instance = APIManager()
return instance
}()
private init() {}
// First Method
public func requestApiWithDictParam(dictParam: Dictionary<String,Any>, apiName: String,requestType: String, isAddCookie: Bool, completionHendler:#escaping (_ response:Dictionary<String,AnyObject>?, _ error: NSError?, _ success: Bool)-> Void) {
var apiUrl = “” // Your api url
apiUrl = apiUrl.appendingFormat("%#", apiName)
let config = URLSessionConfiguration.default
let session = URLSession(configuration: config)
let url = URL(string: apiUrl)!
let HTTPHeaderField_ContentType = "Content-Type"
let ContentType_ApplicationJson = "application/json"
var request = URLRequest.init(url: url)
request.timeoutInterval = 60.0
request.cachePolicy = URLRequest.CachePolicy.reloadIgnoringLocalCacheData
request.addValue(ContentType_ApplicationJson, forHTTPHeaderField: HTTPHeaderField_ContentType)
request.httpMethod = requestType
print(apiUrl)
print(dictParam)
let dataTask = session.dataTask(with: request) { (data, response, error) in
if error != nil {
completionHendler(nil, error as NSError?, false)
} do {
let resultJson = try JSONSerialization.jsonObject(with: data!, options: []) as? [String:AnyObject]
print("Request API = ", apiUrl)
print("API Response = ",resultJson ?? "")
completionHendler(resultJson, nil, true)
} catch {
completionHendler(nil, error as NSError?, false)
}
}
dataTask.resume()
}
// Second Method
public func requestApiWithUrlString(param: String, apiName: String,requestType: String, isAddCookie: Bool, completionHendler:#escaping (_ response:Dictionary<String,AnyObject>?, _ error: NSError?, _ success: Bool)-> Void ) {
var apiUrl = "" // Your api url
let config = URLSessionConfiguration.default
let session = URLSession(configuration: config)
var request: URLRequest?
if requestType == "GET" {
apiUrl = String(format: "%#%#&%#", YourAppBaseUrl,apiName,param)
apiUrl = apiUrl.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)!
print("URL=",apiUrl)
let url = URL(string: apiUrl)!
request = URLRequest.init(url: url)
request?.httpMethod = "GET"
} else {
apiUrl = String(format: "%#%#", YourAppBaseUrl,apiName)
apiUrl = apiUrl.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)!
print("URL=",apiUrl)
let bodyParameterData = param.data(using: .utf8)
let url = URL(string: apiUrl)!
request = URLRequest(url: url)
request?.httpBody = bodyParameterData
request?.httpMethod = "POST"
}
request?.timeoutInterval = 60.0
request?.cachePolicy = URLRequest.CachePolicy.reloadIgnoringLocalCacheData
request?.httpShouldHandleCookies = true
let dataTask = session.dataTask(with: request!) { (data, response, error) in
if error != nil {
completionHendler(nil, error as NSError?, false)
} do {
if data != nil {
let resultJson = try JSONSerialization.jsonObject(with: data!, options: []) as? [String:AnyObject]
print("Request API = ", apiUrl)
print("API Response = ",resultJson ?? "")
completionHendler(resultJson, nil, true)
} else {
completionHendler(nil, error as NSError?, false)
}
} catch {
completionHendler(nil, error as NSError?, false)
}
}
dataTask.resume()
}
}
// Here is example of calling Post API from any class
let bodyParameters = String(format: "appid=%#&appversion=%#","1","1")
APIManager.sharedInstance.requestApiWithUrlString(param: bodyParameters, apiName: "PASS_API_NAME", requestType: HtttpType.POST.rawValue, isAddCookie: false) { (dictResponse, error, success) in
if success {
if let dictMessage = dictResponse?["message"] as? Dictionary<String, AnyObject> {
// do you work
}
} else {
print("Something went wrong...")
}
}
}
/// Or just use simple function
func dataRequest() {
let urlToRequest = "" // Your API url
let url = URL(string: urlToRequest)!
let session4 = URLSession.shared
let request = NSMutableURLRequest(url: url)
request.httpMethod = "POST"
request.cachePolicy = NSURLRequest.CachePolicy.reloadIgnoringCacheData
let paramString = "data=Hello"
request.httpBody = paramString.data(using: String.Encoding.utf8)
let task = session4.dataTask(with: request as URLRequest) { (data, response, error) in
guard let _: Data = data, let _: URLResponse = response, error == nil else {
print("*****error")
return
}
if let dataString = NSString(data: data!, encoding: String.Encoding.utf8.rawValue) {
print("****Data: \(dataString)") //JSONSerialization
}
}
task.resume()
}
Swift 3.0
let request = NSMutableURLRequest(url: NSURL(string: "http://httpstat.us/200")! as URL)
let session = URLSession.shared
request.httpMethod = "GET"
request.addValue("application/json", forHTTPHeaderField: "Content-Type")
request.addValue("application/json", forHTTPHeaderField: "Accept")
let task = session.dataTask(with: request as URLRequest, completionHandler: {data, response, error -> Void in
if error != nil {
print("Error: \(String(describing: error))")
} else {
print("Response: \(String(describing: response))")
}
})
task.resume()
Swift 4
Create an app using Alamofire with Api Post method
Install pod file -pod 'Alamofire', '~> 4.0' for Swift 3 with Xcode 9
Create Webservices.swift class, import Alamofire
Design storyBoard ,Login View
insert following Code for the ViewControllerClass
import UIKit
class ViewController: UIViewController {
#IBOutlet var usernameTextField: UITextField!
#IBOutlet var passwordTextField: UITextField!
var usertypeStr :String = "-----------"
var loginDictionary : NSDictionary?
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
#IBAction func loginButtonClicked(_ sender: Any) {
WebServices.userLogin(userName: usernameTextField.text!, password: passwordTextField.text!,userType: usertypeStr) {(result, message, status )in
if status {
let loginDetails = result as? WebServices
self.loginDictionary = loginDetails?.loginData
if self.loginDictionary?["status"] as? String == "error"
{
self.alertMessage(alerttitle: "Login Error", (self.loginDictionary?["message"] as? String)!)
} else if self.loginDictionary?["status"] as? String == "ok" {
self.alertMessage(alerttitle: "", "Success")
}else {
self.alertMessage(alerttitle: "", (self.loginDictionary?["message"] as? String)!)
}
} else {
self.alertMessage(alerttitle: "", "Sorry")
}
}
}
func alertMessage(alerttitle:String,_ message : String){
let alertViewController = UIAlertController(title:alerttitle, message:message, preferredStyle: .alert)
alertViewController.addAction(UIAlertAction(title: "OK", style: .default, handler: nil))
present(alertViewController, animated: true, completion: nil)
}
}
Insert Following Code For WebserviceClass
import Foundation
import Alamofire
class WebServices: NSObject {
enum WebServiceNames: String {
case baseUrl = "https://---------------"
case UserLogin = "------------"
}
// MARK: - Login Variables
var loginData : NSDictionary?
class func userLogin(userName: String,password : String,userType : String, completion : #escaping (_ response : AnyObject?, _ message: String?, _ success : Bool)-> ()) {
let url = WebServiceNames.baseUrl.rawValue + WebServiceNames.UserLogin.rawValue
let params = ["USER": userName,"PASS":password,"API_Key" : userType]
WebServices.postWebService(urlString: url, params: params as [String : AnyObject]) { (response, message, status) in
print(response ?? "Error")
let result = WebServices()
if let data = response as? NSDictionary {
print(data)
result.loginData = data
completion(result, "Success", true)
}else {
completion("" as AnyObject?, "Failed", false)
}
}
}
//MARK :- Post
class func postWebService(urlString: String, params: [String : AnyObject], completion : #escaping (_ response : AnyObject?, _ message: String?, _ success : Bool)-> Void) {
alamofireFunction(urlString: urlString, method: .post, paramters: params) { (response, message, success) in
if response != nil {
completion(response as AnyObject?, "", true)
}else{
completion(nil, "", false)
}
}
}
class func alamofireFunction(urlString : String, method : Alamofire.HTTPMethod, paramters : [String : AnyObject], completion : #escaping (_ response : AnyObject?, _ message: String?, _ success : Bool)-> Void){
if method == Alamofire.HTTPMethod.post {
Alamofire.request(urlString, method: .post, parameters: paramters, encoding: URLEncoding.default, headers: nil).responseJSON { (response:DataResponse<Any>) in
print(urlString)
if response.result.isSuccess{
completion(response.result.value as AnyObject?, "", true)
}else{
completion(nil, "", false)
}
}
}else {
Alamofire.request(urlString).responseJSON { (response) in
if response.result.isSuccess{
completion(response.result.value as AnyObject?, "", true)
}else{
completion(nil, "", false)
}
}
}
}
//Mark:-Cancel
class func cancelAllRequests()
{
Alamofire.SessionManager.default.session.getTasksWithCompletionHandler { dataTasks, uploadTasks, downloadTasks in
dataTasks.forEach { $0.cancel() }
uploadTasks.forEach { $0.cancel() }
downloadTasks.forEach { $0.cancel() }
}
}
}
swift 4
USE ALAMOFIRE in our App plz install pod file
pod 'Alamofire', '~> 4.0'
We can Use API for Json Data -https://swapi.co/api/people/
Then We can create A networking class for Our project- networkingService.swift
import Foundation
import Alamofire
typealias JSON = [String:Any]
class networkingService{
static let shared = networkingService()
private init() {}
func getPeople(success successblock: #escaping (GetPeopleResponse) -> Void)
{
Alamofire.request("https://swapi.co/api/people/").responseJSON { response in
guard let json = response.result.value as? JSON else {return}
// print(json)
do {
let getPeopleResponse = try GetPeopleResponse(json: json)
successblock(getPeopleResponse)
}catch{}
}
}
func getHomeWorld(homeWorldLink:String,completion: #escaping(String) ->Void){
Alamofire.request(homeWorldLink).responseJSON {(response) in
guard let json = response.result.value as? JSON,
let name = json["name"] as? String
else{return}
completion(name)
}
}
}
Then Create NetworkingError.swift class
import Foundation
enum networkingError : Error{
case badNetworkigStuff
}
Then create Person.swift class
import Foundation
struct Person {
private let homeWorldLink : String
let birthyear : String
let gender : String
let haircolor : String
let eyecolor : String
let height : String
let mass : String
let name : String
let skincolor : String
init?(json : JSON) {
guard let birthyear = json["birth_year"] as? String,
let eyecolor = json["eye_color"] as? String,
let gender = json["gender"] as? String,
let haircolor = json["hair_color"] as? String,
let height = json["height"] as? String,
let homeWorldLink = json["homeworld"] as? String,
let mass = json["mass"] as? String,
let name = json["name"] as? String,
let skincolor = json["skin_color"] as? String
else { return nil }
self.homeWorldLink = homeWorldLink
self.birthyear = birthyear
self.gender = gender
self.haircolor = haircolor
self.eyecolor = eyecolor
self.height = height
self.mass = mass
self.name = name
self.skincolor = skincolor
}
func homeWorld(_ completion: #escaping (String) -> Void) {
networkingService.shared.getHomeWorld(homeWorldLink: homeWorldLink){ (homeWorld) in
completion(homeWorld)
}
}
}
Then create DetailVC.swift
import UIKit
class DetailVC: UIViewController {
var person :Person!
#IBOutlet var name: UILabel!
#IBOutlet var birthyear: UILabel!
#IBOutlet var homeworld: UILabel!
#IBOutlet var eyeColor: UILabel!
#IBOutlet var skinColor: UILabel!
#IBOutlet var gender: UILabel!
#IBOutlet var hairColor: UILabel!
#IBOutlet var mass: UILabel!
#IBOutlet var height: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
print(person)
name.text = person.name
birthyear.text = person.birthyear
eyeColor.text = person.eyecolor
gender.text = person.gender
hairColor.text = person.haircolor
mass.text = person.mass
height.text = person.height
skinColor.text = person.skincolor
person.homeWorld{(homeWorld) in
self.homeworld.text = homeWorld
}
}
}
Then Create GetPeopleResponse.swift class
import Foundation
struct GetPeopleResponse {
let people : [Person]
init(json :JSON) throws {
guard let results = json["results"] as? [JSON] else { throw networkingError.badNetworkigStuff}
let people = results.map{Person(json: $0)}.flatMap{ $0 }
self.people = people
}
}
Then Our View controller class
import UIKit
class ViewController: UIViewController {
#IBOutlet var tableVieww: UITableView!
var people = [Person]()
#IBAction func getAction(_ sender: Any)
{
print("GET")
networkingService.shared.getPeople{ response in
self.people = response.people
self.tableVieww.reloadData()
}
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?)
{
guard segue.identifier == "peopleToDetails",
let detailVC = segue.destination as? DetailVC,
let person = sender as AnyObject as? Person
else {return}
detailVC.person = person
}
}
extension ViewController:UITableViewDataSource{
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return people.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = UITableViewCell()
cell.textLabel?.text = people[indexPath.row].name
return cell
}
}
extension ViewController:UITableViewDelegate{
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
performSegue(withIdentifier: "peopleToDetails", sender: people[indexPath.row])
}
}
In our StoryBoard
plz Connect with our View with another one using segue with identifier -peopleToDetails
Use UITableView In our First View
Use UIButton For get the Data
Use 9 Labels in our DetailVc
Very simple 100% working , tested
var url : String = "https://restcountries.eu/rest/v2/all"
URLSession.shared.dataTask(with: NSURL(string: url) as! URL) { data, response, error in
// Handle result
let response = String (data: data!, encoding: String.Encoding.utf8)
print("response is \(response)")
do {
let getResponse = try JSONSerialization.jsonObject(with: data!, options: .allowFragments)
print(getResponse)
let countryArray = getResponse as! NSArray
print(countryArray)
let country1 = countryArray[0] as! [String:Any]
let name = country1["name"] as! String
print(name)
} catch {
print("error serializing JSON: \(error)")
}
}.resume()
}
If you're working in Swift 3, the syntax changes. The example here worked for me and has a good explanation of the steps: https://grokswift.com/simple-rest-with-swift/
This is the code from that tutorial:
let todoEndpoint: String = "https://jsonplaceholder.typicode.com/todos/1"
guard let url = URL(string: todoEndpoint) else {
print("Error: cannot create URL")
return
}
let urlRequest = URLRequest(url: url)
let task = session.dataTask(with: urlRequest) {
(data, response, error) in
// check for any errors
guard error == nil else {
print("error calling GET on /todos/1")
print(error!)
return
}
// make sure we got data
guard let responseData = data else {
print("Error: did not receive data")
return
}
// parse the result as JSON, since that's what the API provides
do {
guard let todo = try JSONSerialization.jsonObject(with: responseData, options: [])
as? [String: Any] else {
print("error trying to convert data to JSON")
return
}
// now we have the todo
// let's just print it to prove we can access it
print("The todo is: " + todo.description)
// the todo object is a dictionary
// so we just access the title using the "title" key
// so check for a title and print it if we have one
guard let todoTitle = todo["title"] as? String else {
print("Could not get todo title from JSON")
return
}
print("The title is: " + todoTitle)
} catch {
print("error trying to convert data to JSON")
return
}
}
task.resume()
Swift 5
API call method
//Send Request with ResultType<Success, Error>
func fetch(requestURL:URL,requestType:String,parameter:[String:AnyObject]?,completion:#escaping (Result<Any>) -> () ){
//Check internet connection as per your convenience
//Check URL whitespace validation as per your convenience
//Show Hud
var urlRequest = URLRequest.init(url: requestURL)
urlRequest.cachePolicy = .reloadIgnoringLocalCacheData
urlRequest.timeoutInterval = 60
urlRequest.httpMethod = String(describing: requestType)
urlRequest.setValue("application/json; charset=utf-8", forHTTPHeaderField: "Content-Type")
urlRequest.setValue("application/json; charset=utf-8", forHTTPHeaderField: "Accept")
//Post URL parameters set as URL body
if let params = parameter{
do{
let parameterData = try JSONSerialization.data(withJSONObject:params, options:.prettyPrinted)
urlRequest.httpBody = parameterData
}catch{
//Hide hude and return error
completion(.failure(error))
}
}
//URL Task to get data
URLSession.shared.dataTask(with: requestURL) { (data, response, error) in
//Hide Hud
//fail completion for Error
if let objError = error{
completion(.failure(objError))
}
//Validate for blank data and URL response status code
if let objData = data,let objURLResponse = response as? HTTPURLResponse{
//We have data validate for JSON and convert in JSON
do{
let objResposeJSON = try JSONSerialization.jsonObject(with: objData, options: .mutableContainers)
//Check for valid status code 200 else fail with error
if objURLResponse.statusCode == 200{
completion(.success(objResposeJSON))
}
}catch{
completion(.failure(error))
}
}
}.resume()
}
Use of API call method
func useOfAPIRequest(){
if let baseGETURL = URL(string:"https://postman-echo.com/get?foo1=bar1&foo2=bar2"){
self.fetch(requestURL: baseGETURL, requestType: "GET", parameter: nil) { (result) in
switch result{
case .success(let response) :
print("Hello World \(response)")
case .failure(let error) :
print("Hello World \(error)")
}
}
}
}
Api Call using Model Class
let urlString = "http://--.154.--.78/------/index.php?route=api/coupon/all"
let url = URL(string: urlString)
var request = URLRequest(url: url!)
request.httpMethod = "GET"
URLSession.shared.dataTask(with:request) { (data, response, error) in
if error != nil {
print(error)
} else {
do {
let parsedDictionaryArray = try JSONSerialization.jsonObject(with: data!) as! [String:AnyObject]
print(parsedDictionaryArray)
if let arry = parsedDictionaryArray["data"] as? [[String:AnyObject]] {
for dic in arry {
let name = dic["name"]
let descriptionData = dic["description"]
self.modelReference.append(model(name: name as! String, descriptionStr: descriptionData as! String))
print(name!)
}
}
} catch let error as NSError {
print(error)
}
}
}.resume()
create a variable and connect with model class
var modelReference = [model]()
create a model class New -> swift class
import Foundation
class model : NSObject{
var name : String
var descriptionStr: String
init(name : String, descriptionStr: String)
{
self.name = name
self.descriptionStr = descriptionStr
}
}
then we can connect with our table view objects
let cell = tableView.dequeueReusableCell(withIdentifier: "TableViewCellID")as! TableViewCell
cell.listName.text = modelReference[indexPath.row].name
let headers = [
"cache-control": "no-cache",
"postman-token": "6f8a-12c6-87a1-ac0f25d6385a"
]
let request = NSMutableURLRequest(url: NSURL(string: "Your url string")! as URL,
cachePolicy: .useProtocolCachePolicy,
timeoutInterval: 10.0)
request.httpMethod = "GET"
request.allHTTPHeaderFields = headers
let session = URLSession.shared
let dataTask = session.dataTask(with: request as URLRequest, completionHandler: { (data, response, error) -> Void in
if error == nil && data != nil {
do {
// Convert NSData to Dictionary where keys are of type String, and values are of any type
let json = try JSONSerialization.jsonObject(with: data!, options: JSONSerialization.ReadingOptions.mutableContainers) as! [String:AnyObject]
print(json)
//do your stuff
// completionHandler(true)
} catch {
// completionHandler(false)
}
}
else if error != nil
{
//completionHandler(false)
}
}).resume()
}
func getAPICalling(mainUrl:String) {
//create URL
guard let url = URL(string: mainUrl) else {
print("Error: cannot create URL")
return
}
//create request
let urlRequest = URLRequest(url: url)
// create the session
let config = URLSessionConfiguration.default
let session = URLSession(configuration: config)
// make the request
let task = session.dataTask(with: urlRequest) {
(data, response, error) in
// check for any errors
guard error == nil else {
print("error calling GET")
print(error!.localizedDescription)
return
}
// make sure we got data
guard let responseData = data else {
print("error: did not receive data")
return
}
// convert Data in JSON && parse the result as JSON, since that's what the API provides
do {
guard let object = try JSONSerialization.jsonObject(with: responseData, options: [])
as? [String: Any] else {
print("error trying to convert data to JSON")
return
}
//JSON Response
guard let todoTitle = object["response"] as? NSDictionary else {
print("Could not get todo title from JSON")
return
}
//Get array in response
let responseList = todoTitle.value(forKey: "radioList") as! NSArray
for item in responseList {
let dic = item as! NSDictionary
let str = dic.value(forKey: "radio_des") as! String
self.arrName.append(str)
print(item)
}
DispatchQueue.main.async {
self.tblView.reloadData()
}
} catch {
print("error trying to convert data to JSON")
return
}
}
task.resume()
}
Usage:
getAPICalling(mainUrl:"https://dousic.com/api/radiolist?user_id=16")
class ApiManager: NSObject {
static func callGetDataApi(url: String,completion: #escaping([[String: Any]])-> Void) {
guard let url = URL(string: ApiName.baseUrl+url ) else { return }
var request = URLRequest(url: url)
request.httpMethod = "GET"
URLSession.shared.dataTask(with:request) { (data, response, error) in
if error != nil {
print(error ?? "")
} else {
do {
let arry = try JSONSerialization.jsonObject(with: data!) as! [[String:AnyObject]]
print(arry)
DispatchQueue.main.async {
completion(arry)
}
} catch let error as NSError {
print(error)
}
}
}.resume()
}
}

Resources