How #available works in Swift

This article from the Big Nerd Ranch blog goes back to 2016, but it’s a great little rundown by Mark Dalrymple of #available, introduced back in Swift 2.0.

Hi! I’m #available!

Sometimes we just use language features like this and don’t really think about what’s going on under the hood when we use them, or perhaps we have a mental model based on past assumptions. However, Mark does a an excellent job filling in some of the details on this handy feature of Swift, explaining not only what it is but also what it is not.

The article also clarifies Xcode’s Target SDK version and Deployment Target version, which can initially be a point of confusion to many developers on the iOS platform.

Here’s a great summary of the article from the article itself (a TLDR;):

The take-away points: #available is not #if—it is not conditional compilation based on OS platform. Instead, at compile time, #available tells the compiler to temporarily raise the SDK level above your deployment target so that you can safely use newer API. At run time, the code will execute depending on the version of the OS the app is running on.

Handy Swift Resource: Hacking With Swift’s Auto Layout Cheat Sheet

This is a pretty great one-stop-shopping resource for Auto Layout by Paul Hudson.

The Auto Layout cheat sheet – Hacking with Swift

Auto Layout is a powerful tool for creating flexible, maintainable rules for your user interface. It can also be a real brain vortex if you’re unlucky – it’s something that makes hard things easy and easy things hard.

To help relieve the pain, in this article I’ve put together code snippets to solve a variety of common Auto Layout problems: creating constraints, animating constraints, adjusting constraints at runtime, and more.

As with all of Paul’s articles, it’s well thought out, and it’s a useful resource to stash away for the next time you need to do in-code work with Auto Layout and Swift.

It is disappointing that he does not use the word “vexing” anywhere this blog post as I had thoroughly expected him to do (and would have been so appropriate in any article about Auto Layout), but the tips are so good, I’ll overlook it this time.

Excellent resource.

When Worlds Collide: How to call complex Objective-C selectors from Swift

When developing in Swift, you will eventually need to interact with Objective-C APIs. Most of the time this is fine, and fairly straightforward to do using #selector.

However, every once in a while you will need to invoke an Objective-C selector that you did not write (usually when the selector is part of the iOS or macOS SDK), does not have a unique signature, or is just pretty hard to figure out.

This article put out by Big Nerd Ranch’s Mark Dalrymple is an excellent rundown of how to properly invoke Objective-C selectors from Swift. He even covers really hairy signatures that may be very difficult to come up with on your own at first glance.

Big Nerd Ranch — Hannibal #selector

The selector is key to Objective-C’s dynamic runtime nature. It’s just a name that’s used, at runtime, as a key into a dictionary of function pointers. Whenever you send a message to an Objective-C object, you’re actually using a selector to look up a function to call. Sometimes selectors bubble up in Cocoa/CocoaTouch API, and you will need to deal with them in Swift.

iOS Dev Break Episode 009: It Was A Very Googly Experience

The latest episode of iOS Dev Break has arrived! This one was recorded just before WWDC, and a new episode will be published shortly!

It Was A Very Googly Experience – IDB009 | iOS Dev Break

In this episode: The return of iOS Dev Break! Adventures with the Google Play Store, Swift 4 and the Codeable protocol, plus AirPods and their amazing user experience!

Video: Forward Swift 2016 – “How Hot Is My Coffee? Sensors, Core Bluetooth and Swift!”

Somehow I don’t think I broadcasted this when it was available so now seems a good a time as any…

My Core Bluetooth talk from Forward Swift 2016, “How Hot Is My Coffee? Sensors, Core Bluetooth and Swift!” has been up since last year, but thankfully it’s still up!

Evan K. Stone: How Hot Is My Coffee? Sensors, Core Bluetooth and Swift! – Forward Swift 2016

Fitbit, Withings, Nest. The Internet of Things (IoT) innovation tidal wave has just begun, and those are just three of the more well-known players in the IoT and connected gadget landscape. It’s critical that iOS developers know how to connect and interact with BLE prototyping devices using the Core Bluetooth Framework provided by the iOS SDK. In this session, you’ll get the jump-start you need to begin creating engaging apps that bring these devices to life.

Big thanks to the awesome folks at Forward Swift!

Video: Forward Swift 2017 – “What’s this React Native Thing I Keep Hearing About?”

Here’s the video for the talk on React Native I did for Forward Swift in March of 2017 in San Francisco! I’m very glad I didn’t have to wait until November for this one – the Forward Swift team has done an amazing job of getting out their videos in record time!

Evan K. Stone: What’s This React Native Thing I Keep Hearing About?

In our daily lives as iOS developers, we can usually happily keep coding away in Swift and ignore what’s going on in other software development communities, like that of JavaScript. However, there may be some advantages to at least becoming familiar with what’s going on in the world of React Native, and in this session you will get an overview of what React Native is, and why it could be a useful addition to your toolbox an iOS developer.

Video: AltConf 2016 – “Saving Lives with iBeacons”

Evidently this got posted to Realm’s website back in November, and I was not informed. Better late than never!

Evan K. Stone – Saving Lives with iBeacons

You’ve probably heard of iBeacons, but have you seen them up close or developed apps for them? iBeacon technology is a fun and easy way to start working with Bluetooth Low Energy devices. Cloud City Development has been working with Pop Up Labs at MIT to help develop a solution for educating health professionals about the Maker Movement and how it positively affects the medical community. With the help of iBeacons, health professionals have learned how to start creating their own devices and tools!

In this talk from AltConf 2016, Evan will introduce you to iBeacons, show you examples of how to interact with them in an iOS app, and demonstrate how iBeacons were implemented in a Medical Maker Kit. By helping medical staff learn about the items in the kit, they are able to get creative, build new projects, and ultimately save lives! This talk will give you a glimpse of what can be accomplished with iBeacons and will encourage you to think of new ways in which Bluetooth Low Energy devices like iBeacons can be used to design and develop creative solutions that enhance the experience of life!

Zero to BLE Part Two Core Bluetooth Post Updated for Swift at Cloud City Development Blog!

I’m happy to announce that I recently updated Part Two of my Zero-to-BLE series on Core Bluetooth Post Updated for Swift at Cloud City Development Blog! It’s been updated for Swift 2, because when I wrote it, Swift 3 hadn’t officially been released and I honestly thought it would be published well in advance of the September 7 Apple Event.

Zero to BLE on iOS – Part Two – Swift Edition

Better late than never!

Who knows? Maybe there will be a Swift 3 version in the future? For sure all my code samples will be in Swift 3…

Hope you like it!

How to Query Custom URL Schemes in iOS 9 (Hint: it’s different now.)

This was a new one on me and I discovered it accidentally.

In one of my apps, NineOneOne ~ One-Touch Emergency Dialer, I query for the presence of the Skype app, so that an emergency call can be placed using Skype instead of the phone app.

Why would one want to do this? Well, as it turns out, I wanted users who had an iPad or an iPod Touch to be able to use NineOneOne on those devices without the presence of the phone. The obvious candidates were Skype and Google Voice, and at the time Google did not provide a way to launch Google Voice via custom URL scheme. Therefore Skype was it.

Prior to iOS 9, this process was super easy. Just call UIApplication’s canOpenURL method:

self.skypeEnabled = [[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"skype://"]];

Unfortunately I noticed that this was breaking in iOS 9 and there was a little message in Xcode’s console:

This app is not allowed to query for scheme skype

…and this baffled me since I hadn’t heard of any changes to this API and it seemed like a pretty standard and common function to use.

The New Way
It turns out that, while this method still works in iOS 9, you have to take an extra step beforehand. At about the 10-minute mark in the video for Session number 703 at WWDC 2015, you will find that the presenter shows the new process going forward.

You need to open up your Info.plist file, and add a new record, LSApplicationQueriesSchemes, define it as an array, and then add the schemes that you will be querying in your app to that list:

LSApplicationQueriesSchemes-skype

Once I added that key and the “skype” URL scheme to the array in the Info.plist, the URL scheme querying immediately started to function again!

This strikes me as a way for Apple to keep track of who’s-querying-who’s-app by using the app’s metadata, and also for iOS 9 to deny any URL scheme queries that are not in the list, so we need to now be explicit up-front about which URL schemes we are interested in.

Understanding How Notifications Work on Apple Watch

I’ve had my Apple Watch for a little while now, and I have really found myself relying heavily on it as a device primarily for notifications.

The one thing that has really bothered me, however, is the somewhat random nature in which notifications are delivered to The Watch. I’m not sure I want all notifications to go to the watch, but I’d certainly like to know why some don’t end up on my watch and others do.

Finally I thought I’d take the time to find out, since I happen to be doing a lot of work lately with push notifications in iOS and I was wondering why certain notifications were delivered on my watch and others weren’t.

Turns out Apple has a little explainer on their support site that addresses that very subject:

About notifications on your Apple Watch – Apple Support

In a nutshell the rules go like this:

  • If your iPhone is unlocked, you’ll get notifications on your iPhone – not your Apple Watch.
  • If your iPhone is locked or asleep, you’ll get notifications on your Apple Watch, unless your Apple Watch is locked (i.e. you haven’t entered your passcode).

As a side note, the support article also mentions the following:

When you dismiss notifications on your Apple Watch, they’re also dismissed from your iPhone. You might get certain notifications on your watch that you need to respond to using your iPhone.

I hope this helps others who may have been a little mystified about how notifications work on Apple Watch!