简体   繁体   中英

NSBundle mainBundle returning nil

I'm trying to change server settings based on build configurations in Xcode for our iOS app. I am using this guide as a base. I set up the configuration property in the Info.plist but when I try to get it in code it is coming back nil .

info.plist条目

In searching for solutions and testing I found that I'm getting back nil from everything to do with the mainBundle . Here is what I've declared in my testing so far. What am I missing/doing wrong?

NSBundle *bundle = [NSBundle mainBundle];
NSString *configuration = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"Configuration"];
NSString *bundleName = [[NSBundle mainBundle] bundleIdentifier];
NSString *otherName = [[[NSBundle mainBundle] infoDictionary] objectForKey:(NSString *)kCFBundleNameKey];

I'm using Xcode 9.3.

In some instances, for example when your code is executed directly from within Interface Builder, mainBundle does not return an object. So, depending on your context you might want to use bundleForClass: instead. Specify a class that resides within the bundle you want.

NSBundle *bundle = [NSBundle bundleForClass:[YourClass class]];

Turns out I had duplicated my debug configuration from the release configuration. I noticed that some of the debug properties were not the same. So I deleted and re created the new configurations. This solved the issue. If anyone has any specifics on why the bundle information was not being found in a release build, please comment.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM