Create Navigation Bar Programmatically in Swift

In your UIPopoverPresentationControllerDelegate:

func presentationController(controller: UIPresentationController, viewControllerForAdaptivePresentationStyle style: UIModalPresentationStyle) -> UIViewController? {
    let presentedViewController = controller.presentedViewController
    let navigationController = UINavigationController(rootViewController: presentedViewController)
    let dismissButton = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.Done, target: self, action: "dismissPopover:")
        
    presentedViewController.navigationItem.rightBarButtonItem = dismissButton
        
    return navigationController
}
    
func dismissPopover(sender: AnyObject) {
    self.dismissViewControllerAnimated(true, completion: nil)
}

Setting Up Delegates in iOS

This is copied from the iOS Apprentice tutorial on raywenderlich.com but I wanted to put it where I could easily access it. These are the basic steps for setting up a delegate pattern between two objects, where object A is the delegate for object B, and object B will send messages back to A.

  1. Define a delegate protocol for object B
  2. Give object B an optional delegate variable. This variable should be weak
  3. Make object B send messages to its delegate when something interesting happens, such as the user pressing the Cancel or Done buttons, or when it needs a piece of information. You write delegate?.methodName(self, ...)
  4. Make object A conform to the delegate protocol. It should put the name of the protocol in its class line and implement the methods from the protocol.
  5. Tell object B the object A is now its delegate

 

Override Application Transport Security in xCode

When you get the error App Transport Security has blocked a cleartext HTTP (http://) resource load since it is insecure. Temporary exceptions can be configured via your app’s Info.plist file., you can override the security and allow all http urls through by adding this to your info.plist file:

<key>NSAppTransportSecurity</key>  
     <dict>  
          <key>NSAllowsArbitraryLoads</key><true>  
     </true></dict>

However, before doing this, check to see if there is an https version of the url. That will work just as well, and https won’t throw that error.

Get user input in Swift

Here is a helper function that handles user input. You can just plop all this in a file called HelperFunctions.swift.

/*

This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike
4.0 International License, by Yong Bakos.

*/

import Foundation

// Wait for the user to type something in the console, and return what
// they type as a String with the trailing newline character removed.
func getln() -> String {
    let stdin = NSFileHandle.fileHandleWithStandardInput()
    var input = NSString(data: stdin.availableData, encoding: NSUTF8StringEncoding)
    input = input!.stringByTrimmingCharactersInSet(NSCharacterSet.newlineCharacterSet())
    return input as! String
}