Warpling
Warpling

Reputation: 2095

What's the best way for an iOS app to retrieve its URL scheme?

If your app has different targets with corresponding different URL schemes (ex. blackbox://meta, blackbox-alpha://meta) you may find yourself wanting to dynamically look up the current app's scheme at run-time. How can you do that?

The following does NOT work:

[[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleURLSchemes"]

Upvotes: 3

Views: 281

Answers (1)

Warpling
Warpling

Reputation: 2095

The URL schemes array is actually stored under the URL Types array. Assuming you only have one URL type and the first listed scheme is the one you're after:

Objective-C

NSArray *urlTypes = [NSBundle.mainBundle objectForInfoDictionaryKey:@"CFBundleURLTypes"];
NSArray *urlSchemes = [urlTypes firstObject][@"CFBundleURLSchemes"];
NSString *urlScheme = [urlSchemes firstObject];

Swift

let urlTypes = NSBundle.mainBundle.object(forInfoDictionaryKey: "CFBundleURLTypes") as! [[String:Any]]
let urlSchemes = urlTypes.first?["CFBundleURLSchemes"]! as! [String]
let urlScheme2 = urlSchemes.first

Upvotes: 3

Related Questions