2014-10-04 20 views

cevap

18

+mainBundle yöntemi, bir uzantıdan çağrıldığında uygulamanızın bir alt klasörü olan "geçerli uygulama yürütülebilir" paketini içeren paketi döndürür.

Bu çözüm, "appex" ile bittiğinde, paketin URL'sinden iki dizin düzeyinin soyulmasını içerir.

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

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

Swift

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

Swift Objective-C Bu Pathe eğer kıracak Bir iOS uzantısı için xtension veya dizin yapısı hiç değişmez.

+0

Bunun için teşekkürler! Bu şekilde onaylanmış bir AppStore uygulaması edinebildiniz mi? – ewindsor

+0

Evet, burada özel API'ler kullanılmamaktadır. Bunu, ana iOS uygulama paketinden bir 'UIManagedDocument' için yönetilen nesne modelini yüklemek için watchOS 1 uzantımızda kullandık. – phatblat

+0

Ah harika. Anlayış için teşekkürler. – ewindsor

İlgili konular