Getting your app bundle version in Swift

Just a quick post, more for reference than anything else. It’s common practice nowadays as an App Developer to display the current app version somewhere in your app. Sometimes in loading screens, login screens, or in menus. It’s super easy to do this, all you need is the following code:

If your app version was 1.2.3, the above code would set the text property of a UILabel named appVersion to “v 1.2.3”.

Migrating to Swift 2

I was recently tasked with migrating an iOS codebase from Swift 1.2 to the latest Swift version, which at the time of writing is 2.1. Anyone else having to do this will no doubt, like myself, have had mixed results.

If your codebase is small, the Xcode migration tool should take care of most of it for you. However, if your codebase is more mature, you’re going to run into some issues!

Unfortunately for me, the app I was migrating fell in the “super mature” category. I still decided to run the migration tool and see how it coped – not well is the answer. It took about 20 minutes to run and the most helpful thing it did was update some Classes to new versions (e.g. Printable becomes CustomStringConvertible). It also changed some occurrences of “var” to “let”, which got rid of some of those pesky warning messages. But other than that, everything else had to done manually.

When trying to build my app, it instantly failed with hundreds of errors….

The first thing I noticed, were some new function signatures that now throw errors, a new feature of Swift. These new function signatures are basically there to say, “Hey, this function could make a boo boo, so you need to make sure you cater for this”. It’s actually quite straightforward to handle, take the example below:

If for some reason, you can guarantee a function will never fail, you can get rid of the do/catch block and simply write:

Note the difference here of “try!”, not “try”.

There was also an update to SCNetworkReacabilityFlags which means you can no longer compare it to an Int using the equality operator. Instead you now use the .contains() function to do the same thing. Where previously you would write something like:

You now achieve the same result by doing:

Excellent!

There are many more APIs that were updated, luckily for me these were the main and most frequently occurring ones I had to deal with.

Happy migrating folks.

Crop image in UIScrollView with Swift

I recently had a project where I had to crop a UIImageView based on it’s scale, after having been resized within a UIScrollView. The solution to this task turned out to be incredibly simple – see the code below.

Note:
imageScrollView – reference to our UIScrollView
editingImage – reference to our UIImage used to create our UIImageView which itself is a child of imageScrollView.

Happy coding 🙂

 

Simple audio recording using Swift

Just a quick post on how to get audio recording happening on iOS using Swift.

All you need to do is copy this into your UIViewController.

The code itself is quite self explanatory. Make sure at the top of your class to include the line, “import AVFoundation”, this contains the libraries necessary to interact with the audio file system on iOS.

toggleRecording() – The function to call to toggle your audio recording on/off. You’ll need to setup a button linking to this function.

That’s it. Playing the file back is also quite straight forward, but I’ll include that in a later post.

 

Microphone input wave like Siri using Swift

In recent weeks I’ve been developing an audio recording app in my spare time, just for fun.

I wanted to be able to implement a Siri like wave that animates in response to sound input via the microphone as below:

siri wave

Luckily I was able to find an Objective-C implementation on Github called SCSiriWaveformView which was very well documented – thanks to Stefan Ceriu for this.

Since I’m focussing mainly on Swift these days, I’ve put together a Swift version including the files downloaded from Github. The full source code is below.

If you’re unsure on how to import and use Objective-C files in Swift, check out my earlier post on how to do this here.

Enjoy!

 

Calling Objective-C from Swift

Until recently I hadn’t had to do this, but I was surprised at how easy it was. There is an excellent post that provides a walkthrough on this over at stackoverflow here.

In summary, the main steps are:

1. Add the Objective-C files to your project (drag and drop), and when prompted to configure an Objective-C bridging header, hit Yes! For example purposes, let’s call the file we’re importing “TestClass”.

2. In the newly created bridging header file (it will be in the format “YourProject-Bridging-Header.h”), add a line: import “TestClass.h”.

3. Then inside a Swift class, all you need to add is: var instanceOfTestClass:TestClass = TestClass()

And that’s it, easy!

Fetch custom photo album using Swift

I had a project recently where I had to parse the photos inside a photo album at runtime. The code for this is surprisingly simple, but if you’ve never done it before it can be an interesting challenge in Swift.

When loading a custom photo album on iOS, we’re in fact fetching a PHAssetCollection that matches a given string. The given string when searching for an album is the album title as it appears in the Photos app. Use the following code to fetch the collection:

Say the app also needs to create the photo album, e.g. if the album does not exist. In order to do this, we can use the following code: