I have just started programming in swift so please take that in consideration. I am working on a app that send sms messages depending on a button clicked. I have everything figured out and the app works fine but I have a problem that the app prompts me to enter the recipient number every time I open the app. I figured out that the phone number is not saving but I can't find a way to save the usrTextField var to UserDefaults. Here is the code of the app:
import UIKit
import MessageUI
class ViewController: UIViewController, MFMessageComposeViewControllerDelegate{
var usrTextField: UITextField?
var pwdTextField: 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 displayAlertAction(_ sender: Any) {
let alertController = UIAlertController(title: "Enter phone number and password", message: nil, preferredStyle: .alert)
alertController.addTextField(configurationHandler: usrTextField)
alertController.addTextField(configurationHandler: pwdTextField)
let okAction = UIAlertAction(title: "OK", style: .default, handler: self.okHandler)
let cancelAction = UIAlertAction(title: "Cancel", style: .cancel, handler: nil)
alertController.addAction(okAction)
alertController.addAction(cancelAction)
self.present(alertController, animated: true)
}
func usrTextField(textField: UITextField!) {
usrTextField = textField
usrTextField?.placeholder = "06XXXXXXX"
}
func pwdTextField(textField: UITextField!) {
pwdTextField = textField
pwdTextField?.placeholder = "123456"
pwdTextField?.isSecureTextEntry = true
}
func okHandler(alert: UIAlertAction!) {
let simpleVC = SimpleVC()
simpleVC.customInit(usrStr: (usrTextField?.text)!, pwdStr: (pwdTextField?.text)!)
self.navigationController?.pushViewController(simpleVC, animated: true)
}
func messageComposeViewController(_ controller: MFMessageComposeViewController, didFinishWith result: MessageComposeResult) {
self.dismiss(animated: true, completion: nil)
}
@IBAction func Dugme1(_ sender: AnyObject)
{
if MFMessageComposeViewController.canSendText()
{
let controller = MFMessageComposeViewController ()
controller.body = "Message1"
if let temp = usrTextField {
controller.recipients = [self.usrTextField!.text!]
} else {
let alert = UIAlertController(title: "AlfaPro Alarm", message: "Please enter the phone number in settings", preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "OK", style: .cancel, handler: nil))
self.present(alert, animated: true)
}
controller.messageComposeDelegate = self
self.present(controller, animated: true, completion: nil)
let impact = UIImpactFeedbackGenerator() // 1
impact.impactOccurred()
}
else
{
print("error in sending")
}
}
Code of the other Storyboard:
import UIKit
class SimpleVC: UIViewController {
@IBOutlet weak var usrLabel: UILabel!
@IBOutlet weak var pwdLabel: UILabel!
var usrStr: String?
var pwdStr: String?
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
usrLabel.text = usrStr
pwdLabel.text = pwdStr
}
func customInit(usrStr: String, pwdStr: String) {
self.usrStr = usrStr
self.pwdStr = pwdStr
}
/*
// MARK: - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
// Get the new view controller using segue.destination.
// Pass the selected object to the new view controller.
}
*/
}