I read from my server and print it out in my xcode project. But when I tried to represent the json code to show mapkit annotations according to latitude and longitude from the server it would not work.
Here is my structure:
struct User {
var locaitonid: String
var latitude: Double
var longitude: Double
var cityName: String
init(_ dictionary: [String: Any]) {
self.locaitonid = dictionary["location_id"] as? String ?? ""
self.latitude = dictionary["latitude"] as? Double ?? 0.0
self.longitude = dictionary["longitude"] as? Double ?? 0.0
self.cityName = dictionary["city_name"] as? String ?? ""
}
Here is my viewController code:
class ViewController: UIViewController {
var model = [User]()
#IBOutlet weak var mapView: MKMapView!
override func viewDidLoad() {
super.viewDidLoad()
guard let url = URL(string: "http://web server link ") else {return}
let task = URLSession.shared.dataTask(with: url) { (data, response, error) in
guard let dataResponse = data,
error == nil else {
print(error?.localizedDescription ?? "Response Error")
return }
do{
//here dataResponse received from a network request
let jsonResponse = try JSONSerialization.jsonObject(with:
dataResponse, options: [])
// print(jsonResponse) //Response result
guard let jsonArray = jsonResponse as? [[String: Any]] else {
return
}
for dic in jsonArray{
let annotation = MKPointAnnotation()
annotation.title = (dic["location_id"] as! String)
annotation.subtitle = (dic["city_name"]as! String)
annotation.coordinate = CLLocationCoordinate2D(latitude: dic["latitude"]as!Double, longitude: dic["longitude"]as!Double)
self.mapView.addAnnotations([annotation])
self.mapView.showAnnotations([annotation], animated: true)
}
} catch let parsingError {
print("Error", parsingError)
}
}
task.resume()
}
Try the following code
class ViewController: UIViewController {
var model = [User]()
#IBOutlet weak var mapView: MKMapView!
override func viewDidLoad() {
super.viewDidLoad()
guard let url = URL(string: "http://web server link ") else {return}
let task = URLSession.shared.dataTask(with: url) { (data, response, error) in
guard let dataResponse = data,
error == nil else {
print(error?.localizedDescription ?? "Response Error")
return }
do{
//here dataResponse received from a network request
let jsonResponse = try JSONSerialization.jsonObject(with:
dataResponse, options: [])
// print(jsonResponse) //Response result
guard let jsonArray = jsonResponse as? [[String: Any]] else {
return
}
for dic in jsonArray{
let annotation = MKPointAnnotation()
annotation.title = (dic["location_id"] as! String)
annotation.subtitle = (dic["city_name"]as! String)
annotation.coordinate = CLLocationCoordinate2D(latitude: dic["latitude"]as!Double, longitude: dic["longitude"]as!Double)
DispatchQueue.main.async {
self.mapView.addAnnotations([annotation])
self.mapView.showAnnotations([annotation], animated: true)
}
}
} catch let parsingError {
print("Error", parsingError)
}
}
task.resume()
}
Related
For designing i am using storyboards. Home view controller contains side menu in separate view and i have designed home with collection view. for side menu i am using ENSideMenu which contains MyNavigationController.. if i give MyNavigationController as a is initial view controller then home is working fine but when i give sign in view controller as a is initial view controller then home view controller appears but collection view and side menu is not responding while tapping.
Here is my code:
In appdelegagte:
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
let userId: String? = KeychainWrapper.standard.string(forKey: "Uid")
print("appdelegate userid \(userId)")
if userId != nil{
let mainStoryBoard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let homeVC = mainStoryBoard.instantiateViewController(withIdentifier: "HomeViewController") as! HomeViewController
self.window!.rootViewController = homeVC
}
return true
}
in signin vc:
func logInService(){
let parameters = ["username":Int(userIdTextFielf.text ?? ""),
"password":passwordTextField.text] as? [String : Any]
let url = URL(string: "https://dev.anyemi.com/webservices/anyemi/login")
var req = URLRequest(url: url!)
req.httpMethod = "POST"
req.addValue("application/json", forHTTPHeaderField: "Contet-Type")
req.addValue("application/json", forHTTPHeaderField: "Accept")
guard let httpBody = try? JSONSerialization.data(withJSONObject: parameters as Any, options: .prettyPrinted) else {return}
req.httpBody = httpBody
let session = URLSession.shared
session.dataTask(with: req, completionHandler: {(data, response, error) in
if let response = response {
// print(response)
}
if let data = data {
do{
let json = try JSONSerialization.jsonObject(with: data, options: .mutableContainers) as! [String: Any]
print("the json of loginnnnnn \(json)")
let Uid = json["id"] as? String
let loginPhoneNum = json["user_id"] as? String
let nameL = json["user_name"] as? String
let emailL = json["user_email"] as? String
let saveUserId: Bool = KeychainWrapper.standard.set(Uid!, forKey: "Uid")
print("the userid is \(saveUserId)")
if (Uid?.isEmpty)!
{
print("login fail")
}
else{
DispatchQueue.main.async {
let homeVC = self.storyboard?.instantiateViewController(withIdentifier: "HomeViewController") as! HomeViewController
let appDelagate = UIApplication.shared.delegate
appDelagate?.window??.rootViewController = homeVC
}
}
}catch{
print("error")
}
}
}).resume()
}
in home vc:
import UIKit
struct JsonData {
var iconHome: String?
var typeName: String?
init(icon: String, tpe: String) {
self.iconHome = icon
self.typeName = tpe
}
}
class HomeViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource {
#IBOutlet weak var collectionView: UICollectionView!
var itemsArray = [JsonData]()
override func viewDidLoad() {
super.viewDidLoad()
homeServiceCall()
//Do any additional setup after loading the view.
collectionView.delegate = self
collectionView.dataSource = self
let layout = UICollectionViewFlowLayout()
layout.sectionInset = UIEdgeInsets(top: 2.0, left: 2.0, bottom: 2.0, right: 2.0)
layout.itemSize = CGSize(width: 95, height: 95)
collectionView?.collectionViewLayout = layout
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
// Hide the navigation bar on the this view controller
self.navigationController?.setNavigationBarHidden(true, animated: true)
}
#IBAction func sideMenuButton(_ sender: Any) {
print("in side menu")
toggleSideMenuView()
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return itemsArray.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! HomeCollectionViewCell
let aData = itemsArray[indexPath.row]
cell.paymentLabel.text = aData.typeName
if let url = NSURL(string: aData.iconHome ?? "") {
if let data = NSData(contentsOf: url as URL) {
cell.paymentImage.image = UIImage(data: data as Data)
}
}
return cell
}
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
let nextViewController = self.storyboard?.instantiateViewController(withIdentifier: "MakePaymentViewController") as! MakePaymentViewController
self.navigationController?.pushViewController(nextViewController, animated: true)
let indexPath = indexPath.row
}
//MARK:- Service-call
func homeServiceCall(){
let urlStr = "https://dev.anyemi.com/webservices/anyemi/getfinancer"
let url = URL(string: urlStr)
URLSession.shared.dataTask(with: url!, completionHandler: {(data, response, error) in
guard let respData = data else {
return
}
guard error == nil else {
print("error")
return
}
do{
let jsonObj = try JSONSerialization.jsonObject(with: respData, options: .allowFragments) as! [String: Any]
print("the home json is \(jsonObj)")
let financerArray = jsonObj["financer"] as! [[String: Any]]
print("homw financerData \(financerArray)")
for financer in financerArray {
let id = financer["id"] as! String
let pic = financer["icon"] as? String
let type = financer["tpe"] as! String//dob
print(id)
print(type)
print("the icons \(String(describing: pic))")
self.itemsArray.append(JsonData(icon: pic ?? "", tpe: type))
print("online images bug \(self.itemsArray.count)")
}
DispatchQueue.main.async {
self.collectionView.reloadData()
}
}
catch {
print("catch error")
}
}).resume()
}
#IBAction func signOutButton(_ sender: Any) {
print("signout tapped")
KeychainWrapper.standard.remove(key: "Uid")
}
}
if i use MyNavigationController with is initial then sidemenu and didSelectItemAt are working but when i am coming from sinin both are not responding please help me in the above issue.
This is happening due to you are not embedding navigation controller to your HomeViewController. Update your code in sign-in as follow:
let homeVC = self.storyboard?.instantiateViewController(withIdentifier: "HomeViewController") as! HomeViewController
let navigationController = UINavigationController(rootViewController: homeVC)
let appDelagate = UIApplication.shared.delegate
appDelagate?.window??.rootViewController = navigationController
I am trying to parse JSON using the following method, but XCode is giving me an error where I have declared "data" .
I am new, I don't understand what is wrong. Please help me.
import UIKit
struct Contacts: Decodable {
let id: Int
let name: String
let email: String
}
class ViewController: UIViewController {
override func viewDidLoad()
{
super.viewDidLoad()
let urlString = "https://api.androidhive.info/contacts/"
guard let url = URL(string: urlString) else {return}
URLSession.shared.dataTask(with: url) { (data, response, error) in
}
guard let data = data else {return}
//let datastring = String(data: data, encoding: .utf8)
do
{
let contact = try JSONDecoder().decode([Contacts].self, from: data)
print(contact.name)
} catch let jsonErr {
print("Error deserializing json:", jsonErr)
}
}
}
Three major issues.
You are ignoring the root object which is a dictionary containing the contacts array.
The value for key id is String, not Int.
A hard rule is : Everything in double quotes is String even "12" and "false"
You have to resume the task and put the code to parse the JSON into the completion handler.
struct Root : Decodable {
let contacts : [Contact]
}
struct Contact : Decodable { // It's recommended to name this kind of struct in singular form
let id, name, email: String
}
...
override func viewDidLoad()
{
super.viewDidLoad()
let urlString = "https://api.androidhive.info/contacts/"
guard let url = URL(string: urlString) else {return}
URLSession.shared.dataTask(with: url) { (data, response, error) in
if let error = error { print(error); return }
do {
let result = try JSONDecoder().decode(Root.self, from: data!)
let contacts = result.contacts
for contact in contacts {
print(contact.name)
}
} catch {
print("Error deserializing json:", error)
}
}.resume()
}
//
// ViewController.swift
// PostMethodTest
//
// Created by HABIB UR REHMAN on 12/11/2018.
// Copyright © 2018 HABIB UR REHMAN. All rights reserved.
//
import UIKit
class ViewController: UIViewController {
struct Resturant: Decodable {
var name: String
var deliveryCharges: String
var email: String
init(_ dictionary: [String: Any]) {
self.name = dictionary["name"] as? String ?? ""
self.deliveryCharges = dictionary["deliveryCharges"] as? String ?? ""
self.email = dictionary["email"] as? String ?? ""
}
}
override func viewDidLoad() {
super.viewDidLoad()
guard let url = URL(string: "your Link Here ") else { return }
var request = URLRequest(url: url)
request.httpMethod = "POST"
let task = URLSession.shared.dataTask(with: request) {(data, response, error) in
guard let dataResponse = data,
error == nil else {
print(error?.localizedDescription ?? "Response Error")
return }
do{
//here dataResponse received from a network request
let jsonResponse = try JSONSerialization.jsonObject(with:
dataResponse, options: [])
print(jsonResponse) //Response result
} catch let parsingError {
print("Error", parsingError)
}
}
task.resume()
}
}
Please Try this its working for me.
func getContactListsApiCalling() {
var request = URLRequest(url: URL(string: "https://api.androidhive.info/contacts/")!)
request.httpMethod = "GET"
request.addValue("application/json", forHTTPHeaderField: "Content-Type")
let session = URLSession.shared
let task = session.dataTask(with: request, completionHandler: { data, response, error -> Void in
do {
if let Data = data {
let responseJSON = try JSONSerialization.jsonObject(with: Data) as? Dictionary<String, AnyObject> ?? [:]
if let contacts = responseJSON["contacts"] as? [[String :AnyObject]] {
for contact in contacts {
let id = contact["id"] as? String ?? ""
let name = contact["name"] as? String ?? ""
let email = contact["email"] as? String ?? ""
let address = contact["address"] as? String ?? ""
let gender = contact["gender"] as? String ?? ""
print(id,name,email,address,gender)
}
}
}
} catch {
print("error")
}
})
task.resume()
}
}
I have this func in a Swift file, and it returns the value of the data in the database and prints it out in the counsel.
I want to use the value in the other View Controller but I can't get this to work, so I hope somebody can help me.
It is the nameUser, statusUser and pointUser I like to use in other View Controller.
import Foundation
import UIKit
var code = "100"
var getStatusUSer = ""
class getJSON: NSObject, URLSessionDataDelegate
{
//properties
var data : NSMutableData = NSMutableData()
func downloadItems()
{
let url = NSMutableURLRequest(url: NSURL(string: "http://www.hholm.dk/time_app/qrcode4.php")! as URL)
url.httpMethod = "POST"
let postString = "username=\(code)"
url.httpBody = postString.data(using: String.Encoding.utf8)
print(url.httpBody = postString.data(using: String.Encoding.utf8))
var session: URLSession!
let configuration = URLSessionConfiguration.default
session = URLSession(configuration: configuration, delegate: self, delegateQueue: nil)
let task = session.dataTask(with: url as URLRequest)
task.resume()
}
func urlSession(_ session: URLSession, dataTask: URLSessionDataTask, didReceive data: Data)
{
self.data.append(data as Data);
}
func urlSession(_ session: URLSession, task: URLSessionTask, didCompleteWithError error: Error?)
{
if error != nil
{
print("Not Found", error)
}
else
{
print("Ok")
self.parseJSON()
}
}
func parseJSON()
{
var jsonResult: NSArray = NSArray()
do
{
jsonResult = try JSONSerialization.jsonObject(with: self.data as Data, options:JSONSerialization.ReadingOptions.allowFragments) as! NSArray
print("jsonResult.count",jsonResult.count)
}
catch let error as NSError
{
print("jsonResult: ", error)
}
var jsonElement: NSDictionary = NSDictionary()
var contador = 0
for i in (0..<jsonResult.count)
{
jsonElement = jsonResult[i] as! NSDictionary
if let nameUser = jsonElement["name"] as? String,
let pointUser = jsonElement["point"] as? String,
let statusUser = jsonElement["status"] as? String
{
getStatusUSer = statusUser
print("Name: ", nameUser)
print("Status: ", statusUser)
print("Point: ", pointUser)
}
}
}
}
Hi Woof this is what i have in my viewcontroler:
import UIKit
class inputcodeViewController: UIViewController {
#IBOutlet weak var input: UITextField!
#IBAction func but(_ sender: Any) {
downloadItems()
}
func downloadItems(){
let getJson = GetJSON()
//setting the delegate
getJson.delegate = self
//starting download
getJson.downloadItems()
}
}
extension inputcodeViewController: GetJSONDelegate {
func didReceiveValues(name: String, status: String, point: String){
//now you can use values in your view controller
}
}
how can i print the values
You can use protocol to return those values:
import Foundation
import UIKit
var code = "100"
var getStatusUSer = ""
//define the protocol
protocol GetJSONDelegate {
func didReceiveValues(name: String, status: String, point: String)
}
//I've changed the first char of the class name to uppercase
class GetJSON: NSObject, URLSessionDataDelegate{
//properties
var data : NSMutableData = NSMutableData()
//delegate
var delegate: GetJSONDelegate?
func downloadItems(){
let url = NSMutableURLRequest(url: NSURL(string: "http://www.hholm.dk/time_app/qrcode4.php")! as URL)
url.httpMethod = "POST"
let postString = "username=\(code)"
url.httpBody = postString.data(using: String.Encoding.utf8)
print(url.httpBody = postString.data(using: String.Encoding.utf8))
var session: URLSession!
let configuration = URLSessionConfiguration.default
session = URLSession(configuration: configuration, delegate: self, delegateQueue: nil)
let task = session.dataTask(with: url as URLRequest)
task.resume()
}
func urlSession(_ session: URLSession, dataTask: URLSessionDataTask, didReceive data: Data)
{
self.data.append(data as Data);
}
func urlSession(_ session: URLSession, task: URLSessionTask, didCompleteWithError error: Error?)
{
if error != nil
{
print("Not Found", error)
}
else
{
print("Ok")
self.parseJSON()
}
}
func parseJSON()
{
var jsonResult: NSArray = NSArray()
do
{
jsonResult = try JSONSerialization.jsonObject(with: self.data as Data, options:JSONSerialization.ReadingOptions.allowFragments) as! NSArray
print("jsonResult.count",jsonResult.count)
}
catch let error as NSError
{
print("jsonResult: ", error)
}
var jsonElement: NSDictionary = NSDictionary()
var contador = 0
for i in (0..<jsonResult.count)
{
jsonElement = jsonResult[i] as! NSDictionary
if let nameUser = jsonElement["name"] as? String,
let pointUser = jsonElement["point"] as? String,
let statusUser = jsonElement["status"] as? String
{
getStatusUSer = statusUser
print("Name: ", nameUser)
print("Status: ", statusUser)
print("Point: ", pointUser)
//here we will return received data to the delegate
self.delegate?.didReceiveValues(name: nameUser, status: statusUser, point: pointUser)
}
}
}
}
Now we need to set your controller as a delegate for that protocol:
//this is an example, you need to add the methods described in your controller where you want to use those values
class YourViewController: UIViewController{
// the method that is called by you to get values
func downloadItems(){
let getJson = GetJSON()
//setting the delegate
getJson.delegate = self
//starting download
getJson.downloadItems()
}
}
//defining protocol methods in the extension of the view controller
extension YourViewController: GetJSONDelegate {
func didReceiveValues(name: String, status: String, point: String){
//now you can use values in your view controller
}
}
I'm having a problem with the following code. I'm downloading a list of actors in JSON and I want to populate Struct Actor with the received data. Everything works great until I try to flatMap on the received data and try to initialize the struct Actor. When I try to compile the code i get the error: Cannot assign value of type '()' to type [Actor]. The error corresponds to a line in viewDidLoad actorsList = downloadActors() Would anybody have any recommendation who to solve this?
import UIKit
func downloadActors() {
var request = URLRequest(url: URL(string: "url...")!)
request.httpMethod = "POST"
let postString = "actorGroup=\("Superhero")"
request.httpBody = postString.data(using: .utf8)
let task = URLSession.shared.dataTask(with: request) { data, response, error in
DispatchQueue.main.async {
guard let data = data, error == nil else {
print("error=\(error)")
return
}
if let httpStatus = response as? HTTPURLResponse, httpStatus.statusCode != 200 {
print("error : statusCode should be 200 but is \(httpStatus.statusCode)")
print("response = \(response)")
}
if let httpStatus = response as? HTTPURLResponse, httpStatus.statusCode == 200 {
do {
let json = try JSONSerialization.jsonObject(with: data, options: JSONSerialization.ReadingOptions.mutableContainers) as? [String: AnyObject]
guard let actorsJSON = json?["response"] as? [[String : AnyObject]] else {
return
}
} catch {
print("catch error")
}
}
}
}
task.resume()
}
func loadActors() -> [Actor] {
if let actors = actorsJSON as? [[String : AnyObject]] {
return actors.flatMap(Actor.init)
}
}
let actorsArray = loadActors()
class MasterViewController: UITableViewController {
var actorsList = [Actor]()
var detailViewController: DetailViewController? = nil
var objects = [Any]()
override func viewDidLoad() {
super.viewDidLoad()
actorsList = downloadActors()
print(actorsList)
Struct Actors is as follows:
struct Job {
let actorGroup: String
let actorName: String
}
extension Actor: JSONDecodable {
init?(JSON: [String : AnyObject]) {
guard let actorGroup = JSON["actorGroup"] as? String, let actorName = JSON["actorName"] as? String else {
return nil
}
self. actorGroup = actorGroup
self. actorName = actorName
}
}
let listActors = actorsJSON as? [[String : AnyObject]] {
Should be:
if let listActors = actorsJSON as? [[String : AnyObject]] {
Edit: For more info I'd like to add Vadian's comment:
Very confusing code. What does the function in the middle of the do block? Why do you type-check actorsJSON twice? The computed property is let listActors... which should be probably an optional binding (if let ... ). Further .mutableContainers is completely nonsense in Swift. And finally a JSON dictionary is [String:Any] in Swift 3.
I am trying to parse JSON but getting this error:
type of expression is ambiguous without more context
My code is:
func jsonParser() {
let urlPath = "http://headers.jsontest.com/"
let endpoint = NSURL(string: urlPath)
let request = NSMutableURLRequest(URL:endpoint!)
let session = NSURLSession.sharedSession()
NSURLSession.sharedSession().dataTaskWithRequest(request){ (data, response, error) throws -> Void in
if error != nil {
print("Get Error")
}else{
//var error:NSError?
do {
let json:AnyObject = try NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions(rawValue: 0)) as? NSDictionary
print(json)
} catch let error as NSError {
// error handling
print(error?.localizedDescription)
}
}
}
//task.resume()
}
This is working fine with out try catch in Xcode 6.4 but this is not working in Xcode 7.
Don't declare an AnyObject type for your decoded object since you want it to be an NSDictionary and you're performing a conversion to do this.
Also it's better to use zero options for NSJSONSerialization instead of random ones.
In my example I've also used a custom error type just for demonstration.
Note, if you're using a custom error type, you have to also include a generic catch to be exhaustive (in this example, with a simple downcasting to NSError).
enum JSONError: String, ErrorType {
case NoData = "ERROR: no data"
case ConversionFailed = "ERROR: conversion from JSON failed"
}
func jsonParser() {
let urlPath = "http://headers.jsontest.com/"
guard let endpoint = NSURL(string: urlPath) else {
print("Error creating endpoint")
return
}
let request = NSMutableURLRequest(URL:endpoint)
NSURLSession.sharedSession().dataTaskWithRequest(request) { (data, response, error) in
do {
guard let data = data else {
throw JSONError.NoData
}
guard let json = try NSJSONSerialization.JSONObjectWithData(data, options: []) as? NSDictionary else {
throw JSONError.ConversionFailed
}
print(json)
} catch let error as JSONError {
print(error.rawValue)
} catch let error as NSError {
print(error.debugDescription)
}
}.resume()
}
The same with Swift 3.0.2:
enum JSONError: String, Error {
case NoData = "ERROR: no data"
case ConversionFailed = "ERROR: conversion from JSON failed"
}
func jsonParser() {
let urlPath = "http://headers.jsontest.com/"
guard let endpoint = URL(string: urlPath) else {
print("Error creating endpoint")
return
}
URLSession.shared.dataTask(with: endpoint) { (data, response, error) in
do {
guard let data = data else {
throw JSONError.NoData
}
guard let json = try JSONSerialization.jsonObject(with: data, options: []) as? NSDictionary else {
throw JSONError.ConversionFailed
}
print(json)
} catch let error as JSONError {
print(error.rawValue)
} catch let error as NSError {
print(error.debugDescription)
}
}.resume()
}
Apple declare here.
func dataTaskWithRequest(request: NSURLRequest, completionHandler: (NSData?, NSURLResponse?, NSError?) -> Void) -> NSURLSessionDataTask
Fix it:
NSURLSession.sharedSession().dataTaskWithRequest(request) { (data, response, error) -> Void in
// Your handle response here!
}
UPDATE:
func jsonParser() {
let urlPath = "http://headers.jsontest.com/"
let endpoint = NSURL(string: urlPath)
let request = NSMutableURLRequest(URL:endpoint!)
NSURLSession.sharedSession().dataTaskWithRequest(request) { (data, response, error) -> Void in
print(error)
}.resume()
}
RESULT:
Optional(Error Domain=NSURLErrorDomain Code=-1022 "The resource could not be loaded because the App Transport Security policy requires the use of a secure connection." UserInfo={NSUnderlyingError=0x7f8873f148d0 {Error Domain=kCFErrorDomainCFNetwork Code=-1022 "(null)"}, NSErrorFailingURLStringKey=http://headers.jsontest.com/, NSErrorFailingURLKey=http://headers.jsontest.com/, NSLocalizedDescription=The resource could not be loaded because the App >Transport Security policy requires the use of a secure connection.})
Hope this helps!
For Swift 4 Web service Call , Post Method using URLSession
func WebseviceCall(){
var request = URLRequest(url: URL(string: "YOUR_URL")!)
request.httpMethod = "POST"
let postString = "PARAMETERS"
request.httpBody = postString.data(using: .utf8)
request.setValue("application/json", forHTTPHeaderField: "Content-Type")
request.setValue("application/json", forHTTPHeaderField: "Accept")
let task = URLSession.shared.dataTask(with: request) { data, response, error in
guard let data = data, error == nil else {
print("error=\(error)")
return
}
if let httpStatus = response as? HTTPURLResponse, httpStatus.statusCode != 200 {
print("statusCode should be 200, but is \(httpStatus.statusCode)")
print("response = \(response)")
}
do {
if let convertedJsonIntoDict = try JSONSerialization.jsonObject(with: data, options: []) as? NSDictionary {
// Print out dictionary
print(convertedJsonIntoDict)
}
} catch let error as NSError {
print(error.localizedDescription)
}
}
task.resume()
}
Here is the simplest way to parse JSON using NSUrlSession.,
let PARAMS = "{\"params1\":\"%#\",\"Params2\":\"%#\",\"params3\":\"%#\"}"
let URL = "your url here"
on submit button write this code.,
let urlStr = String(format: "%#",URL)
let jsonString = String(format:PARAMS, params1value,params2value,params3value )
// Encode your data here
let jsonData = jsonString.data(using:.utf8)
var request = URLRequest(url: URL(string: urlStr)!)
request.setValue("application/json", forHTTPHeaderField: "Content-Type")
request.setValue("application/json", forHTTPHeaderField: "Accept")
//set your method type here
request.httpMethod = "POST"
request.httpBody = jsonData
let configuration = URLSessionConfiguration.default
// create a session here
let session = URLSession(configuration: configuration, delegate: nil, delegateQueue: OperationQueue.main)
let task = session.dataTask(with: request) {(data , response, error) in
if(error != nil){
print("Error \(String(describing: error))")
}
else {
do {
let fetchedDataDictionary = try JSONSerialization.jsonObject(with: data!, options: []) as? NSDictionary
print(fetchedDataDictionary!)
let message = fetchedDataDictionary?["response key here"] as! String
if message == "your response string" {
print(message)
}
else {
self.dataArray = (fetchedDataDictionary?["data"] as! NSArray)
}
}
catch let error as NSError {
print(error.debugDescription)
}
}
}
task.resume()
1)Make ApiConnection class in to your project..
import Foundation
class ApiConnection: NSObject {
class func postDataWithRequest(_ dicData:NSDictionary, completionHandler:#escaping (_ response:NSDictionary?,_ status:Bool)->Void)
{
let URL=Foundation.URL(string: Constant.API_URL)
let request=NSMutableURLRequest(url: URL!)
request.httpMethod="POST"
request.addValue(Constant.kApplicationJSON, forHTTPHeaderField:Constant.kContentType)
let data=try? JSONSerialization .data(withJSONObject: dicData, options: JSONSerialization.WritingOptions.prettyPrinted)
request.httpBody=data
//let dispatchTime = dispatch_time(DISPATCH_TIME_NOW, Int64(Double(NSEC_PER_SEC)*5))
//dispatch_after(dispatchTime, dispatch_get_main_queue()) {
let session = URLSession.shared.dataTask(with: request as URLRequest,completionHandler: { (data, response, error) in
if error==nil
{
DispatchQueue.main.async(execute: {
let dicResponse = try? JSONSerialization .jsonObject(with: data!, options: JSONSerialization.ReadingOptions.allowFragments) as! NSDictionary
completionHandler(dicResponse, error==nil)
})
}
else
{
completionHandler(nil, error==nil)
}
})
session.resume()
}
}
**********************************use this in your view controller****************
let dict : NSMutableDictionary = [:];
dict["Your key"] = "your value"
dict["Your key"] = "your value"
dict["Your key"] = "your value"
ApiConnection.postDataWithRequest(dict) { (response, status) in
if(status){
print(response);
else{
print("failed webservice call");
}
}
*************************************Swift3.0*************************************
var objDic = [String: Any]()
let dic = NSMutableDictionary()
print(dic)
objDic["action"] = ""
objDic["product_id"] = self.peroductid
// arrProduct .addObjects(from: objDic) as! Dictionary
print("\(objDic)")
Alamofire.request(Constant.Webservice_productinfo,
method: HTTPMethod.post,
parameters:objDic as? Parameters,
encoding: JSONEncoding.default,
headers: nil).responseJSON
{
(response:DataResponse<Any>) in
switch(response.result)
{
case .success(_):
if response.result.value != nil
{
let status = response2?.object(forKey: "status") as! String?
if status == "error"{}
//finding the status from response
var response2 = response.result.value as AnyObject?
self.response1 = response.result.value as! NSDictionary
let type =
(self.cartlistarray[0] as!NSDictionary)["base_image"]
cell.productname.text = (self.cartlistarray[0] as!NSDictionary)["name"] as? String
//Store the result value in swift 3.0
UserDefaults.standard.set(userDetail.value(forKey: "email") as? NSString, forKey: "email")
if(UserDefaults.standard.object(forKey:"email") == nil){}
//did select row click the data pass into another view
let ProductListViewController = self.storyboard?.instantiateViewController(withIdentifier: "ProductListViewController") as! ProductListViewController
ProductListViewController.category_id = ((self.bannerarry[0] as? [String : String])?["cat_id"])!
//or else callin from indexpath.row
item = ((cartlistarray[indexpath.row] as? NSDictionary)?.value(forKey:"product_id") as! String?)!
extension UIAlertController{
func showErrorAlert(strMesage:NSString,VC:Any)
{
let alert = UIAlertController(title: "Demo App", message: strMesage as String, preferredStyle: UIAlertControllerStyle.alert)
alert.addAction(UIAlertAction(title: "Ok", style: UIAlertActionStyle.default, handler: nil))
(VC as AnyObject).present(alert, animated: true, completion: nil)
}
}
extension UILabel{
func setLabel(strTitle:String)
{
self.backgroundColor = UIColor.clear
self.textColor = UIColor.white
self.textAlignment = NSTextAlignment.left
self.font = UIFont(name: "Avenir-Light", size: 15.0)
self.font = UIFont.italicSystemFont(ofSize: 15)
self.text=strTitle
}
}
//image in to base 64
let image = imageCamera.image
let imageData:NSData = UIImageJPEGRepresentation(image!, 1.0)!as NSData
imageconvert = imageData.base64EncodedString(options: .lineLength64Characters)
base64formate = imageconvert.trimmingCharacters(in:CharacterSet.whitespaces)
print(base64formate)
print data into profle view
let imageurl:String! = SharedManager.sharedInstance().myMutableDict.value(forKey:"profileimg") as? String ?? "123"
let url = URL(string: imageurl)
DispatchQueue.global(qos: .userInitiated).async {
let imageData:NSData = NSData(contentsOf: url!)!
// When from background thread, UI needs to be updated on main_queue
DispatchQueue.main.async {
let image = UIImage(data: imageData as Data)
self.imageview.image = image
}
}
let actionSheetController: UIAlertController = UIAlertController(title: "Magento Extension App", message:response1?.object(forKey: "message") as? String, preferredStyle: .alert)
actionSheetController.addAction(UIAlertAction(title: "Ok", style: .default , handler:{ (UIAlertAction)in
print("Ok button click")
}))
self.present(actionSheetController, animated: true, completion: nil)
}
case .failure(_):
print("error: \(response.result.error)") // original
URL request
break
}
}
**************************objc**************************************************
NSDictionary *objDic1 = #{#"mode":#"loginUser",
#"email":[result
objectForKey:#"email"],
#"password":#"",
};
// With AFNetworking
AFHTTPSessionManager *manager = [[AFHTTPSessionManager alloc]initWithSessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
manager.requestSerializer = [AFJSONRequestSerializer serializer];
[manager.requestSerializer setTimeoutInterval:100];
// manager set
[manager.requestSerializer setValue:#"application/json" forHTTPHeaderField:#"Content-Type"];
[manager POST:WEBSERVICE_CALL_URL parameters:objDic1 progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable result) {
[SVProgressHUD dismiss];
NSLog(#"This s my response %#",result);
NSLog(#"success!");
if ([[result valueForKey:kStatus] isEqualToString:kOK])
{
}
}
failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error)
{
}];
****************SDK LINK*******************************************
https://github.com/AFNetworking/AFNetworking
var userDetail = NSArray()
userDetail = self.response1.value(forKey: "userData") as! NSArray
print(userDetail)
self.tmpDic = userDetail[0] as! NSDictionary
print(self.tmpDic)
var optionsdic = NSDictionary()
optionsdic = self.tmpDic.value(forKey:"options") as! NSDictionary
print(optionsdic)
self.arrayOfKeys = optionsdic.allKeys as NSArray
print(self.arrayOfKeys)
if (self.arrayOfKeys.contains("color"))
{
print("color")
self.colorarray = optionsdic.value(forKey:"color") as! NSArray
print(self.colorarray.count)
for index in 0..<self.colorarray.count
{
var dic = NSDictionary ()
dic = self.colorarray .object(at: index) as! NSDictionary
self.colorarrayobject .add(dic)
print(dic)
}
print(self.colorarrayobject)
}
else {
var defaultarray = NSArray()
defaultarray = optionsdic.value(forKey:"default") as! NSArray
print(defaultarray)
self.element0array = defaultarray[0] as! NSArray
print(self.element0array)
self.dic = self.element0array[0] as! NSDictionary
print(dic)
self.arr5 = self.dic .value(forKey: "values") as! NSArray
print(self.arr5)
for iteams in 0..<self.arr5.count
{
var type = String()
type = ((self.arr5[iteams]as! NSDictionary)["label"]! as? String)!
self.configeresizeaarray.append(type)
}
print("default")
}
}
self.imagearray = self.array[0] as! NSArray
for items in 0..<self.imagearray.count
{
var type = String()
type = ((self.imagearray [items]as! NSDictionary)["image"]! as? String)!
self.cell0imagearray.append(type)
}
self.count = self.imagearray.count as Int
self.configurePageControl()
self.tableView.reloadData()
}
else
{
}
}
else
{
}