简体   繁体   中英

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"]

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM