从扩展
答
的+mainBundle
方法返回包含“当前应用程序可执行”,这是您的应用的子文件夹从分机内调用时的束。
该解决方案涉及剥离从束的URL 2个目录层次,当在“APPEX”结束。
目标C
NSBundle *bundle = [NSBundle mainBundle];
if ([[bundle.bundleURL pathExtension] isEqualToString:@"appex"]) {
// Peel off two directory levels - MY_APP.app/PlugIns/MY_APP_EXTENSION.appex
bundle = [NSBundle bundleWithURL:[[bundle.bundleURL URLByDeletingLastPathComponent] URLByDeletingLastPathComponent]];
}
NSString *appDisplayName = [bundle objectForInfoDictionaryKey:@"CFBundleDisplayName"];
夫特2.2
var bundle = NSBundle.mainBundle()
if bundle.bundleURL.pathExtension == "appex" {
// Peel off two directory levels - MY_APP.app/PlugIns/MY_APP_EXTENSION.appex
bundle = NSBundle(URL: bundle.bundleURL.URLByDeletingLastPathComponent!.URLByDeletingLastPathComponent!)!
}
let appDisplayName = bundle.objectForInfoDictionaryKey("CFBundleDisplayName")
夫特3
var bundle = Bundle.main
if bundle.bundleURL.pathExtension == "appex" {
// Peel off two directory levels - MY_APP.app/PlugIns/MY_APP_EXTENSION.appex
let url = bundle.bundleURL.deletingLastPathComponent().deletingLastPathComponent()
if let otherBundle = Bundle(url: url) {
bundle = otherBundle
}
}
let appDisplayName = bundle.object(forInfoDictionaryKey: "CFBundleDisplayName")
这将中断,如果百代iOS扩展的xtension或目录结构不断变化。
感谢这个!你是否能够通过这种方式获得AppStore应用程序? – ewindsor 2016-04-19 21:43:40
是的,这里没有使用私有API。我们用这个在我们watchOS 1扩展到从主iOS应用包中加载的管理对象模型的'UIManagedDocument'。 – phatblat 2016-04-21 16:54:23
啊真棒。感谢您的洞察力。 – ewindsor 2016-04-22 21:05:59