从扩展

问题描述:

中获取主要的应用程序包是否有可能从一个应用程序扩展名中包含应用程序的NSBundle得到什么?我想获取主应用的显示名称,而不是扩展名的显示名称。从扩展

+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或目录结构不断变化。

+0

感谢这个!你是否能够通过这种方式获得AppStore应用程序? – ewindsor 2016-04-19 21:43:40

+0

是的,这里没有使用私有API。我们用这个在我们watchOS 1扩展到从主iOS应用包中加载的管理对象模型的'UIManagedDocument'。 – phatblat 2016-04-21 16:54:23

+0

啊真棒。感谢您的洞察力。 – ewindsor 2016-04-22 21:05:59