Peter C. Peter C. - 7 months ago 24
Swift Question

Sending an SMS with Swift, remember phone number

I'm working on an app where you can send an sms to someone within the app. This is what I've found so far. I would like to remember the phone number once it has been entered for the future, though the user has the ability to change it whenever they want to. Any ideas?

import UIKit
import MessageUI

class ViewController: UIViewController, MFMessageComposeViewControllerDelegate {

@IBOutlet weak var phoneNumber: UITextField!

override func viewDidLoad() {
super.viewDidLoad()
}

@IBAction func sendText(sender: UIButton) {
if (MFMessageComposeViewController.canSendText()) {
let controller = MFMessageComposeViewController()
controller.body = "Message Body"
controller.recipients = [phoneNumber.text]
controller.messageComposeDelegate = self
self.presentViewController(controller, animated: true, completion: nil)
}
}

func messageComposeViewController(controller: MFMessageComposeViewController!, didFinishWithResult result: MessageComposeResult) {
//... handle sms screen actions
self.dismissViewControllerAnimated(true, completion: nil)
}

override func viewWillDisappear(animated: Bool) {
self.navigationController?.navigationBarHidden = false
}
}

Answer

You can save phone number in nsuser default. and when you need that number you can retrive from Nsuser default

NSUserDefaults.standardUserDefaults().setObject(phoneNumber.text, forKey: "phone_no")

by above line you can store mobile number in nsuserDefault

then you can retrive it by this code

NSUserDefaults.standardUserDefaults().valueForKey("phone_no")