How to pass data from view controller to data model in swift - swift

I am building a simple app that talks to a web service.
I have used the delegates method to communicate data (from my model to view controller).
But I am not sure how to read the data from view controller (text_field.text) in my model. I need to do that so that I can pass the right parameter to my webservice
my view controller is:
import UIKit
class ViewController: UIViewController,HomeModelDelegate {
var homeModel = HomeModel()
#IBOutlet weak var loginid: UITextField!
#IBOutlet weak var pwd: UITextField!
#IBAction func submit(_ sender: UIButton) {
homeModel.chkpwd()
//Here viewcontroller is assigning itself to the homemodel's delegate property
homeModel.delegate = self
}
override func viewDidLoad() {
super.viewDidLoad()
loginid.layer.cornerRadius=10
pwd.layer.cornerRadius = 10
}
func itemsDownloaded(locations: [Location]) {
loginid.text = locations[0].pwd
}
}
My model code is:
import UIKit
protocol HomeModelDelegate{
func itemsDownloaded(locations:[Location])
}
class HomeModel: NSObject
{
var delegate:HomeModelDelegate?
func chkpwd()
{
//Hit the webservice url
let x = ViewController()
let z = x.loginid
let serviceUrl = "http://www.xyz.in/webservice.php?loginid=(loginid.text)"
//download the json data
let url = URL(string: serviceUrl)
if let url = url {
let session = URLSession(configuration: .default)
let task = session.dataTask(with: url, completionHandler:
{ (data, response, error) in
if error == nil {
//succeeded
self.parseJson(data!)
}
else {
//failed
}
})
task.resume()
}
}
func parseJson(_ data:Data){
var locArray = [Location]()
do{
let jsonArray = try JSONSerialization.jsonObject(with: data, options: []) as! [Any]
for jsonResult in jsonArray{
let jsonDict = jsonResult as! [String:String]
let loc = Location(pwd: jsonDict["loginid"]!, loginid: jsonDict["pwd"]!)
locArray.append(loc)
//pass the location back to the delegate
delegate?.itemsDownloaded(locations: locArray)
}
}
catch{
print("An error occured")
}
}
}

Please try this :
import UIKit
class ViewController: UIViewController,HomeModelDelegate {
var homeModel = HomeModel()
#IBOutlet weak var loginid: UITextField!
#IBOutlet weak var pwd: UITextField!
#IBAction func submit(_ sender: UIButton) {
homeModel.z = loginid.text! // ASSIGNING z here
homeModel.chkpwd()
//Here viewcontroller is assigning itself to the homemodel's delegate property
homeModel.delegate = self
}
override func viewDidLoad() {
super.viewDidLoad()
loginid.layer.cornerRadius=10
pwd.layer.cornerRadius = 10
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func itemsDownloaded(locations: [Location]) {
loginid.text = locations[0].pwd
}
}
Model :
import UIKit
protocol HomeModelDelegate{
func itemsDownloaded(locations:[Location])
}
class HomeModel: NSObject
{
var z:String = "" // INITIALIZING z
var delegate:HomeModelDelegate?
func chkpwd()
{
print(z) // CALLING z
//Hit the webservice url
let serviceUrl = "http://www.xyz.in/webservice.php?loginid=(loginid.text)"
//download the json data
let url = URL(string: serviceUrl)
if let url = url {
let session = URLSession(configuration: .default)
let task = session.dataTask(with: url, completionHandler:
{ (data, response, error) in
if error == nil {
//succeeded
self.parseJson(data!)
} else {
//failed
}
})
task.resume()
}
}
func parseJson(_ data:Data){
var locArray = [Location]()
do{
let jsonArray = try JSONSerialization.jsonObject(with: data, options: []) as! [Any]
for jsonResult in jsonArray{
let jsonDict = jsonResult as! [String:String]
let loc = Location(pwd: jsonDict["loginid"]!, loginid: jsonDict["pwd"]!)
locArray.append(loc)
//pass the location back to the delegate
delegate?.itemsDownloaded(locations: locArray)
}
} catch {
print("An error occured")
}
}
}

Related

Realm list data in Swift is saving but not loading properly. New to this and not sure what the problem is. Code below

Below is my main view controller. The user selects images of clothing which are then categorized using CoreML and given a filename. Then, data is saved to Realm. When I call the function loadClothing(), the array is empty even though items were added during func detect. Any help is much appreciated!
import UIKit
import PhotosUI
import RealmSwift
import CoreML
import Vision
class ViewController: UIViewController, PHPickerViewControllerDelegate {
#IBOutlet weak var shoesImageView: UIImageView!
#IBOutlet weak var shirtImageView: UIImageView!
#IBOutlet weak var pantsImageView: UIImageView!
var documentsUrl: URL {
return FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!
}
let realm = try! Realm()
var clothing: Results<Clothing>?
override func viewDidLoad() {
super.viewDidLoad()
loadClothing()
let clothingArray = Clothing()
print(clothingArray)
}
#IBAction func addClothesButton(_ sender: UIBarButtonItem) {
pickPhotos()
}
#IBAction func randomizeButton(_ sender: UIBarButtonItem) {
loadClothing()
let clothingArray = Clothing()
print(clothingArray)
shirtImageView.image = load(fileName: clothingArray.shirtImages.randomElement()!)
pantsImageView.image = load(fileName: clothingArray.pantsImages.randomElement()!)
shoesImageView.image = load(fileName: clothingArray.shoesImages.randomElement()!)
}
//MARK: - PHPickerViewController
#objc func pickPhotos() {
var config = PHPickerConfiguration()
config.selectionLimit = 25
config.filter = PHPickerFilter.images
let pickerViewController = PHPickerViewController(configuration: config)
pickerViewController.delegate = self
self.present(pickerViewController, animated: true, completion: nil)
}
// MARK: - PHPickerViewControllerDelegate
func picker(_ picker: PHPickerViewController, didFinishPicking results: [PHPickerResult]) {
picker.dismiss(animated: true, completion: nil)
for result in results {
result.itemProvider.loadObject(ofClass: UIImage.self) {(object, error) in
if let image = object as? UIImage {
DispatchQueue.main.async {
guard let fileName = result.itemProvider.suggestedName else {
fatalError("Could not retrieve file name.")
}
print(fileName)
guard let ciImage = CIImage(image: image) else {
fatalError("Could not convert to CI Image.")
}
self.detect(image: ciImage, fileName: fileName)
}
}
}
}
}
// MARK: - Core ML
func detect(image: CIImage, fileName: String) {
guard let model = try? VNCoreMLModel(for: ClothingClassifier(configuration: MLModelConfiguration()).model) else {
fatalError("Loading CoreML Model failed.")
}
let request = VNCoreMLRequest(model: model) { (request, error) in
guard let results = request.results as? [VNClassificationObservation] else {
fatalError("Model failed to process image.")
}
let newClothing = Clothing()
if let firstResult = results.first {
let uiImage = UIImage(ciImage: image)
if firstResult.identifier.contains("shirts") {
newClothing.shirtImages.append(fileName)
} else if firstResult.identifier.contains("pants"){
newClothing.pantsImages.append(fileName)
} else if firstResult.identifier.contains("shoes") {
newClothing.shoesImages.append(fileName)
}
self.save(clothing: newClothing)
print(newClothing)
}
}
let handler = VNImageRequestHandler(ciImage: image)
do {
try handler.perform([request])
}
catch {
print(error)
}
}
// MARK: - Data Manipulation Methods
func save(clothing: Clothing) {
do {
try realm.write {
realm.add(clothing)
}
} catch {
print("Error saving uploaded clothing. \(error)")
}
}
func loadClothing() {
clothing = realm.objects(Clothing.self)
print("loaded")
}
private func load(fileName: String) -> UIImage? {
let fileURL = documentsUrl.appendingPathComponent(fileName)
do {
let imageData = try Data(contentsOf: fileURL)
return UIImage(data: imageData)
} catch {
print("Error loading image : \(error)")
}
return nil
}
}
Clothing Class
import Foundation
import RealmSwift
class Clothing: Object {
let shirtImages = List<String>()
let pantsImages = List<String>()
let shoesImages = List<String>()
}

How to do login using MVVM?

I am still quite new to iOS development and I am trying to teach myself good coding practices and design patterns in Swift starting with MVVM. I need to pass the data from a completion handle in my ServiceCall class to my ViewModel. I would like assistance in understanding how I can do it and also guidance in best practices using MVVM on my code.
This is what I have done so far:
Model
struct Login {
var appVersion: String ?
var deviceID : String ?
var deviceOS : String ?
var password : String ?
var username : String ?
}
Service Call / API Client
class LoginServiceCall : NSObject, URLSessionDelegate {
let viewResponse = ThrowResponse()
func requestLogin(request: URLRequest, requestObject: Login, completion: #escaping ([NSDictionary] ? ) -> Void) {
let searchParams = Login.init(appVersion: requestObject.appVersion, deviceID: requestObject.deviceID, deviceOS: requestObject.deviceOS, password: requestObject.password, username: requestObject.username)
var request = request
request.httpMethod = "POST"
do {
request.httpBody = try JSONSerialization.data(withJSONObject: searchParams, options: .prettyPrinted)
} catch let error {
print(error.localizedDescription)
}
request.addValue("application/json", forHTTPHeaderField: "Content-Type")
let session = URLSession.shared
let task = session.dataTask(with: request, completionHandler: {
data, response, error -> Void in
do {
let json = try JSONSerialization.jsonObject(with: data!) as ? Dictionary<String, Any>
//completion(json!)
// This is where i would like to pass the dictionary data
} catch {
DispatchQueue.main.async {
self.viewResponse.dismissLoader()
self.viewResponse.showFailureAlert(title: "Failure", message: "")
completion(nil)
}
}
})
task.resume()
}
}
View Controller
class LoginViewController: UIViewController, UITextFieldDelegate {
#IBOutlet var loginButton: UIButton!
#IBOutlet var usernameOrEmailTextField: UITextField ?
#IBOutlet var passwordTextField : UITextField ?
var serviceBalance = 0.0
let defaults = UserDefaults.standard
var Reach : Reachability ? = Reachability()
var viewModel : LoginViewModel ?
override func viewDidLoad() {
super.viewDidLoad()
usernameOrEmailTextField?.delegate = self
passwordTextField?.delegate = self
}
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
usernameOrEmailTextField?.resignFirstResponder()
passwordTextField?.resignFirstResponder()
return (true)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
#IBAction func loginButton(_ sender: Any) {
viewModel?.setLoginObject(withUsername: usernameOrEmailTextField?.text, withPassword : passwordTextField?.text)
}
#IBAction func forgotPasswordButton(_ sender: Any) {
self.performSegue(withIdentifier: "forgotPasswordSegue", sender: nil)
}
#IBAction func registerButton(_ sender: Any) {
self.performSegue(withIdentifier: "registerUserSegue", sender: nil)
}
}
ViewModel
class LoginViewModel: NSObject {
var Reach: Reachability? = Reachability()
var login: Login?
var homeViewDictionary: [NSDictionary]?
var APIClient: LoginServiceCall!
var request = URLRequest(url: URL(string: "http://myapiuser/login")!)
func setLoginObject(withUsername username: String?, withPassword password: String?){
login?.username = username
login?.password = password
login?.appVersion = self.getAppVersion()
login?.deviceID = self.getDeviceID()
login?.deviceOS = self.getDeviceOs()
APIClient.requestLogin(request: request, requestObject: login! { (AppDictionary) in
DispatchQueue.main.async {
self.homeViewDictionary = AppDictionary
}
}, completion: ())
}
func getAppVersion() -> String { return "0.2" }
func getDeviceID() -> String {
if let deviceid = UIDevice.current.identifierForVendor?.uuidString { return deviceid }
}
func getDeviceOs() -> String {
let systemVersion = UIDevice.current.systemVersion
let model = UIDevice.current.model
return systemVersion+" "+model
}
}

User input as global variable to display text from different views

I have a main View Controller than has a "Sign In" button if a user is not logged in. I then have another view controller "Login View Controller" that checks if user exists and then logs them in. Once a user has logged in via the login View Controller. I want the text of my Sign In Button on my main view controller to change to their email that they used to login. I am using a struct to try and accomplish this. My struct email variable seems to work within my Login View Controller but then does not change the string value of my button on the main view controller. Any help would be greatly appreciated.
LoginViewController.swift
class LoginViewController: NSViewController {
#IBOutlet weak var userEmailTextField: NSTextField!
struct GloablUserEmail {
static var user_status = Bool()
static var user_email = String()
}
#IBAction func LoginPushed(_ sender: AnyObject) {
GloablUserEmail.user_email = userEmailTextField.stringValue
let requestURL: NSURL = NSURL(string: "myurl")!
let urlRequest: NSMutableURLRequest = NSMutableURLRequest(url: requestURL as URL)
urlRequest.httpMethod = "POST"
let postString = "user_email=\(GloablUserEmail.user_email)&user_password=\(user_password)"
urlRequest.httpBody = postString.data(using: String.Encoding.utf8);
let session = URLSession.shared
let task = session.dataTask(with: urlRequest as URLRequest) {
(data, response, error) -> Void in
let httpResponse = response as! HTTPURLResponse
let statusCode = httpResponse.statusCode
if (statusCode == 200) {
do{
let json = try JSONSerialization.jsonObject(with: data!, options:.mutableContainers) as? NSDictionary
if let parseJSON = json {
let resultValue = parseJSON["Status"] as? String
print("result: \(resultValue)")
self.isUserRegistered = false
if(resultValue=="Success"){
GloablUserEmail.user_status = true;
UserDefaults.standard.set(true, forKey: "isUserLoggedIn");
UserDefaults.standard.synchronize();
}
}
}catch{
print("Error parsing the JSON: \(error)")
}
}
}
task.resume()
}
}
Once the result is successful and the user is verified to exist then I assign the global user_status to true.
MainViewController.swift
class ViewController: NSViewController {
#IBOutlet weak var SignInButton: NSButton!
override func viewDidLoad() {
super.viewDidLoad()
if(LoginViewController.GloablUserEmail.user_status == false){
SignInButton.title = "Sign In"
}else if(LoginViewController.GloablUserEmail.user_status == true){
SignInButton.title = LoginViewController.GloablUserEmail.user_email
}
}
}
You can user a simple UserDefaults extension:
extension UserDefaults{
var isLoggedIn: Bool{
return bool(forKey: "loggedIn")
}
func set(loggedIn value: Bool){
set(value, forKey: "loggedIn")
synchronize()
}
}
To set login status:
let defaults = UserDefaults.standard
defaults.set(loggedIn: true)
And check its value:
let defaults = UserDefaults.standard
defaults.isLoggedIn

JSON parsing error in Swift 2.0

I'm learning to code in Swift 2.0 and I got stuck while compiling it into simulator. The self.setLabels(data!) line displays an info Thread 1: EXC_BAD_INSTRUCTION. Can anyone help me with this? I'm doing a trial-and-error technique but no luck yet...
lass ViewController: UIViewController {
#IBOutlet weak var cityNameTextField: UITextField!
#IBOutlet weak var cityNameLabel: UILabel!
#IBOutlet weak var cityTempLabel: UILabel!
#IBAction func getWeatherDataClick(sender: AnyObject) {
getWeatherData("http://api.openweathermap.org/data/2.5/weather?q=" + cityNameTextField.text! + "")
}
override func viewDidLoad() {
super.viewDidLoad()
getWeatherData("http://api.openweathermap.org/data/2.5/weather?q=London,uk&appid=2de143494c0b295cca9337e1e96b00e0")
// 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.
}
func getWeatherData(urlString: String) {
let url = NSURL(string: urlString)
let task = NSURLSession.sharedSession().dataTaskWithURL(url!) { (data, response, error) in
dispatch_async(dispatch_get_main_queue(), {
self.setLabels(data!)
})
}
task.resume()
}
func setLabels(weatherData: NSData) {
let jsonResult = AnyObject? ()
do {
if let jsonResult = try NSJSONSerialization.JSONObjectWithData(weatherData, options: []) as? NSDictionary {
print(jsonResult)
}
} catch {
print(error)
}
if let name = jsonResult!["name"] as? String {
cityNameLabel.text = name
}
if let main = jsonResult!["main"] as? NSDictionary {
if let temp = main["temp"] as? Double {
cityTempLabel.text = String(format: "%.1f", temp)
}
}
};
}
First guess would be: data == nil. Your function: setLabels: is not prepared to receive nil argument. Try to change declaration of this function to:
func setLabels(weatherData: NSData?)
Or even better handle data == nil possibility before calling setLabels, in your NSURLSession block:
if let weatherData = data as? NSData {
//your data is not nil
//you can securely call setLabels
self.setLabels(weatherData)
} else {
//ooops sth goes wrong your data is nil, try to figure out why
}

Swift and SwiftyJSON - dump() not work

try to use dump method on variable filled with JSON. But it doesnt work and i dont understand why
import UIKit
class ViewController: UIViewController {
var hoge: JSON?
override func viewDidLoad() {
super.viewDidLoad()
let url = NSURL(string: "https://api.whitehouse.gov/v1/petitions.json")
var request = NSURLRequest(URL: url!)
var data = NSURLConnection.sendSynchronousRequest(request, returningResponse: nil, error: nil)
if data != nil {
hoge = JSON(data: data!)
let count = hoge!["results"][0]["body"].stringValue
println(count)
}
}
func res() {
dump(self.hoge)
}
}
can you please help me show what am i doing wrong ?