简体   繁体   English

iOS应用程序检索其URL方案的最佳方式是什么?

[英]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. 如果您的应用程序具有不同的目标和相应的不同URL方案(例如blackbox://metablackbox-alpha://meta ),您可能会发现自己想要在运行时动态查找当前应用程序的方案。 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. URL方案数组实际存储在URL类型数组下。 Assuming you only have one URL type and the first listed scheme is the one you're after: 假设您只有一种URL类型,并且第一个列出的方案是您所追求的方案:

Objective-C 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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