Architecting iOS apps: Coordinators

When switching from Windows Phone development to iOS I had about 3 months to learn iOS and Swift before starting the work on an actual iOS application. I had a chance to build the application from scratch with a colleague so I wanted the application to be really well written and architected.

I started to look at some iOS tutorials and other peoples’ iOS code. Learning and using Swift was easy (read more about my Swift experience in a separate blog post) but when reading about using the iOS SDK and especially application architecture I found stuff that I really disliked.

There were three big things in particular that I disliked, that I want to show you together with solutions I found. This first post deals with navigation.

The problem

When going through some iOS tutorials I found code like this a lot

class ProfileViewController: UIViewController {
  
  @objc func donection(sender: UIButton) {
    let vc = PreferencesViewController()
    navigationController?.pushViewController(vc, animated: true)
  }
}

When you are a long-time iOS developer, you may have seen and probably written code like this. All the tutorials contain code likes this. It may look perfectly OK to you. But for me, coming from the .NET world, this was a real WTF moment:

  • Why would anyone write code like this?
  • Why the strong coupling between those two view controllers?
  • Why an assumption the view controller is embedded in a navigation controller and we always want to do a push?

This code looked absolutely awful to me and I never wanted to write a code like this. So I started looking for better approaches and solutions. And I found coordinators (sometimes called flow controllers).

The solution: Coordinators

The idea of a coordinator is simple. In your application you probably have some flows, like registration flow, user settings flow, product purchasing flow, etc. Every flow is managed by a coordinator. The role of the coordinator is to know which view controller to display at a certain time.

[Read More]
swift  ios 

Adding MobileIron AppConnect to a Swift application

If you work on an iOS application intended for corporate environments, you are probably familiar with MobileIron AppConnect, because it is the most commonly used MDM solution. They have an SDK for iOS with stated support for Objective-C, Xamarin C# bindings and an Cordova plugin. If your application is written completely in Swift, there is some bad news in the documentation:

NOTE: The AppConnect for iOS API supports apps written in Objective-C. It does not support apps written in Swift.

Luckily, this is not true, you can integrate the AppConnect SDK to an application written entirely in Swift, you just need to do a few more steps.

First, add the SDK to the project exactly as the documentation says:

  • Add AppConnect.framework to your Xcode project
  • Add the libcrypto.a library
  • Add the libProtocolBuffers.a library
  • Link the Security framework
  • Link the Mobile Core Services framework
  • Link the Local Authentication framework
  • Add linker flags
  • Copy bundle resources from AppConnect.framework
  • Register as a handler of the AppConnect URL scheme
  • Declare the AppConnect URL scheme as allowed

When you encounter the Use AppConnect’s UIApplication subclass step you have a problem, you cannot do it in a Swift application the same way as in Objective-C. You need to use the Info.plist in your project and add a key called NSPrincipalClass with the value of AppConnectUIApplication instead. This ensures your main application class inherits from the required AppConnect class.

<key>NSPrincipalClass</key>
<string>AppConnectUIApplication</string>

[Read More]

Writing a simple Pascal interpreter in Swift

About a month ago I stumbled on a series of blog post called Let’s build a simple interpreter by Ruslan Spivak. In these blog posts the author write about building a simple interpreter for the Pascal programming language in Python. It is very well written and explained, I would even say that all the concepts are explained better than in the compiler course I took at the university.

In that class I had to write a Pascal compiler not interpreter in C++ and with tools like lex and yacc so I remembered most concepts but the interpreter is a bit different than a compiler and not using any external tools makes you think more about the problem.

Motivation

I felt a bit nostalgic and wanted to tackle an algorithmically challenging problem, especially because Pascal was my first programming language back in high school so I decided to follow along and write a simple Pascal interpreter in Swift, the language I switched to from C# nearly a year ago.

Swift is a different language than Python so I had to do many things differently, especially those where the Python implementation relayed on the dynamicity of the language.

But the biggest challenge was that the series ended before introducing function calling and the call stack so I had to come with a good way to do this. I also wanted to support recursion and basic loops and flow controls.

Goal

The goal was to be able to interpret a Pascal program like factorial computation or a simple number guessing game.

[Read More]
swift  ios 

Workaround for receive remote notification callback not getting called in foreground on iOS 11

When your iOS application receives a push notification while in foreground, the didReceiveRemoteNotification method in the application’s AppDelegate gets called. You get the whole push notification payload and you can react to it. But there is a problem introduced in iOS 11, then fixed and then broken again (like many things done by Apple these days) that didReceiveRemoteNotification does not get called when a push notification arrives and the application is in foreground. This can be really bad if your application depends on reliable push notifications while running.

The worst thing about this problem is that everything seems to be working while you are debugging the application from XCode. The method gets called, the payload is available. But when you open the application in your iPhone or iPad without the debugger attached, didReceiveRemoteNotification just never gets called. There are many developers reporting this problem on the Apple forums and on StackOverflow.

[Read More]
ios  swift 

Preventing Windows drives from getting automatically mounted on macOS

If you run macOS side by side with Windows or have some drives formated with NTFS, you may not want them to get automatically mounted when you start macOS. I have a Windows 10 SSD with NFTS and a data HDD with NTFS next to my macOS SSD and I do not use any o those two drivers when booted in macOS, so I was looking for a way to have them not mounted at startup.

The main reason for this other than them not being shown in Finder is that macOS spins the data HDD from time to time for no apparent reason and I really do not want this.

In a classic Linux system you could edit /etc/fstab. This file can be also created on macOS, but Apple does not recommend editing it directly but to use sudo vifs. The drives should be addressed by their UUID as opposed to their “location” on Linux, so you first have to find that UUIDs.

When you have the drivers mounted, run diskutil info /Volumes/"Volume name" | grep 'Volume UUID' where “Volumne name” is the volume name as shown in Finder. This will get you just the UUID.

[Read More]

My experience with Swift after 9 months

About 9 months ago I basically left the world of Windows development because of the death of Windows Phone and me being really fed up with Microsoft. I got a chance to work on a complete native rewrite of an iOS application so I turned my desktop PC into a hackintosh and started learning Swift and iOS development.

This post describes my experience and feelings about Swift, the language, tooling, resources after 9 months. I will probably write another post about my experience with iOS development and the iOS community.

Swift language

I think Swift is a nice modern language, it reminds me of a combination of C# and F#, so two languages I really like. The introduction of Swift got me first thinking about switching to iOS development, Windows Phone was already dying at that time.

I tried iOS development with Objective-C some time ago, did some tutorials but the language just felt wrong. Not only the strange syntax, I do not really care about that, but the expressiveness of the language compared to C#. I had to write so much more code to do anything. Swift is just much better with that.

As a side note I remember reading Masterminds of Programming a few years back, the interview with one of Objective-C authors and his hate about C++ saying he did everything better that Stroustrup and thinking “I really do not like this guy” (I do not remember if it was Cox or Love).

[Read More]
swift  ios 

Using Visual Studio Code as Git merge tool

Visual Studio Code is a neat editor with many good functionalities but I especially like the way it shows Git merge conflicts. Instead of a 2-way or a 3-way split it just shows one window with both changes, nicely highlighted with colors and actions.

I like this feature so much I decided to set Visual Studio Code as my Git merge tool for both the command line and Git Tower.

Command line

Using Visual Studio Code as a merge tool for Git when using command line means editing your .gitconfig. You just need to define a new tool called code and set it as the default merge tool.

[merge]
        tool = vscode
[mergetool "vscode"]
        cmd = code --wait $MERGED

[Read More]
git  macos  vscode 

Filling UITableView with data from bottom to top

If you work on something like an chat app, you may need to use the UITableView in a way where data is filled from bottom to top. An example of this is a chat detail screen, where you want the UITableView to show the latest messages at the bottom when loaded, new messages are added to the bottom and immediately shown and older messages are loaded on top when the user scrolls to the top of the UITableView.

There are multiple ways to achieve this, each with some advantages and disadvantages.

Scrolling

The first simplest idea that comes to mind is using the UITableView as is and just scrolling it when necessary:

  • Scroll to bottom when the initial messages are loaded
  • Scroll to bottom when a new message is added
  • When older messages are about to be added to the top, remember the position, add the older messages, scroll back to that position

The first two situations are easy to accomplish, but the last one is not. I could not find a way to make it works without a visible scrolling effect.

Rotating UITableView 180 degrees

Another solution is to rotate the UITableView by 180 degrees; rotating it upside down. Of course you have to also “flip” your data source but that is trivial to achieve. The advantage is that you do not have to do any scrolling when new messages are added to the bottom (which is the top of the rotated UITableView) and if you use batch updates instead of reload neither when older messages are loaded.

[Read More]
ios  swift 

More readable XCode build output for CI

If you use Continuous Integration (CI) builds or build your app from the command line using xcodebuild you know that the output is not pretty and not very readable. Reading the build output is important when a CI build breaks, but it is not easy when it looks like this

Many iOS developers were not satisfied with this so the xcpretty project was created. Xcpretty is a fast and flexible formatter that turn the output from screnshot above to this neatly formatted output

[Read More]
ios  macos  xcode 

iOS tip: Wireless debugging from XCode

One of the best XCode 9 features is the ability to deploy and debug iOS app on your device over WiFi, with no need to have the device connected to you computer by a cable. The only requirement is that the device runs iOS 11.

Setting it up is really easy. Connect the device using a cable like you normally do and go to Window | Devices and Simulators. You will see a new checkbox next to your iOS 11 devices called Connect via Network (see screenshot below), so check it. Now you can disconnect the cable and debug on your device over WiFi, the device has to be on the same network as your computer of course.

[Read More]
ios  macos  xcode