2014-10-04 2 views

답변

18

+mainBundle 메서드는 "현재 응용 프로그램 실행 파일"이 포함 된 번들을 반환합니다.이 번들은 확장 프로그램 내에서 호출 될 때 응용 프로그램의 하위 폴더입니다.

이 솔루션은 "appex"로 끝나면 번들의 URL에서 두 개의 디렉토리 레벨을 벗겨냅니다.

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

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

스위프트

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

스위프트 오브젝티브 C 이것은 파테 경우 중단합니다 xtension 또는 iOS 확장 프로그램의 디렉토리 구조가 변경됩니다.

+0

감사합니다. 이 방법으로 승인 된 AppStore 앱을 갖게 되었습니까? – ewindsor

+0

예, 여기에는 비공개 API가 사용되지 않습니다. 우리는 watchOS 1 익스텐션에서 이것을 사용하여 메인 iOS 앱 번들에서 'UIManagedDocument'에 대한 관리 객체 모델을로드했습니다. – phatblat

+0

아아. 통찰력에 감사드립니다. – ewindsor