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.
Related
I am trying to integrate Stripe into my app and I followed a tutorial that seems to be out of date. This is how my code looks:
import Foundation
import UIKit
import Stripe
import AFNetworking
class PaymentViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
}
#IBOutlet weak var emailTextField: UITextField!
#IBOutlet weak var cardNumberTextField: UITextField!
#IBOutlet weak var expirationDateTextField: UITextField!
#IBOutlet weak var cvcTextField: UITextField!
#IBAction func payButton(_ sender: Any) {
// Initiate the card
let stripCard = STPCardParams()
// Split the expiration date to extract Month & Year
if self.expirationDateTextField.text?.isEmpty == false {
let expirationDate = self.expirationDateTextField.text?.components(separatedBy: "/")
let expMonth = UInt((expirationDate?[0])!)
let expYear = UInt((expirationDate?[1])!)
// Send the card info to Strip to get the token
stripCard.number = self.cardNumberTextField.text
stripCard.cvc = self.cvcTextField.text
stripCard.expMonth = expMonth!
stripCard.expYear = expYear!
}
var underlyingError: NSError?
stripCard.validateCardReturningError(&underlyingError)
if underlyingError != nil {
self.handleError(underlyingError!)
return
}
}
}
I am getting an error in this block of code:
var underlyingError: NSError?
stripCard.validateCardReturningError(&underlyingError)
if underlyingError != nil {
self.handleError(underlyingError!)
return
}
}
The error says that .validateCardReturningError(&underlyingError) is deprecated and that I should use STPCardValidator instead. I tried doing so but could not fix it. Help will be greatly appreciated.
according to #jflinter . you should flowing this way
let cardParams = STPCardParams()
cardParams.number = ...
cardParams.expMonth = ...
cardParams.expYear = ...
cardParams.cvc = ...
if STPCardValidator.validationStateForCard(cardParams) == .Valid {
// the card is valid.
}
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
}
}
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.
I'm trying to pass data from categoriesTableViewController to quotesContentViewController.Here is the code:
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
let DestViewController : quotesContentsViewController = segue.destinationViewController as! quotesContentsViewController
let categoryString = sender?.titleLabel!!.text
let quotesContentURLString = "http://quotes.rest/qod.json?category=" + categoryString!
if let quotesContentURL = NSURL(string: quotesContentURLString) {
self.updateQuotesContentFeed(quotesContentURL, completion: {(feed) -> Void in
//print(feed!.item.imageURL)*
DestViewController.feed = feed!
DestViewController.author.text = feed!.item.author
DestViewController.quotes.text = feed!.item.quotes
DestViewController.imageURLString = feed!.item.imageURL
//print ("updateQuotesContentFeed")*
//print(DestViewController.imageURLString)*
})
}
}
DestViewController.author.text and DestViewController.quotes.text are passed to quotesContentViewController successfully,but DestViewController.imageURLString failed. print(DestViewController.imageURLString) printed out exactly what I want.(That's why I thought it should have been passed successfully)
Now the code of quotesContentViewController:
import UIKit
class quotesContentsViewController: UIViewController {
var feed: quotesContentFeed?
var urlSession: NSURLSession!
weak var dataTask: NSURLSessionDataTask?
var imageURLString: String!
#IBOutlet var quotes: UILabel!
#IBOutlet var author: UILabel!
#IBOutlet var backgroundImage: UIImageView!
override func viewDidLoad() {
//let request = NSURLRequest(URL: self.imageURL!)
//dataTask = self.urlSession.dataTaskWithRequest(request) {(data, response, error) -> Void //in
//NSOperationQueue.mainQueue().addOperationWithBlock({ () -> Void in
//if error == nil && data != nil {
//let image = UIImage(data: data!)
//self.backgroundImage.image = image
//}
//})
//}
print("viewDidLoad")
print(self.imageURLString)
super.viewDidLoad()
// Do any additional setup after loading the view.
}
When I run it ,it throw an errorfatal error: unexpectedly found nil while unwrapping an Optional value.You can have a look at the screenshot of the error below.
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.