Deeplinking  (how to force out of Facebook's in-app browser and fallback on desktop)

Deeplinking (how to force out of Facebook's in-app browser and fallback on desktop)

So it turns out these in-app web views can't die quick enough (hello, SafariViewController!). Marty tweeted he was trying to get YouTube videos to open in the native iOS app rather than on the website but was finding it impossible. By default, the iOS app adds a URL scheme to open youtube.com links. When someone texts you a link to a video, Safari will redirect you to the app. However, Facebook's in-app browser (and Twitter's and I expect others) doesn't honour this and linking to the youtube:// URL scheme doesn't work either. Workaround With JavaScript It turns out, if…

Make UILabel Copyable in Swift

I've been working on an iOS app that called for some text to be copyable but not editable. I wanted to able to long-press and have the contextual menu show the "Copy" action. I'd initially thought about using a UITextField and disabling the edit functionality to achieve this. However that caused some issues with keyboards popping up even when editing was disabled. It struck me as strange that a UILabel isn't copyable. At least by default, as it turns out is actually fairly easy to make it so we can copy and paste out label value. Subclassing UILabel…

Swift "AnyClass" Type

TL;DR you're looking for MyClass.self When working with ConnectSDK, I came across something I hadn't before in Swift: the AnyClass type. ConnectSDK is an Objective-C based library so I'd imagine this is a rarity when working with native Swift code. Here's what it looks like: discoveryManager?.registerDeviceService(deviceClass: AnyClass!, withDiscovery: AnyClass!) I mistakenly though it was looking for an instance (i.e. MyClass()) but that threw an error. It turns out that in Objective-C each class has the static class property. In the case of ConnectSDK it's looking for the following: [AirPlayService class] [DIALService class] [CastService class] This…

Creating a Delegate in Swift

The Delegate pattern is one that's widely used is developing apps for iOS and OS X and it's one that's very simple to implement in Swift. It's primarily used to alert classes of changes or interactions in other instances. For example, UITableView's delegate has a variety of methods of which include notifying when a cell has been selected or edited. Apple describes a delegate as the following in their documentation: A delegate is an object that acts on behalf of, or in coordination with, another object when that object encounters an event in a program. Delegate == Protocol Creating a delegate…

Creating a sharedInstance in Swift

Or, "How the fuck to I make a singleton?" One of the patterns that seems to be very common in Cocoa is the sharedInstance pattern. You can see it in a few of the native classes to UIKit and Cocoa. For example: In UIApplication you havesharedApplication which returns the current application. You'll use it when wanting to display the network indicator for instance. On OS X NSNotificationCenter has the defaultCenter. NSUserNotificationCenter has the defaultUserNotificationCenter which again follows this principal. While the above may not share the same name, the all follow the same pattern and return an instance…