Category Archives: iPad

App-o-Mat updates

I started App-o-Mat as an iOS tutorial site back when I was consulting. It mostly had cordova screencasts.  I’m going to be doing more writing about iOS there (native and possibly cordova-based).

You can subscribe to the mailing list there if you want to get updates. If you have requests for topics to write about, let me know.

Here are the latest posts:

How do I pass values from a VC back to the VC that presented it?

Ok, so you have a view controller that brings up another view controller. Let’s call the first one FirstVC and the second one SecondVC. FirstVC either presents SecondVC or there is some segue that it uses to bring it up.

How do you keep Storyboards from causing merge problems later?

There’s this myth in the iOS community that “professional” iOS developers never use Interface Builder. It’s meant to imply that coding your interfaces is always better, and if you don’t do it, you are somehow less of a programmer. The myth perpetuates the idea that IB is a crutch, a toy, something that only newbies use.

I call BS.

How do you pass values to a VC in a segue?

A theme I see a lot on StackOverflow is a developer makes a View Controller that collects some information from a user and wants to use it on a VC that they bring up in a segue.

 

Introducing 3D-o-Mat

3D-o-Mat is a simple app that creates the type of 3D photos that you view with red/cyan glasses.

I have been writing apps that do this for a while. It was one of the first things I wrote with DotImage back when I joined Atalasoft in 2006 and ported their image processing commands to WPF. Then, I wrote a simple version for iOS that I showed to middle-schoolers when I was a volunteer for DIGITS.

I was recently invited to speak at a Smith College Python programming summer program for HS girls and I decided to make this app more real.

PS: here’s a tweet sized version of the app in Python that I shared with the class the last time I spoke:

This gist shows a better way to do it that preserves the color in the original photos.

NerdSummit 2017 Talk: Introduction to iOS Development through Apps

On March 18th, I’ll be giving a presentation to teach iOS development by looking at completed apps and customizing them.

If you are planning to attend and want help after the talk to set up your machine and get started on the exercises, here’s what you’ll need:

  1. To do the exercises, you need a Mac with Xcode 8.2+ installed. If you don’t have access to a Mac, I think we’ll have enough people with one and can pair you with someone.
  2. We’re going to be forking apps on GitHub, so having a GitHub account already would be good.
  3. You don’t need a device — we’ll be able to use the simulator for all of the examples, but if you want help getting apps on devices, sign up for a free Apple Developer account.

It’s a beginner talk, so anybody with an interest in programming will get something out of it. It will help if you have some programming experience (in any language).

Here’s the plan

  1. Basic Swift (enough to be able to read the apps)
  2. The MVC pattern as implemented in UIKit
  3. Interface Builder (connecting outlets and actions)
  4. Then, we’ll fork an app and make some customizations
  5. Based on the group’s questions, we’ll cover as much iOS Development and Swift as we need.

The idea is that the apps we’ll fork are generally useful apps that people might want a custom version of. All of the code is open-source, and you’ll be able to continue to develop them after the workshop if you wish and release them to the App Store.

I’ll introduce the apps in subsequent blog posts here (I have to make them).

There will be handouts so you can work on your own after the talk.

Handling Pluralizations Correctly in Strings

Earlier this year, Trello launched in 21 languages. I worked on the i18n effort of the iOS apps, and I have been collecting thoughts for a series of blog posts. The first one, about plurals, was published today on the Trello Tech Blog. It begins

On page 52 of my copy of K&R, in a discussion of the ?: operator, is this line of code

printf("You have %d item%s.\n", n, n==1 ? "" : "s");

And thus began my decades-long proliferation of plural-unfriendly strings.

To see why, read the rest of Lessons from Internationalizing Trello, Part I: Plurals on iOS

As a follow-up, I found a Russian translation of K&R. Here is how that line of code is translated

printf("Вы имеете %d элемент%s.\n", n, (n%10==1 && n%100 ! = 11) ?
    " " : ((n%100 < 10 || n%100 > 20) && n%10 >= 2 && n%10 <= 4) ?
    "а" : "ов");

Which is another way to do it, I guess.

iOSDevUK Lightning Talk about Swift Playground Books

Links from the talk

  1. Playground Book package format
  2. WWDC 2016 talk about Swift Playgrounds
  3. My fix of Talking to the Live View
  4. Ash Furrow’s playgroundbook command-line tool
  5. My PlaygroundSupportMock framework for building an app from a book
  6. My pgbookc script for turning a Contents folder in a host app into one that you run as a book
  7. My ShapeSearch playground book and host app built using those tools

Read my previous post on authoring a Swift Playground Book for more information.

Authoring an iOS Swift Playground Book

I expect at some point Apple will have some kind of Playground Book authoring solution, but until then, I’ve hacked together something basic that I am ready to share.

Take a look at my previous post if you’re getting started with playground books. The important things to know are:

  1. The book is a package with a specific folder layout of chapters and pages.
  2. The left-hand side is a stateless playground with markdown and code.
  3. The right-hand side is a stateful live view that can host a View Controller and do pretty much anything you could do in an app.
  4. The playground view and live view can communicate via a simple asynchronous inter-process communication mechanism.
  5. The book is delivered as Swift source files, assets, and plist config files.

As I mentioned in the last post, Ash Furrow has a way to generate this format from a .yaml description and playground files. Even if you don’t ultimately use it as your book source, it’s a good way to generate your initial package structure. The same project also has a linter, which you should use no matter how you author playground books.

The drawback of trying to write an entire book this way is that some of the code lives in the .yaml file and is hard to write and check.

I wanted a way to:

  1. Write the code in Xcode (to get autocomplete, docs, etc)
  2. Build it there too
  3. Run it in a host app where I could test the LiveViews in the simulator
  4. Unit-test the playground and LiveView code

I started by putting a book’s Contents folder into a skeleton app and loading the first page’s LiveView as my rootViewController. The first problem is that on the iPad, there’s a framework called PlaygroundSupport with support for Swift Playground features like the messaging between the playground and LiveView. This framework is not currently available in Xcode.

To make it buildable in Xcode, I made PlaygroundSupportMock. This is a work in progress, and I will add more support of PlaygroundSupport as I need it. You can add it to a skeleton app via Carthage.

The second problem is that the format requires each page folder to have a Contents.swift and an optional LiveView.swift. But, apps cannot have two files with the same name in the same module.  To get around that, I wrote a script called pgbookc to copy the Contents folder from the host app and rename any Contents-*.swift or LiveView-*swift files (removing the -* suffix).

Finally, there are several places in the book where code that would work in the book could not work in an app (and vice versa). So, I added a feature to pgbookc to look for special comments in the Swift files that help it to alter it to work as a book.  For example, here is some typical code you might find in a book (to send a PlaygroundValue dictionary to the LiveView and call it from the playground itself)

func hasAttribute(shape: Shape, attribute: ShapeAttribute) -> Bool {
    let attributes: Set = shape.attributes
    return attributes.contains(attribute)
}

hasAttribute(shape: cloud, attribute: .IsGray)

The problem is the last line, which must be at the top-level in a playground (but cannot work in an app).  So, pgbookc will look for any line that ends with

// REMOVE LINE

and removes that entire line. This lets you write:

func Contents0102() -> Bool { return // REMOVE LINE
hasAttribute(shape: cloud, attribute: .IsGray)
} // REMOVE LINE

The first and last line will be stripped from the book, but it’s there for the app build. It’s also there for tests, which lets me write:

func testContents0102() {
    XCTAssert(Contents0102())
}

To make sure any playground code in a Contents.swift file works.

I have an (almost done) Playground Book that I am writing using this technique. It’s called Shape Search and has the reader build a simple guessing game that teaches how to do a binary search.

Swift Playground Books: 2-Sep-2016

I imagine a lot of the information here is going to go out of date fast, but if you are looking at playground books today, here is where to start.

  1. The WWDC 2016 talk, Introducing Swift Playgrounds, has a lot of technical detail about the .playgroundbook format and how the LiveView proxying works.
  2. The demo for that talk is available on Apple’s dev site, but it’s out of date. I’ve been updating it for each iOS 10 Beta (to track Swift 3). Go to my GitHub to get the fixed Talking to the Live View sample.
  3. The documentation for the .playgroundbook format can be found on the Apple Developer site.
  4. Erica Sadun, who has written the book on Xcode playgrounds did some early investigations of iOS Swift Playgrounds. You might want to read them — here is Part I, Part II, Part III, Part IV,  and Part V.
  5. Ash Furrow made a playground book linter and authoring tool (source). You can use the linter on any .playgroundbook (however you make it), and the authoring tool allows you to specify the parts of the book in a .yaml file, and it puts each piece in the correct file and folder for you in the .playgroundbook package.