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.

How to Force Bluetooth Headphones to Use Internal Microphone in macOS

I love using my AirPods and my Urbanears Plattan 2 headphones with my Mac.

However, when macOS uses the microphone in AirPods or the Urbanears, the audio in the headphones goes to mono, sounding like an AM radio. It turns out that if you select “Internal Microphone” from Sound Preferences (or use the shortcut of Option-clicking on the Sound icon in your menu bar, and select “Internal Microphone” from the “Input” section), then your headphones go back to stereo and sound awesome again.

The problem is that—at least for me and some other users—macOS always selects the headphones’ microphone as the Input device when you connect your AirPods or other bluetooth headphones.

So I wanted to find a solution to this. Even if it involved coding up a solution.

Turns out there’s a really interesting AppleScript-based solution here:

Ask Different (Stack Exchange) – How to always use internal microphone?

I was fully prepared to do the AppleScript thing, or even go the extra step (as my reply to that solution suggests) and build a Mac menu bar app to handle this. But then I stumbled on this other solution that requires no coding (AppleScript/Swift/ObjC/Other) in High Sierra, and it turns out it’s super easy to configure.

Disclaimer: Please note that the answer was in the comment section of the article and it seems to work on High Sierra (I’m on 10.13.6), but I don’t know if this works on Mojave and beyond.

Open the standard “Audio MIDI Setup.app” utility, and click the + at the bottom to create an Aggregate Device. Then select the Internal Microphone as the only component in this aggregate device and select it as the default input. For some reason this prevents Mac OS X from selecting the Bluetooth Microphone from now on and it will stay on the internal one when you reconnect.

After creating that aggregate input, go to your usual System Preferences -> Sound -> Input (tab)

Source: http://ssrubin.com/posts/fixing-macos-bluetooth-headphone-audio-quality-issues-with-hammerspoon.html#comment-4018174387

Pro Tip: To get to Audio MIDI Setup quickly, an easy way is to just invoke your Spotlight Search (Cmd-Spacebar or whatever you have it mapped to), and type “Audio” – it should pick it up, or at least be in the list of results.

The great news is that—at least in my preliminary tests with disconnecting and reconnecting my bluetooth headphones—it appears macOS does in fact hold on to the Internal Microphone setting. My one concern at this point is whether that would impact connecting other microphones. I suspect it will be a matter of manually selecting the microphone in the Sound Preferences (or menu), but I can live with that because it’s the much-less-common scenario for me.

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.

Fixing Git SSH After Upgrading to macOS Sierra

Yes. I’m a latecomer to the Sierra game (due mainly to an audio hardware driver I had to wait on to be upgraded to Sierra, which finally was updated but probably won’t be updated to High Sierra…), and most developers have probably already run into this problem and have dealt with it accordingly. However, I thought it might be a good idea to capture this here for posterity…

Just this morning, immediately after upgrading to macOS Sierra, I experienced a problem by which Git began incessantly asking for my passphrase. This was an unwelcome surprise to me, but thankfully there’s a reasonably simple fix, thanks to this SuperUser suggestion:

macOS keeps asking my ssh passphrase since I updated to Sierra – Super User

In the latest version of macOS (10.12.2), this is easy to fix. Just edit your ~/.ssh/config and enable the UseKeychain option:

Host *
 UseKeychain yes

There is no need to change anything else. Now everything works the way it used to work before the latest updates. You do not need to add keys to ssh-agent.

In my case, I couldn’t just edit the config file in question, because it didn’t exist. Therefore, I had to create the config file in my ~/.ssh directory with the information mentioned in the SuperUser post.

Thankfully, after creating the file, re-launching Terminal, and entering my credentials one final time, it seems to have worked and Git is happy again.

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!

Google Chrome Pro Tip: How to Merge Multiple Tabs from Two Google Chrome Windows

I have been wanting the ability to merge two window’s collections of tabs in Google Chrome for some time now, because moving one tab a time from one window to another is tedious at best.

I thought I’d need an extension for it, but it turns out that you can already do it in Chrome without any extra extension! I found this tip on the Google Chrome Product Forums, which tells how to do it:

Merge two windows with multiple tabs

Left-click the first tab in the window you want to merge. While holding the shift key, left-click the right most tab. All of tabs for the current window are now selected, so you can drag and drop them as as group on the other window.

One thing the tip doesn’t mention is that you have to keep holding down the Shift key while doing this process to move all tabs, otherwise it shifts back into single-tab-move-mode.

Hope this helps!

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.