Using protocol default implementation instead of abstract classes

When I started using Swift the first thing I started missing was the support for abstract classes. I was used to using abstract classes for my ViewModels, especially to implement the template pattern, but also to provide some basic methods, like showing dialogs, that the ViewModels may need. Of course there are some “tricks” to do abstract classes like checking in the class type in constructor and throwing an error if it is the abstract class type, but Swift is a language based on protocols so there are better ways to achieve the same results just using the protocols.

Imagine you want to add a functionality to show iOS alerts to some of your ViewControllers. In a language like C# you would create an abstract class, something like BaseViewController, add a ShowAlert method to it and make all your ViewControllers inherit from this base class. Most languages nowadays only support single inheritance, so you would put all the functionality your ViewController may or may not need to your one abstract class. But you can typically implement as many interfaces as you like.

Using Swift and protocols there is another way. Protocols in Swift are really similar to interfaces in languages like Java or C# but with some neat stuff added to them like default protocol implementation.

[Read More]

Creating animations of your apps

They say that a picture is worth a thousand words so whenever I create an issue, a pull request or write a blog post about and application or some visual stuff I include a relevant image. Sometimes an image is not enough and an animation is needed to better describe the issue, or show the content of your pull request.

There are some good tools to help you to create animations like this on both Windows and macOS.

[Read More]

Simpler and safer iOS custom table view cells with Reusable

When you create a UITableViewCell that you want to use with multiple UITableViews and design its view using a XIB file you know that registering and using it involves the usage of string constants. When you register your custom UITableViewCell with the UITableView you use a string as XIB name and a string as the reuse identifier. Then you use the string reuse identifier again to actually use it. There must be a better, safer way, right? Of course there is, just use Reusable.

Reusable is a Swift mixin for reusing views easily and in a type-safe way for UITableViewCells, UICollectionViewCells, custom UIViews, ViewControllers, Storyboards. It contains protocols you add to your classes and let the magic (the default implementation for those protocols) happen.

So how do you get rid of all those strings when using custom cells with UITableView? First, add the NibReusable protocol to you custom cell class

[Read More]

Making UITableView's header 'stickier'

Working on na iOS app I had to solve a interesting UI problem. The screen had to contain a UITableView with a header. The header should not have been visible when the screen was displayed. In fact the header should not have been visible when the user just scrolled the UITableView up and down, it only had to become visible when the user “dragged” the UITableView down, similar to doing pull to refresh. Scrolling the UITableView then hides the header again.

To better imagine the requirements, take a look at this animation

Notice that you see that the header is there but I have to really drag the UITableView to make it visible. It then disappears when I scroll the UITableView.

[Read More]

Delaying Disqus comments to save (a ton of) requests

My blog is a static website generated using Hugo and running on Netlify (you can read about my move from WordPress to Hugo in a previous article) but I still want to allow users to leave comments on my posts so I use Disqus like many other blogs.

The advantage of Disqus is that you do not need to host the comments yourself, and I really cannot with a static website, you just embed a JavaScript file and everything is taken care of for you. There are a few alternatives to Disqus but none of them works very well so there is basically no other option if you want to have comments on a static website.

The problem with Disqus is that this one JavaScript file you embed to your website makes about 50 requests when a page loads. And that is a lot!

[Read More]

Re-signing iOS apps

There are some times when you need to re-sign the IPA of your iOS app with a different certificate. For example, an external developer creates the IPA for you, but it is signed with their personal certificate and you need to re-sign it with yours to deploy it to the App Store. Or one of your clients does not want to have their employees install the iOS app from the App Store but wants to distribute it directly using their MDM tools.

The second case is a bit more complicated, because it involves creating a new app identity for the app. When you change the app id if your app, your push notifications will stop working and you need to also generate a new APNS certificate with the new app id and deploy it to your server. Here is everything you need to do, step by step.

[Read More]

Solving problem after upgrading Windows 10 to EFI

When I upgraded my Windows 10 installation to the Creators Update I wanted to take advantage of the new MBR2GPT.EXE tool. This tool allows you to upgrade your Windows 10 installation to EFI without having to reinstall or loosing any data. My motivation for moving to EFI installation was to be able to boot Windows 10 from Clover, the macOS bootloader in my hackintosh installation, making switching between Windows 10 and macOS easier without involving the BIOS boot menu.

The upgrade from MBR to EFI went fine on both my desktop computer and my Thinkpad but there were a few thing that needed fixing afterwards.

[Read More]

Formatting Swift code in XCode

When I started using XCode I was really surprised about the really poor implementation of its code formatting functionality. It kind of formats the alignment of the code but ignores unnecessary spaces and a lot of other things. Formatting the source code and keeping the style consistent is really important to me so I was looking for a solution. I found some linters like SwiftLint but I was interested in a tool that will actually format the source code for me on demand. I found SwiftFormat.

SwiftFormat

SwiftFormat is a code library and command-line tool for reformatting swift code. It applies a set of rules to the formatting and space around the code, leaving the meaning intact.

[Read More]

Generating a list of libraries your iOS app uses

If you work on an iOS app that is a bit more corporate you probably need to show the list of all the libraries you use with their licenses somewhere in the app. Creating and updating this list by hand is a pain. If you use Carthage to manage all your dependencies (and you really should) there is a handy script by Piet Brauer I contributed to that will help you.

When you run the script using $ ./PATH_TO_YOUR_SCRIPT/fetch_licenses.swift Cartfile.resolved OUTPUT_DIR it reads your Carthage file, gets all the libraries you use, downloads their licenses and stores them all in a single plist file. The plist file contains the name, license name and full license content for every library in your Carthage file.

There is currently no support for using multiple Carthage files (when you have more projects in your workspace), you need to generate the plist file for each of them separately and then merge them manually. But you can set up a bash script to do it for you.

[Read More]

My experience running a hackintosh

A few months ago I decided to take part in an iOS project. The first problem I needed to solve was to be able to run macOS Sierra and XCode. I did not really want to buy an overpriced MacBook without function keys or and underpowered Mac Mini. Especially when I own a more than 3 years old desktop computer that is still usable for all my needs. A few iOS developers I know recommended I go the Hackintosh way.

Hackintosh

Hackintosh is PC that runs macOS. This configuration is not supported by Apple but it is possible if you have the right hardware since Apple has been using a fairly standard PC hardware for the last couple of years. For example you cannot us any new GeForce 10X0 (Pascal) because there are no Apple computers with those new graphic cards so there are no drivers yet (NVIDIA has released new drivers supporting all the Pascal graphic cards). But if you have an older GeForce like me or an integrated one, you will be fine. The tonymacx86.com website, basically the central hub of all the Hackintosh information, regularly publishes a buying guide that can be useful if you want to buy a new computer and install macOS on it.

If you do not wish to install macOS directly on your hardware you can run it in a virtual machine, but the performance will never be very good. Some people do it for Xamarin development when they just need to compile their project and run the simulator, so there are a few tutorial on how to do it. There is also an interesting blog post series about a virtual hackintosh. I tried running macOS in WMWare on my Thinkpad T440s but the performance was not good.

[Read More]