I have these JSON data:
{"login":"ET001","email":"email#try.com"}
In Swift 3.0, I created two files which are LoginVC and ViewController.
ViewController can only be accessed after LoginVC verified the credentials. So far I managed to make the login access the ViewController page based on "success" JSON data from database.
But my next goal is to pass the JSON data "[login]" from LoginVC into ViewController.
In ViewController, I created UILabel "loginLbl" to display the JSON value from LoginVC.
How do update my code?
LoginVC.swift
import UIKit
class LoginVC: UIViewController {
#IBOutlet var _login: UITextField!
#IBOutlet var _pass: UITextField!
#IBOutlet var outputLbl: UILabel!
var login: String!
var pass: String!
override func viewDidLoad() {super.viewDidLoad()}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "tocey"{
if let destination = segue.destination as? ViewController {
destination.passedData = self.outputLbl.text
print("Sender value is : \(sender)")
}
}
}
#IBAction func loginData(_ sender: Any) {
login = _login.text
pass = _pass.text
if(login == "" || pass == "") {
return
}
else {
let url = URL(string: "http://localhost/login.php")
let session = URLSession.shared
let request = NSMutableURLRequest(url: url! as URL)
request.httpMethod = "POST"
let paramToLogin = "login=\(login!)&pass=\(pass!)"
request.httpBody = paramToLogin.data(using: String.Encoding.utf8)
let task = session.dataTask(with: request as URLRequest, completionHandler: {
(data, response, error) in
if error != nil {
return
}
else {
do {
if let json = try JSONSerialization.jsonObject(with: data!) as? [String: String] {
DispatchQueue.main.async {
let success = Int(json["success"]!)
let loginvaluefromdb = json["login"]
if(success == 1){
self.outputLbl.text = loginvaluefromdb;
let abc = json["login"]
self.performSegue(withIdentifier: "tocey", sender: abc)
return
}
}
}
}
catch {
}
}
})
task.resume()
}
}
}
ViewController.swift
import UIKit
class ViewController: UIViewController {
#IBOutlet var loginLbl: UILabel!
var passedData: String!
override func viewDidLoad() {
super.viewDidLoad()
loginLbl.text = passedData
}
}
How to pass it into UILabel loginLbl?
Once you identify that login data is correct in your response you need to push your viewController in navigation controller and take one dictionary in your viewController and assign json to that dictionary.
if let json = try JSONSerialization.jsonObject(with: data!) as? [String: String] {
DispatchQueue.main.async {
let success = Int(json["success"]!)
let loginvaluefromdb = json["login"]
if(success == 1){
let viewController = self.storyboard?.instantiateViewController(withIdentifier: "yourViwController") as! yourViwController
viewController.dictJson = json
self.navigationController?.pushViewController(viewController, animated: true)
}
if(success == 1){
self.outputLbl.text = loginvaluefromdb;
// Here you trigger the segue
self.performSegue(withIdentifier: "goToViewController", sender: loginvaluefromdb)
return
}
You need to pass the data in prepare for segue method :
Here is the editec code
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "goToViewController" {
if let destination = segue.destination as? ViewController {
// Here you will copy tha data you want to pass
destination.passedData = sender as? String
print("Sender Value: \(sender)")
}
}
}
Related
im redoing my ode after getting mixed up with some concepts of swift. Have in mind im new to swift.
In my project there are currently 2 ViewControllers, in the first one there is a UITextField and a UIButton. in the second one there is a UIWebView.
I know that the UIWebView only allows a URL type address, so i want the text introduced in the UITextField to be a URL, So how do i change the string introduced into a URL and display that value (URL introduced) in the UIWebView? Should i store that value in a global variable? I really tried all...
You can send it to the nextVC via Segue , or store it in defaults and read it there
let myUrl = URL(string:textfield.text)
self.performSegue(withIdentifier: "goToNext", sender:myUrl)
//
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if let let next = segue.destination as? nextVC {
next.currentUrl = sender as! URL
}
}
//
class nextVC : UIViewController
{
var currentUrl:URL?
}
It is very important to check if the user has entered a valid url. you can check that in Second viewController while loading the request from url.
Here is the overall code :
First viewController :
var url: String!
url = URL(string:textfield.text) // Convert text to url
self.performSegue(withIdentifier: "yourIdentifier", sender:url) // Go from one VC to other.
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if let let vc = segue.destination as? nextVC {
vc.url = self.url
}
}
Second viewController :
var url: String! // Global var
if url.isUrl
{
webView.loadRequest(URLRequest(url: url!))
}
else
{
print("invalid url")
}
Validate URL string extension :
extension String {
var isUrl: Bool {
// for http://regexr.com checking
// (?:(?:https?|ftp):\/\/)(?:xn--)?(?:\S+(?::\S*)?#)?(?:(?!10(?:\.\d{1,3}){3})(?!127(?:\.\d{1,3}){3})(?!169\.254(?:\.\d{1,3}){2})(?!192\.168(?:\.\d{1,3}){2})(?!172\.(?:1[6-9]|2\d|3[0-1])(?:\.\d{1,3}){2})(?:[1-9]\d?|1\d\d|2[01]\d|22[0-3])(?:\.(?:1?\d{1,2}|2[0-4]\d|25[0-5])){2}(?:\.(?:[1-9]\d?|1\d\d|2[0-4]\d|25[0-4]))|(?:(?:[a-z\u00a1-\uffff0-9]+-?)*[a-z\u00a1-\uffff0-9]+)(?:\.(?:[a-z\u00a1-\uffff0-9]+-?)*[a-z\u00a1-\uffff0-9]+)*(?:\.(?:[#-z\u00a1-\uffff]{2,})))(?::\d{2,5})?(?:\/[^\s]*)?
let schemes = URLSchemes.getAllSchemes(separetedBy: "|").replacingOccurrences(of: "://", with: "")
let regex = "(?:(?:\(schemes)):\\/\\/)(?:xn--)?(?:\\S+(?::\\S*)?#)?(?:(?!10(?:\\.\\d{1,3}){3})(?!127(?:\\.\\d{1,3}){3})(?!169\\.254(?:\\.\\d{1,3}){2})(?!192\\.168(?:\\.\\d{1,3}){2})(?!172\\.(?:1[6-9]|2\\d|3[0-1])(?:\\.\\d{1,3}){2})(?:[1-9]\\d?|1\\d\\d|2[01]\\d|22[0-3])(?:\\.(?:1?\\d{1,2}|2[0-4]\\d|25[0-5])){2}(?:\\.(?:[1-9]\\d?|1\\d\\d|2[0-4]\\d|25[0-4]))|(?:(?:[a-z\\u00a1-\\uffff0-9]+-?)*[a-z\\u00a1-\\uffff0-9]+)(?:\\.(?:[a-z\\u00a1-\\uffff0-9]+-?)*[a-z\\u00a1-\\uffff0-9]+)*(?:\\.(?:[#-z\\u00a1-\\uffff]{2,})))(?::\\d{2,5})?(?:\\/[^\\s]*)?"
let regularExpression = try! NSRegularExpression(pattern: regex, options: [])
let range = NSRange(location: 0, length: self.characters.count)
let matches = regularExpression.matches(in: self, options: [], range: range)
for match in matches {
if range.location == match.range.location && range.length == match.range.length {
return true
}
}
return false
}
var toURL: URL? {
let urlChecker: (String)->(URL?) = { url_string in
if url_string.isUrl, let url = URL(string: url_string) {
return url
}
return nil
}
if !contains(".") {
return nil
}
if let url = urlChecker(self) {
return url
}
let scheme = URLSchemes.detectScheme(urlString: self)
if scheme == .unknown {
let newEncodedString = URLSchemes.http.rawValue + self
if let url = urlChecker(newEncodedString) {
return url
}
}
return nil
}
}
Here is an example of how to do this.
Here is the gist:
ViewController:
class ViewController: UIViewController, UIWebViewDelegate {
#IBOutlet weak var urlTextView: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if let webViewController = segue.destination as? WebViewController {
if let url = sender as? URL {
webViewController.urlToLoad = url
}
}
}
#IBAction func gotoURLButtonAction(_ sender: Any) {
let url = self.urlTextView.text ?? ""
if url.count > 0 {
if isProperHTTPUrl(str: url) {
self.performSegue(withIdentifier: "ViewControllerToWebViewController", sender: URL(string: url))
} else {
showError("not proper url format")
}
} else {
showError("must set url")
}
}
func isProperHTTPUrl(str:String) -> Bool {
let re = try! NSRegularExpression(pattern: "(?i)https?:\\/.*", options: [])
return re.numberOfMatches(in: str, options: .anchored, range: NSRange(location: 0, length: str.count)) > 0
}
}
WebViewController:
class WebViewController: UIViewController {
var urlToLoad:URL?
#IBOutlet weak var webView: UIWebView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
if let url = self.urlToLoad {
self.webView.loadRequest(URLRequest(url: url))
}
}
public func webView(_ webView: UIWebView, didFailLoadWithError error: Error) {
showError("Unable to load, \(error)")
}
}
I am currently working on facebook login app.And i am trying to display user cover photo and user profile pic.
But here i was not able to pic the cover, profile photo and not able to populate in my image views.
here my code :
func getFacebookGraphAndSegue(token:String) {
guard let req = FBSDKGraphRequest(graphPath: "me", parameters: ["fields":"email,name, first_name, last_name,id,cover,picture.width(100).height(100)"], tokenString: token, version: nil, httpMethod: "GET") else {
print("could not get Facebook user info")
return
}
req.start(completionHandler: { (connection, result, error) in
if(error == nil) {
print("result \(result)")
guard let userInfo = result as? [String: AnyObject] else {print("bad result");return}
let name = userInfo["name"] as? String ?? ""
// let cover = (userInfo["cover"] as? UIImage!)["source"]{
// var coverUrl = cover as? String
//
// }
self.performSegue(withIdentifier: "loginSegue", sender: userInfo)
}
else {
print("error \(error)")
}
})
}
//--------------------------------------------------------------------------------------------
// MARK: - Segue
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "loginSegue" {
if let vc = segue.destination as? UserViewController {
if let info = sender as? [String: AnyObject] {
let email = info["email"] as? String ?? ""
let name = info["name"] as? String ?? ""
let firstName = info["first_name"] as? String ?? ""
let lastName = info["last_name"] as? String ?? ""
let lbl:String = "\nNAME:\(name)"
print(lbl)
vc.info = lbl
}
}
}
}
the commented line in above function is i tried to get the cover photo as well as profile pic and below i need to display.
In my next screen only i will display user name, profile pic,cover photo :
#IBOutlet weak var labelUserInfo: UILabel!
#IBOutlet weak var coverpageImage: UIImageView!
#IBOutlet weak var profilePicImage: UIImageView!
var info:String = ""
override func viewDidLoad() {
super.viewDidLoad()
self.labelUserInfo.text = info
}
#IBAction func onLogout(_ sender: UIButton) {
//Facebook Logout
print("on Facebook Logout Tapped")
let loginManager = LoginManager()
loginManager.logOut()
self.performSegue(withIdentifier: "logouSegue", sender: nil)
}
please help me out. How can i get the cover , profile pic to display in my screen / image view.
thanks
I'm new to iOS and was hoping someone would be willing to help me out with an issue I'm having
Say I have 2 Views in my storyboard:
- View1: Has 1 text box
- View2: Has 1 Label
Each being respectively controlled by a ViewControllers:
- FirstViewController
- SecondViewController
My app would send the text of the textbox in View1 as an HTTP (POST) request to an API, and would display on View2 the result which is sent back in JSON format.
My approach is to use the prepare(for segue:,Sender:), however I am having a hard time returning the JSON response from Task() in order to send it to SecondViewController via a Segue.
class ResultViewController: UIViewController {
#IBOutlet var text_input: UITextField!
Let api_url = (the api url)
func makeRequest(voucher_number:String, redemption_code:String){
let json: [String: Any] = [
"input" : text_input.text
]
let request_json = try? JSONSerialization.data(withJSONObject: json)
let url:URL = URL(string: api_url)!
let session = URLSession.shared
var request = URLRequest(url: url)
request.httpMethod = "POST"
request.cachePolicy = NSURLRequest.CachePolicy.reloadIgnoringCacheData
request.httpBody = request_json
let task = session.dataTask(with: request as URLRequest, completionHandler: {
(
data, response, error) in
guard let _:Data = data, let _:URLResponse = response , error == nil else {
return
}
let json: Any?
do
{
json = try JSONSerialization.jsonObject(with: data!, options: [])
}
catch
{
}
guard let server_response = json as? [String: Any] else
{
return
}
//This is where I think the return should take place
//but can't figure out how
})
task.resume()
}
}
I know I would need to modify my func declaration by adding the return syntax, but I can't figure out how to return data in the first place :P so I skipped this part for the time being.
I would then do the following to send the response to SecondViewController
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "firstSegue" {
if let resultViewController = segue.destination as? SecondViewController {
if (text_input.text != nil && redemption_code.text != nil) {
if let json_response = makeRequest() {
SecondViewController.request_result = json_response
}
// request_result is the variable in
// SecondViewController that will store the data
// being passed via the segue.
}
}
}
}
I know my code may not be the best practice for what I'm trying to achieve. And I'm open to suggestions to tackle a different approach, as long as it's not too advanced for me.
Cheers
Notifications are a good way to forward JSON data out of completion handler blocks, like:
NotificationCenter.default.post(name: Notification.Name(rawValue:"JSON_RESPONSE_RECEIVED"), object: nil, userInfo: server_response)
Register and handle the notification in FirstViewController:
NotificationCenter.default.addObserver(self, selector: #selector(FirstViewController.json_Response_Received(_:)), name:NSNotification.Name(rawValue: "JSON_RESPONSE_RECEIVED"), object: nil)
(in viewDidLoad()) and:
func json_Response_Received(_ notification:Notification) {
responseDictionary = (notification as NSNotification).userInfo as! [String:AnyObject];
self.performSegue(withIdentifier: "SegueToSecondController", sender: self)
}
Then you can pass responseDictionary to SecondViewController in:
override func prepare(for segue:UIStoryboardSegue, sender:Any?) {
if (segue.identifier == "SegueToSecondController") {
secondViewController = segue.destinationViewController as! SecondViewController
secondViewController.response = responseDictionary
}
}
I have a log in page that collects a username and password. On submit, Its sends to the database to retrieve our servers access key. I do this through an asynchronous JSON POST using session.dataTask. When I retrieve the JSON Object I parse the key out of it. I want to pass it to the next page, retrieve a firebase token and then send both pieces of data back to the server for DB storage. I have created a "prepare for segue" function that collects the variable and passes it to a variable on the next page. I believe I am not setting up the sequence of events correctly or that the data isn't making it out of the Async container. Can someone have a look at these two files and see where I am getting it wrong?
Here is the first page I want to segue away from after making the REST web service call...
loginVC.swift:
import UIKit
class LoginVC: UIViewController {
#IBOutlet weak var username: UITextField!
#IBOutlet weak var password: UITextField!
#IBOutlet weak var validationBox: UITextView!
#IBAction func logInAction(_ sender: UIButton) {
guard let user = username.text, !user.isEmpty else {
validationBox.text = "Please enter valid credentials"
return
}
guard let pass = password.text, !pass.isEmpty else {
validationBox.text = "Please enter valid credentials"
return
}
let params = ["sUser": username.text!, "sPass": password.text!]
let url = URL(string: "restWebServiceURL")!
let session = URLSession.shared
var request = URLRequest(url: url)
request.httpMethod = "POST"
do {
request.httpBody = try JSONSerialization.data(withJSONObject: params, options: .prettyPrinted)
} catch let error {
print(error.localizedDescription)
}
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 in
guard error == nil else { return }
guard let data = data else { return }
do {
if let parsedJSON = try JSONSerialization.jsonObject(with: data, options: .mutableContainers) as? [String: Any] {
let parsedData = parsedJSON["d"] as! [String:Any]
let key = parsedData["key"] as! String
DispatchQueue.main.async {
print(key)
self.performSegue(withIdentifier: "FirebaseVC", sender: key)
}
}
} catch let error {
print(error.localizedDescription)
}
})
task.resume()
}
func sayHello() {
print("Hello!")
}
func sayGoodbye() {
print("Goodbye!")
}
override func viewDidLoad() {
super.viewDidLoad()
validationBox.text = "Ready..."
func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if let FirebaseInit = segue.destination as? FirebaseVC {
if let sKey = sender as? String {
print("prepare - " + sKey)
FirebaseInit.sessionKey = sKey
}
}
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
Here is the page I want to go to to receive the data access key ...
FirebaseVC.swift:
import UIKit
class FirebaseVC: UIViewController {
private var _sessionKey = String()
var sessionKey : String {
get { return _sessionKey }
set { _sessionKey = newValue }
}
#IBOutlet weak var sessionKeyTestBox: UITextView!
override func viewDidLoad() {
super.viewDidLoad()
print(_sessionKey)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
Feel free to suggest a better way to pass the data to the next page. Thanks...
It turns out I was correct in my assumption the the chain of events was off. Following the model suggested by #achrefGassoumi, I moved the datatask to a Singleton Service here:
import Foundation
struct CallWebService {
static let sharedInstance = CallWebService()
func logInToCaduceus(u: String, p: String, completion: #escaping (_ sKey: String) -> ()) {
let params = ["sUser": u, "sPass": p]
let url = URL(string: "https://telemed.caduceususa.com/ws/telemed.asmx/telemedLogin")!
let session = URLSession.shared
var request = URLRequest(url: url)
request.httpMethod = "POST"
do {
request.httpBody = try JSONSerialization.data(withJSONObject: params, options: .prettyPrinted)
} catch let error {
print(error.localizedDescription)
}
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 in
guard error == nil else { return }
guard let data = data else { return }
do {
if let parsedJSON = try JSONSerialization.jsonObject(with: data, options: .mutableContainers) as? [String: Any] {
let parsedData = parsedJSON["d"] as! [String:Any]
let key = parsedData["key"] as! String
DispatchQueue.main.async {
completion(key)
}
}
} catch let error {
print(error.localizedDescription)
}
})
task.resume()
}
}
Then my two controllers look like this:
LoginVC
import UIKit
class LoginVC: UIViewController {
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if (segue.destination.isKind(of: FirebaseVC.self)) {
let vc = segue.destination as! FirebaseVC
if let sKey = sender as? String {
vc.sessionKey = sKey
}
}
}
#IBOutlet weak var username: UITextField!
#IBOutlet weak var password: UITextField!
#IBOutlet weak var validationBox: UITextView!
#IBAction func logInAction(_ sender: UIButton) {
guard let user = username.text, !user.isEmpty else {
validationBox.text = "Please enter valid credentials"
return
}
guard let pass = password.text, !pass.isEmpty else {
validationBox.text = "Please enter valid credentials"
return
}
CallWebService.sharedInstance.logInToCaduceus(u: username.text!, p: password.text!, completion: {(sessionKey: String) -> Void in
print(sessionKey)
self.performSegue(withIdentifier: "FirebaseVC", sender: sessionKey)
}
)
}
override func viewDidLoad() {
super.viewDidLoad()
//validationBox.textAlignment = .center
validationBox.text = "Ready..."
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
AND THE receiving FirebaseVC
import UIKit
class FirebaseVC: UIViewController {
private var _sessionKey = String()
var sessionKey : String {
get { return _sessionKey }
set { _sessionKey = newValue }
}
#IBOutlet weak var sessionKeyTestBox: UITextView!
override func viewDidLoad() {
super.viewDidLoad()
sessionKeyTestBox.text = _sessionKey
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}
Excuse my (non-swift) Javascript terminology but essentially I moved the data call into a service and then place a callback method in the service with the completion method to ensure the the performSegue doesn't fire until the data has been received and parsed out. So when i submit the log in form data to the server the segue doesn't fire until that async call has been completed.
How to pass data between controller using UIStoryboard in swift 3.0 ?
I created login (LoginViewController) and main (ViewController) page where apps keep active if user did not logout.
So far it work.
And I have these JSON value "login" that need to be passed from LoginViewController into ViewController
Some of people suggest to use perform and prepare segue.
It work but the value can only be display if it sent from LoginViewController.
ViewController did not hold the data when the page ViewController is active (when i rebuild/clean the apps).
My goal is to keep the "login" data in ViewController when page is active and clear "login" data when the page logout.
LoginViewController.swift
import UIKit
class LoginViewController: UIViewController {
#IBOutlet var _loginLbl: UITextField!
#IBOutlet var _pwLbl: UITextField!
#IBOutlet var login_button: UIButton!
#IBOutlet var outputLbl: UILabel!
var login: String!
var pw: String!
override func viewDidLoad() {super.viewDidLoad()}
#IBAction func loginData(_ sender: Any) {
login = _loginLbl.text
pw = _pwLbl.text
let url = URL(string: "http://localhost/login.php")
let session = URLSession.shared
let request = NSMutableURLRequest(url: url! as URL)
request.httpMethod = "POST"
let LoginDataToPost = "login=\(login!)&pw=\(pw!)"
request.httpBody = LoginDataToPost.data(using: String.Encoding.utf8)
let task = session.dataTask(with: request as URLRequest, completionHandler: {
(data, response, error) in
if error != nil {
return
}
else {
do {
if let json = try JSONSerialization.jsonObject(with: data!) as? [String: String]
{
DispatchQueue.main.async
{
let message = Int(json["message"]!)
let login = json["login"]
if(message == 1) {
UserDefaults.standard.set(true, forKey: "isUserLoggedIn")
UserDefaults.standard.synchronize();
self.dismiss(animated: true, completion: nil)
let myViewController:ViewController = self.storyboard!.instantiateViewController(withIdentifier: "ViewController") as! ViewController
let appDelegate = UIApplication.shared.delegate as! AppDelegate
appDelegate.window?.rootViewController = myViewController
appDelegate.window?.makeKeyAndVisible()
self.outputLbl.text = login;
return
}
else { }
}
}
else { }
} catch let jsonParse {}
}
})
task.resume()
}
}
}
ViewController.swift
import UIKit
class ViewController: UIViewController {
#IBOutlet var loginLbl: UILabel!
var login: String!
override func viewDidLoad() {
super.viewDidLoad()
let preferences = UserDefaults.standard
if(preferences.object(forKey: "isUserLoggedIn") != nil){
loginLbl.text = login
}
else {
}
}
override func viewDidAppear(_ animated: Bool) {
let isUserLoggedIn = UserDefaults.standard.bool(forKey: "isUserLoggedIn")
if(!isUserLoggedIn){
self.performSegue(withIdentifier: "loginview", sender: self)
}
}
#IBAction func logoutData(_ sender: Any) {
UserDefaults.standard.set(false, forKey: "isUserLoggedIn");
UserDefaults.standard.synchronize();
let loginViewController = self.storyboard!.instantiateViewController(withIdentifier: "loginview") as! LoginViewController
let appDelegate = UIApplication.shared.delegate as! AppDelegate
appDelegate.window?.rootViewController = loginViewController
appDelegate.window?.makeKeyAndVisible()
}
}
Any idea ?
Thanks.
You can store the value in NSUserDefaults in LoginViewController. Like below :
UserDefaults.standard.set(login, forKey: "loginJSONValue")
And get the value in ViewController like this (I assume that the value is String):
textField1.text = UserDefaults.standard.object(forKey: "loginJSONValue") as? String
LoginViewController.swift
import UIKit
class LoginViewController: UIViewController {
#IBOutlet var _loginLbl: UITextField!
#IBOutlet var _pwLbl: UITextField!
#IBOutlet var login_button: UIButton!
#IBOutlet var outputLbl: UILabel!
var login: String!
var pw: String!
override func viewDidLoad() {super.viewDidLoad()}
#IBAction func loginData(_ sender: Any) {
login = _loginLbl.text
pw = _pwLbl.text
let url = URL(string: "http://localhost/login.php")
let session = URLSession.shared
let request = NSMutableURLRequest(url: url! as URL)
request.httpMethod = "POST"
let LoginDataToPost = "login=\(login!)&pw=\(pw!)"
request.httpBody = LoginDataToPost.data(using: String.Encoding.utf8)
let task = session.dataTask(with: request as URLRequest, completionHandler: {
(data, response, error) in
if error != nil {
return
}
else {
do {
if let json = try JSONSerialization.jsonObject(with: data!) as? [String: String]
{
DispatchQueue.main.async
{
let message = Int(json["message"]!)
let login = json["login"]
if(message == 1) {
UserDefaults.standard.set(true, forKey: "isUserLoggedIn")
UserDefaults.standard.set(login, forKey: "loginJSONValue")
UserDefaults.standard.synchronize();
self.dismiss(animated: true, completion: nil)
let myViewController:ViewController = self.storyboard!.instantiateViewController(withIdentifier: "ViewController") as! ViewController
let appDelegate = UIApplication.shared.delegate as! AppDelegate
appDelegate.window?.rootViewController = myViewController
appDelegate.window?.makeKeyAndVisible()
self.outputLbl.text = login;
return
}
else { }
}
}
else { }
} catch let jsonParse {}
}
})
task.resume()
}
}
}
ViewController.swift
import UIKit
class ViewController: UIViewController {
#IBOutlet var loginLbl: UILabel!
var login: String!
override func viewDidLoad() {
super.viewDidLoad()
let preferences = UserDefaults.standard
if(preferences.object(forKey: "isUserLoggedIn") != nil){
// let isUserLoggedIn = UserDefaults.standard.bool(forKey: "isUserLoggedIn")
let isUserLoggedIn = UserDefaults.standard.object(forKey: "isUserLoggedIn") as Bool
// your login data is below. do what you want to do
login = UserDefaults.standard.object(forKey: "loginJSONValue") as String
if(!isUserLoggedIn){
self.performSegue(withIdentifier: "loginview", sender: self)
}
loginLbl.text = login
}
else {
}
}
override func viewDidAppear(_ animated: Bool) {
}
#IBAction func logoutData(_ sender: Any) {
UserDefaults.standard.set(false, forKey: "isUserLoggedIn");
// After logging out, you can set empty to login data.
UserDefaults.standard.set("", forKey: "loginJSONValue");
UserDefaults.standard.synchronize();
let loginViewController = self.storyboard!.instantiateViewController(withIdentifier: "loginview") as! LoginViewController
let appDelegate = UIApplication.shared.delegate as! AppDelegate
appDelegate.window?.rootViewController = loginViewController
appDelegate.window?.makeKeyAndVisible()
}
}
You need to call Login service again and pass the data when application is restarted. or you can stored data in UserDefault and reuse it.
UserDefaults.standard.set(login, forKey: "loginResponse")
You can pass data from one controller to another controller as below:
let myViewController:ViewController = self.storyboard!.instantiateViewController(withIdentifier: "ViewController") as! ViewController
var loginResponse = UserDefaults.standard.get(forKey:"loginResponse")
myViewController.your_object = loginResponse