Why Code by Hand When You Have Storyboards?

Brick path winding through the treesContinuing to acquaint myself with the latest developments in iOS programming, some words in Apple’s documentation made me reconsider coding interfaces by hand if it wasn’t strictly necessary. I brought up a test application created from a template using both the Swift language and storyboards, and poked around on it until I was satisfied that I had no idea what I was doing.

I found a tutorial video covering some basics of using storyboards, and immediately was enlightened. Tonight I made another test application and, armed with what I learned from the video and some other resources, somewhat more confidently plowed my way to graphically designing application page flow and connecting view objects to Swift code.

While my initial foray into hand-coding interfaces started as a particular business product requirement, and turned into a personal habit, I feel my iOS programming thought patterns shifting. Storyboards will clearly help me churn out iOS productivity software faster than I have in the past.

[I imagine that most iOS programmers would have never seriously considered not using storyboards in the first place...]

First Steps with Swift for iOS

A view through the treesBy my records, it has been nearly three years since I last touched the innards of an iOS application. I’m hacking on an iOS project again, and while programming with Objective-C appears to still be supported, Apple’s fancy new Swift programming language is clearly the future of iOS development (for now).

Beyond the most basic of applications, I never found Xcode’s graphical interface tools to be very intuitive, and I personally was more productive writing everything in straight code. So the first thing I want to do in Swift is see how to get a basic storyboard-free application going:

  • Create a new Single View application
  • On the main General tab in the Xcode project, under the Deployment Info section, backspace over the “Main” text for the main interface field to clear it out. That’s referring to a main storyboard, which we do not want to use.
  • Delete the files Main.storyboard and ViewController.swift, neither of which we need.
  • Delete the contents of the file AppDelegate.swift. We do actually need this file, but we don’t need what it is auto-populated with.

With AppDelegate.swift cleared, we can now replace it with what we actually want, the humble starting place for launching an application purely from code:

import UIKit
@UIApplicationMain
class AppDelegate : UIResponder, UIApplicationDelegate {
    var window : UIWindow?
    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool
    {
        self.window = UIWindow(frame:UIScreen.mainScreen().bounds)
        self.window!.rootViewController = UIViewController()
        let myView = self.window!.rootViewController!.view
    
        // Then we can add subviews to myView. But for now, just give it
        // a background color that we can look at.
    
        self.window!.backgroundColor = UIColor.blueColor()
        self.window!.makeKeyAndVisible()
        return true
    }
}

Build the project, and now we’re off to the races? Maybe, but unless we’re just going to be puttering around with graphical views, we should probably back way up first. What is our application going to do? What is the data model? What views will we need, and how will they relate to each other?

Now would be a good time for some drawings (on paper, even!) of how the user will navigate the interface, and some GUI-free Swift code that builds up the data model and algorithms for the application will give our graphical interface a solid foundation to rest on.

Deleting Useless Data from an iPhone

IMG_6020As I’ve been using my iPhone more for storing and playing back music to listen to in the 2014 Honda CR-V, I’ve been much more attentive to how much space data is taking up on the phone. My 16GB iPhone 4s has been precariously close to its limit, with the storage summary in iTunes showing that about five gigabytes were consumed by “Documents and Data” and “Other”.

Looking at neither iTunes nor the iPhone interface revealed any clues as to what these categories of data represented. I know that I store applications and audio on the phone, both of which were already accounted for with about three gigabytes each. I had already deleted a bunch of pictures to clear up space. But where was all of this other data and other “other” coming from?

Poking around on the web suggested trying out iMazing, a desktop application for exploring the raw iOS file system. Sometimes, supposedly, voice memos can get repeatedly duplicated, taking up a lot of space, or maybe some other temporary cache files can get out of hand. I didn’t see any obvious waste of stored data on the file system, but I tentatively tried deleting a few files that appeared to be unneeded cache, and synced my phone with iTunes to see if it made any difference at all.

Nearly all of both the “Documents and Data” and “Other” data disappeared, leaving five gigabytes of usable space in their stead.

I have absolutely no idea how the few small cache files that I deleted could have had this large of an impact. The phone seems to still be working as expected. I guess I’m pleased with the results, even though I do not understand how they came to be…

[Update: after several more days of using the iPhone, it became apparent that all was not quite well. When trying to play many songs from the music player, the songs would disappear from view. Sometimes entire albums disappeared. I fixed this by removing all music from the iPhone, and then putting it back on. Along with the repaired music came 3 gigabytes of "Other" data again! I still have 3 gigabytes free, but am puzzled as to why 4 gigabytes of audio files need to be accompanied by 3 gigabytes of "Other" in order to be played back from the iPhone?]

[Update 2: Further web searching suggests the classic method of backing up your iPhone to your computer, erasing the data from the iPhone, and restoring from the computer backup. This resulted in a significant reduction of "Other" data, giving me nearly 6 gigabytes of free space again.]