[iphone] iOS : 코드에서 app-info.plist 변수에 액세스

Universal 앱에서 작업 중이며 내 코드의 app-info.plist 파일에 저장된 값에 액세스하고 싶습니다.

이유 : 다음을 사용하여 스토리 보드에서 UIViewController를 동적으로 인스턴스화합니다.

UIStoryboard* storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard_iPhone" bundle:nil];
self = [storyboard instantiateViewControllerWithIdentifier:@"ExampleViewController"];

이제 위의 스토리 보드 이름이 @ “MainStoryboard_iPhone”인 것은 추악합니다.

다음과 같이하고 싶습니다.

UIStoryboard* storyboard = [UIStoryboard storyboardWithName:appInfo.mainStoryboardBaseNamePhone bundle:nil];
self = [storyboard instantiateViewControllerWithIdentifier:@"ExampleViewController"];

여기서 appInfo는 아마도 app-info.plist에있는 모든 값의 NSDictionary 일 수 있습니다.



답변

프로젝트에 대한 info.plist의 속성은 다음을 통해 직접 액세스 할 수 있습니다.

[[NSBundle mainBundle] objectForInfoDictionaryKey:key_name];

예를 들어 버전 번호를 얻으려면 다음을 수행하십시오.

NSString *appVersion = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleVersion"];

버전 번호가 이제 info.plist에 두 가지 속성을 가지고 있다는 점에 문제가 있습니다.하지만 아이디어를 얻었습니까? info.plist를 소스 코드로 보는 경우 (info.plist를 마우스 오른쪽 버튼으로 클릭-다른 이름으로 열기 선택) 사용할 수있는 모든 다양한 키 이름을 볼 수 있습니다.


답변

Damo 솔루션을 위한 Swift 4+ 구문

Bundle.main.object(forInfoDictionaryKey: "KEY_NAME")

let appVersion = Bundle.main.object(forInfoDictionaryKey: "CFBundleVersion")


답변

info.plist에 매우 쉽게 액세스 할 수 있습니다.

스토리 보드의 이름 얻기 :

NSString *storyboard  = [[NSBundle mainBundle].infoDictionary objectForKey:@"UIMainStoryboardFile"];

iPad에 있는지 감지 할 것인지 확실하지 않으며 UIMainStoryboardFile~ipadinstated 키를 사용해야합니다 .


답변

NSString *path = [[NSBundle mainBundle] pathForResource: @"YOURPLISTNAME" ofType: @"plist"]; 
NSMutableDictionary *dictplist =[[NSMutableDictionary alloc] initWithContentsOfFile:path];


답변

NSBundle에서 infoDictionary 메서드를 사용할 수도 있습니다.

NSDictionary *infoPlistDict = [[NSBundle mainBundle] infoDictionary];
NSString *version = infoPlistDict[@"CFBundleVersion"];


답변