0

I have been trying to integrate a Pinboard bookmarks view (by parsing an RSS Feed and displaying it in a TableView) in my browser app. To get the username and API Token for the feed I have a UIAlertController in the Settings view of my app. The details entered are preserved through the session but if I force quit the app from the multitasking view, The details are deleted. How can I make them stay?

This is the code I'm using for the UIAlertController:

@IBAction func pinboardUserDetailsRequestAlert(sender: AnyObject) {
    //Create the AlertController
    var pinboardUsernameField :UITextField?
    var pinboardAPITokenField :UITextField?
    let pinboardUserDetailsSheetController: UIAlertController = UIAlertController(title: "Pinboard Details", message: "Please enter your Pinboard Username and API Token to access your bookmarks", preferredStyle: .Alert)
    //Add a text field
    pinboardUserDetailsSheetController.addTextFieldWithConfigurationHandler({(usernameField: UITextField!) in
        usernameField.placeholder = "Username"
        var parent = self.presentingViewController as! ViewController
        usernameField.text = parent.pinboardUsername
        pinboardUsernameField = usernameField
    })
    pinboardUserDetailsSheetController.addTextFieldWithConfigurationHandler({(apiTokenField: UITextField!) in
        apiTokenField.placeholder = "API Token"
        var parent = self.presentingViewController as! ViewController
        apiTokenField.text = parent.pinboardAPIToken
        pinboardAPITokenField = apiTokenField
    })
    pinboardUserDetailsSheetController.addAction(UIAlertAction(title: "Cancel", style: .Cancel, handler: nil))
    pinboardUserDetailsSheetController.addAction(UIAlertAction(title: "Done", style: .Default, handler: { (action) -> Void in
        // Now do whatever you want with inputTextField (remember to unwrap the optional)
        var parent = self.presentingViewController as! ViewController
        parent.pinboardAPIToken = pinboardAPITokenField?.text
        parent.pinboardUsername = pinboardUsernameField?.text
    }))
    //Present the AlertController
    self.presentViewController(pinboardUserDetailsSheetController, animated: true, completion: nil)

}
rmaddy
  • 314,917
  • 42
  • 532
  • 579
PastaCoder
  • 1,031
  • 1
  • 9
  • 16
  • If the data you want to persist is simple use [NSUserDefaults](https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSUserDefaults_Class/index.html), if it's more complex use [CoreData](https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/CoreData/cdProgrammingGuide.html) – Automate This Apr 25 '15 at 17:12
  • I think it's simple, It's just 2 String variables (1 for Username and 1 for API Token). How do I go about using NSUserDefaults for this? – PastaCoder Apr 25 '15 at 18:06
  • I'd agree but I'm not sure about security and if you should store such things using NSUserDefaults. Just to get started though, NSUserDefaults is really simple with just a couple lines of code. – Automate This Apr 25 '15 at 18:08
  • It's working now with a few lines of NSUserDefaults :) I have some issues with NSXMLParser though, but I'll post it as a separate question. Thanks Portland :) – PastaCoder Apr 25 '15 at 20:47

1 Answers1

0

This question has been answered by Portland Runner in the comments to the question. The solution that worked was to save the text using NSUserDefaults.

Thanks Portland Runner! :)

PastaCoder
  • 1,031
  • 1
  • 9
  • 16