How Do I Post an Alamofire JSON Request on button press? - ios

I am attempting to build a simple iOS app that features a login but first I want to make it so that pressing the "Continue" button on sign up posts data to the REST api. I can't successfully bind it to a button press for some reason. The code below doesn't know what inputboxes is. I ctr+dragged the button then added it in.
import Alamofire
import SwiftyJSON
import UIKit
class SignUpViewController: UIViewController {
var onButtonTapped : (() -> Void)? = nil
#IBOutlet weak var usernametextfield: UITextField!
#IBOutlet weak var passwordtextfield: UITextField!
#IBOutlet weak var emailtextfield: UITextField!
#IBOutlet weak var loginMessage: UILabel!
#IBAction func continueButtonPressed(sender: AnyObject) {
// POST requests dont need a response!
Alamofire.request(.POST, endpoint, parameters: inputboxes)
}
lazy var json : JSON = JSON.null
let endpoint = "anyapi.com/api/users"
override func viewDidLoad() {
digestUser()
}
func digestUser() {
let passwordInput = self.passwordtextfield.text
let usernameInput = self.usernametextfield.text
let emailInput = self.emailtextfield.text
let inputboxes: [String:AnyObject] = [
"hashword": passwordInput!,
"username": usernameInput!,
"email": emailInput!
]
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
Edit: Alamofire wasn't working properly because I forgot to include: "https://" before the URL

Declare inputboxes as instance variable below your outlets: var inputboxes: [String:AnyObject] = [:] and it should work.

Alamofire.request(.POST, BASE_URL , parameters: parameters as? [String : AnyObject])
.responseJSON { response in
if let JSON = response.result.value {
print("Success with JSON: \(JSON)")
}
}

Try to declare inputboxes outside function so it is accessible in the whole class.

Related

Data persistence after call to webservice iOS

Sorry for my English.
I have many problems in achieving data persistence after call the webservice .
Everything works correctly but I need to use that data in other methods .
As seen in the image once charge data on the screen then I want to give the user the option to use them well or load new data . But not as saving the data recovery webservice .
I was trying to use NSUserDefault but neither work .
From already thank you very much , any response is helpful .
Regards!
ViewController:
class ViewController: UIViewController {
let defaults = NSUserDefaults.standardUserDefaults()
#IBOutlet weak var myLabel2: UITextField!
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.
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
let newVC : SecondViewController = segue.destinationViewController as! SecondViewController
let telefono = myLabel2.text!
newVC.recibirString = telefono
}
#IBAction func mostrarAlgo(sender:AnyObject) {
//something
}
}
SecondViewController:
class SecondViewController: UIViewController {
let defaults = NSUserDefaults.standardUserDefaults()
var tel: String = ""
#IBOutlet weak var myLabel1: UITextField!
#IBOutlet weak var myLabel3: UITextField!
#IBOutlet weak var myLabel4: UITextField!
#IBOutlet weak var myLabel5: UITextField!
#IBOutlet weak var myLabel6: UITextField!
#IBOutlet weak var myLabel7: UITextField!
#IBOutlet weak var myLabel8: UITextField!
var recibirString = String()
override func viewDidLoad() {
super.viewDidLoad()
myLabel1.text = recibirString
performRequestAndUpdateUI2(myLabel1.text!)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func performRequestAndUpdateUI2(tel:String) {
WebService.llamarWebService (tel,completionHandler: { datos in
guard datos.count == 5 else {
self.myLabel8.text = ("El número ingresado no está registrado.")
return
}
guard datos[0] as! String == "OK" else {
print("not everything was OK")
return
}
dispatch_async(dispatch_get_main_queue()) {
self.myLabel3.text = datos[0] as? String
self.myLabel4.text = datos[1] as? String
self.myLabel5.text = datos[2] as? String
self.myLabel6.text = datos[3] as? String
self.myLabel7.text = datos[4] as? String
self.myLabel8.text = ("Usted está registrado.")
//need save this data
}
})
}
#IBAction func guardarDatos(sender: AnyObject) {
// do something
}
}
WebService:
class WebService{
var telefono = ""
class func llamarWebService(telefono:String, completionHandler: (datos:NSArray)->()){
let urlPath = "http://xxxxxxxxxxx.com.ar/xxxxxxxxxx/forwarder_number?phone=\(telefono)"
let url = NSURL(string: urlPath)
let session = NSURLSession.sharedSession()
var arreglo:NSArray!
let task = session.dataTaskWithURL(url!,completionHandler: {data,response,error -> Void in
if (error != nil){
print(error?.localizedDescription)
}
let nsdata: NSData = NSData(data: data!)
arreglo = self.retornarDatos(nsdata)
completionHandler(datos: arreglo)
})
task.resume()
}
class func retornarDatos(nsdata: NSData)-> Array<String>{
let datos = NSString(data:nsdata,encoding: NSUTF8StringEncoding)
let partes = datos?.componentsSeparatedByString(",")
var arreglo : [String] = []
for i in partes!{
arreglo.append(i)
}
return arreglo
}
}

Swift custom updateUI() function does not work on viewDidLoad

I'm working on an app, that should request some data from my server. I'm using Alamofire to do that, and then use SWXMLHash to parse the XML data. There are two View Controllers, on the first one I can write a shipment number, then override function prepareForSegue and send that number to the next View Controller that should display data from server and updateUI on viewDidLoad, but it does not. Where is a problem?
My Class:
class Shipment {
private var _shipmentNumber: String!
private var _shipmentStatus: String!
private var _trackURL: String!
var shipmentNumber: String {
if _shipmentNumber == nil {
_shipmentNumber = ""
}
return _shipmentNumber
}
var shipmentStatus: String {
if _shipmentStatus == nil {
_shipmentStatus = ""
}
return _shipmentStatus
}
init(spNumber: String) {
self._shipmentNumber = spNumber
_trackURL = "..."
}
func requestXmlInformation(completed: DownloadComplete) {
let url = NSURL(string: _trackURL)!
Alamofire.request(.GET, url).responseData { response in
if let xmlToParse = response.data as NSData! {
let xml = SWXMLHash.parse(xmlToParse)
do {
let xmlSpWeight = try xml["fmresultset"]["resultset"]["record"]["field"].withAttr("name", "ТotalWeight")["data"].element!.text! as String
self._shipmentStatus = xmlSpStatus
print(self._shipmentStatus)
} catch let err as NSError {
print(err.debugDescription)
}
}
}
}
}
My Second View Controller
#IBOutlet weak var numberLbl: UILabel!
#IBOutlet weak var weightLbl: UILabel!
#IBOutlet weak var statusLbl: UILabel!
#IBOutlet weak var packageQtyLbl: UILabel!
var shipment: Shipment!
override func viewDidLoad() {
super.viewDidLoad()
shipment.requestXmlInformation { () -> () in
self.updateUi()
print(self.statusLbl.text)
}
}
updateUI function:
func updateUi() {
numberLbl.text = shipment.shipmentNumber
weightLbl.text = shipment.shipmentWeight
statusLbl.text = shipment.shipmentStatus
packageQtyLbl.text = shipment.shipmentPackageQty
}
It prints data in terminal but i think updateUI function does not work.
Make sure that the code in your requestXmlInformation closure is called on the main thread. You shouldn't update the UI in background threads.
shipment.requestXmlInformation { () -> () in
dispatch_async(dispatch_get_main_queue(), { () -> Void in
self.updateUi()
print(self.statusLbl.text)
})
}
Also, you don't seem to call the complete closure anywhere in your requestXmlInformation method

.Type does not have a member named "x" when trying to get variable value in Struct(Swift)

I am currently working on an app and I am having an issue. When the user login the webservice, if the login is successful the server responds with JSON, where we use the "firstName" and "SecondName" to then create our "User" which is a struct defined in another file called User.swift . Then, what I want to do is user the "firstName" that has been given to the "User struct" as a UILabel in my homepageview that comes after a successful login. when I try to give my label User.prenom(which is firstName in french) I get the error: User.type does not have a member called...
Here is my code:
the client file where the Login Method is defined:
import Foundation
import Alamofire
import SwiftyJSON
private let _instance = Client()
class Client {
// Router is used to do a request to the server.
private enum Router: URLRequestConvertible {
private static let baseURL = "https://mobile.uqam.ca/portail_etudiant/"
// stores the authentication token.
static var code_perm: String?
static var nip:String?
// Login request.
case Login(String, String)
// URLRequestConvertible protocol.
var URLRequest: NSURLRequest {
// Returns the path, http method and parameters for the request.
var (path: String, method: Alamofire.Method, parameters: [String: AnyObject]) = {
switch self {
case .Login (let code_perm, let nip):
let params: [String: AnyObject] = [
"code_perm": code_perm,
"nip": nip,
]
return ("proxy_dossier_etud.php", .POST, params)
}
}()
// Setup the URLRequest.
let url = NSURL(string: Router.baseURL)
let urlRequest = NSMutableURLRequest(URL: url!.URLByAppendingPathComponent(path))
urlRequest.HTTPMethod = method.rawValue
if let code_perm = Router.code_perm {
if let nip = Router.nip{
parameters["nip"] = nip
parameters["code_perm"] = code_perm
}
}
let encoding = Alamofire.ParameterEncoding.URL
return encoding.encode(urlRequest, parameters: parameters).0
}
}
// Singleton
class var sharedInstance: Client {
return _instance
}
private init() {}
// Login logs in the user with his email and password.
func login(code_perm:String, nip:String, callback:(LoginResponse?) -> Void) {
Alamofire.request(Router.Login(code_perm, nip)).responseJSON { (_, _, data, error) in
if(error != nil) {
callback(nil)
return
}
var json = JSON(data!)
let prenom = json["socio"]["prenom"].stringValue
let nom = json["socio"]["nom"].stringValue
Router.code_perm = code_perm
Router.nip = nip
callback(LoginResponse(
user: User(prenom: prenom,nom: nom)
))
}
}
}
the loginViewController where the login function is called
import UIKit
class LoginViewController: UIViewController {
#IBOutlet weak var LoginScreenImage: UIImageView!
#IBOutlet weak var codeTextField: UITextField!
#IBOutlet weak var nipTextField: UITextField!
#IBOutlet weak var loadingLogin: UIActivityIndicatorView!
let client = Client.sharedInstance
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
LoginScreenImage.image = UIImage(named: "UQAMLOGO")
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
#IBAction func connect() {
let code_perm = codeTextField.text
let nip = nipTextField.text
self.loadingLogin.startAnimating()
if code_perm != "" && nip != "" {
client.login(code_perm, nip: nip, callback: { (response) in
if let response = response {
self.loadingLogin.stopAnimating()
let homeViewController = self.storyboard!.instantiateViewControllerWithIdentifier("HomeViewController") as HomeViewController
self.showViewController(homeViewController, sender: self)
} else {
self.loadingLogin.stopAnimating()
let badLogin = UIAlertController(title: "Échec de connexion", message: "La combinaison du code permanent et du nip n'est pas bonne", preferredStyle: .Alert)
let reessayer = UIAlertAction(title: "Réessayer", style: .Default, handler: { (reessayer) -> Void in
self.dismissViewControllerAnimated(true , completion: nil)
})
badLogin.addAction(reessayer)
self.presentViewController(badLogin, animated: true, completion: nil)
}
})
}
}
}
the User.swift while where the user struct is
import Foundation
struct User {
var prenom :String
var nom: String
}
struct LoginResponse {
var user: User
}
and finally the HomePageViewController where I try to give the value to my label:
import UIKit
class HomeViewController: UIViewController {
#IBOutlet weak var schedule: UIImageView!
#IBOutlet weak var courses: UIImageView!
#IBOutlet weak var email: UIImageView!
#IBOutlet weak var grades: UIImageView!
#IBOutlet weak var bienvenueLabel: UILabel!
let client = Client.sharedInstance
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
schedule.image = UIImage(named:"schedule")
courses.image = UIImage(named: "courses")
email.image = UIImage(named:"mail")
grades.image = UIImage(named:"grades")
bienvenueLabel.text = User.prenom
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
Thanks everyone for the help and have a great day/night
Charles
You are accessing the class instead of an instance. Instead, you should pass the response instance to your HomeViewController:
class HomeViewController : .. {
// ...
var loginResponse : LoginResponse
// ...
override func viewDidLoad() {
// ...
bienvenueLabel.text = loginResponse.user.prenom
}
}
// ...
client.login(code_perm, nip: nip, callback: { (response) in
if let loginResponse = response as LoginResponse {
self.loadingLogin.stopAnimating()
let homeViewController = self.storyboard!.instantiateViewControllerWithIdentifier("HomeViewController") as HomeViewController
homeViewController.loginResponse = loginResponse
// assign your instance ^^^^^^^^^^^^^^^^^^^^^^^^
self.showViewController(homeViewController, sender: self)
}
You are accessing the class instead of an instance. Instead, you should pass the response instance to your HomeViewController:
class HomeViewController : .. {
// ...
var loginResponse : LoginResponse
// ...
override func viewDidLoad() {
// ...
bienvenueLabel.text = loginResponse.user.prenom
}
}
// ...
client.login(code_perm, nip: nip, callback: { (response) in
if let loginResponse = response as LoginResponse {
self.loadingLogin.stopAnimating()
let homeViewController = self.storyboard!.instantiateViewControllerWithIdentifier("HomeViewController") as HomeViewController
homeViewController.loginResponse = loginResponse
// assign your instance ^^^^^^^^^^^^^^^^^^^^^^^^
self.showViewController(homeViewController, sender: self)
}
This really isn't very good structure, but it should at least answer your question.

iOS Swift JSON retrieving data issue

What I would like to do: Retrieve in my static cell Labels the returns of my JSON Query.
What is my problem: I can not do it. Can not return single value to change my label.
What did I try: First of all, I install the Alamofire and SwiftyJSON library. Secondly I did my PHP Script which result in correct JSON Data. I create the labels in my storyboard and created the #IBOutlet UILabel in my UITableViewController. Thirdly, I did my Alamofire request and I can get the output of my whole Data.
What I can not do: I can not get the individuals fields to replace my labels. I would like to retrieve [username] [gender] [location] [birthday] [mobilephone] [signature] from my JSON and replace my labels with this return. But when I would like to retrieve [mobilephone] for example, I got nil result.
I also think I got a problem with my JSON as it does not seem to return Array but only Dictionary
my tableviewcontroller
import UIKit
import Alamofire
import SwiftyJson
class PersonalDetails: UITableViewController {
required init(coder aDecoder: NSCoder) {
println("init PersonalDetails")
super.init(coder: aDecoder)
}
deinit {
println("deinit PersonalDetails")
}
var usersData = [PersonalDetailsData]()
#IBOutlet weak var dataUsername: UILabel!
#IBOutlet weak var dataGender: UILabel!
#IBOutlet weak var dataArea: UILabel!
#IBOutlet weak var dataBirthday: UILabel!
#IBOutlet weak var dataMobilePhone: UILabel!
#IBOutlet weak var dataSignature: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
let prefs:NSUserDefaults = NSUserDefaults.standardUserDefaults()
var username = prefs.valueForKey("USERNAME") as NSString
//load and parse the JSON into an array
Alamofire.request(.GET, "http://mywebsite/app/data/jsonpersodata.php", parameters: ["username": username]).responseJSON { (request, response, data, error) in
let swiftyJSONObject = JSON(data!)
if (error != nil)
{
// got an error in getting the data, need to handle it
println("error calling GET usersdata")
println(error)
}
else if let data: AnyObject = data
{
// handle the results as JSON, without a bunch of nested if
let userdata = JSON(data)
if let mobilephone: String = userdata [0]["mobilephone"].stringValue {
self.dataMobilePhone.text = mobilephone
}
}
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
#IBAction func logoutTapped(sender : UIButton) {
let appDomain = NSBundle.mainBundle().bundleIdentifier
NSUserDefaults.standardUserDefaults().removePersistentDomainForName(appDomain!)
self.performSegueWithIdentifier("logout", sender: self)
}
}
my class
import Foundation
class PersonalDetailsData: NSObject {
var dataUsername:String?
var dataGender:String?
var dataArea:String?
var dataBirthday:String?
var dataMobilePhone:String?
var dataSignature:String?
}
my json
[
{
"username": "username1",
"gender": "?",
"location": "??",
"birthday": "1983/01\/16",
"mobilephone": "136777777",
"signature": null
}
]
my php
<?php
header('Content-type: application/json');
/* include db.config.php */
include_once("config.php");
// Get user id
$id = isset($_GET['username']) ? mysql_real_escape_string($_GET['username']) : “”;
if(empty($id)){
$data = array ("result" => 0, "message" => 'Wrong user id');
} else {
// get user data
$sql = mysql_query("SELECT username, gender, location, birthday, mobilephone, signature FROM users WHERE username='$id'");
$data = array ();
while ($row = mysql_fetch_array($sql, MYSQL_ASSOC)) {
$row_array['username'] = $row['username'];
$row_array['gender'] = $row['gender'];
$row_array['location'] = $row['location'];
$row_array['birthday'] = $row['birthday'];
$row_array['mobilephone'] = $row['mobilephone'];
$row_array['signature'] = $row['signature'];
//push the values in the array
array_push($data,$row_array);
}
echo json_encode($data);
mysql_close($conn);
/* JSON Response */
}
?>
The JSON you're returning is actually an array, not a direct value of the object you're expecting (see the [ and ] in your raw JSON? They represent an array).
Here's what you could do to parse the first item in your JSON array:
// handle the results as JSON, without a bunch of nested if
let userdata = JSON(data)
if let mobilephone: String = userdata[0]["mobilephone"].string {
self.dataMobilePhone.text = mobile phone
}
// etc.

Zoopla API, try to get some values from JSON in swift

I try to get some JSON values from the Zoopla API but that not response like other API, most probably is my fault and i can't see the error, someone can check my code and find out if is something missing?
Regards,
import UIKit
class ViewController: UIViewController {
#IBOutlet var areaLabel : UILabel!
#IBOutlet var streetLabel : UILabel!
#IBOutlet var townLabel : UILabel!
#IBOutlet var postcodeLabel : UILabel!
#IBOutlet var outputTypeLabel : UILabel!
#IBOutlet var posterImageView : UIImageView!
#IBOutlet var completar : UITextField!
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 buttonPressed (sender: UIButton)
{
self.searchZoopla("\(completar)")
}
func searchZoopla (forContent : String) {
var spacelessString = forContent.stringByAddingPercentEscapesUsingEncoding(NSUTF8StringEncoding)
var urlPath = NSURL(string: "http://api.zoopla.co.uk/api/v1/zed_indices?postcode=\(spacelessString)&output_type=outcode&api_key=<api_key>")
var session = NSURLSession.sharedSession()
var task = session.dataTaskWithURL(urlPath) {
data, response, error -> Void in
if ((error) != nil) {
println(error.localizedDescription)
}
var jsonError : NSError?
var jsonResult = NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.MutableContainers, error: &jsonError) as Dictionary<String, String>
if(jsonError != nil) {
println(error.localizedDescription)
}
self.areaLabel.text = jsonResult["area_name"]
self.streetLabel.text = jsonResult["street"]
self.townLabel.text = jsonResult["town"]
self.postcodeLabel.text = jsonResult["postcode"]
self.outputTypeLabel.text = jsonResult ["area_url"]
}
}
}
Here in your URL if you want JSON response from zoopla then you need to specify it by .js otherwise response will be in xml. So your URL will be http://api.zoopla.co.uk/api/v1/zed_indices.js?postcode=...
The default assumed output format in zoopla will be XML and others can be requested using a file extension, for example using a ".js" suffix would produce JavaScript output.

Resources