how to get INFOPLIST_FILE name iOS - ios

Each target in my project has to has different info.plist. I want to get the name programmatically from BuildSettings> Packaging> Info.plist file but somehow I cant really retrieve the plist list filename. Is there a way i can retrieve the plist programmatically?
NSBundle.mainBundle().objectForInfoDictionaryKey("CFBundleInfoPlistURL")
only gives me
Info.plist -- file:///Users/XXXX/Library/Developer/CoreSimulator/Devices/XXXXXX-9B37-4C3D-88E9-XXXXX62B470/data/Containers/Bundle/Application/7XXXXXXXB-BE36-4D3E-9C90-XXXXX/PROJECT%20KOL.app/

Actually NSBundle.mainBundle().objectForInfoDictionaryKey("CFBundleInfoPlistURL") returns an NSURL object. Extract the absoluteString from it which will give you the name of your plist file. Check the code below. Sorry for giving answer in Objective-C as I don't know Swift
NSDictionary *infoDict = [NSBundle mainBundle].infoDictionary;
NSString *plistFilePath = [[infoDict objectForKey:#"CFBundleInfoPlistURL"] absoluteString];
NSString *plistFileName = [[plistFilePath componentsSeparatedByString:#"/"] lastObject];
plistFileName is the required file name (with extension .plist)

Related

Can i define User Defined key at info.plist

Can i defined a user defined key with given name "SomeKey" and can i access with [[NSBundle mainBundle] objectForInfoDictionaryKey:#"SomeKey"]
Of course!
Apple Docs:
Custom Keys
iOS and macOS ignore custom keys you include in an Info.plist file. If
you want to include app-specific configuration information in your
Info.plist file, you can do so freely as long as your key names do not
conflict with the ones Apple uses. When defining custom key names,
prefix them with a unique prefix, such as your app’s bundle ID or your
company’s domain name, to prevent conflicts.
And you can get the value by doing something like this:
Objective-C
[[NSBundle mainBundle].infoDictionary objectForKey:#"SomeKey"];
or
[[NSBundle mainBundle] objectForInfoDictionaryKey:#"SomeKey"];
Swift
Bundle.main.infoDictionary?["SomeKey"]
Reference:
Info Plist Docs
Reserved Keys
We do access the application bundle info.plist like shown below,
Objective-C
NSDictionary *infoDictionary = [[NSBundle mainBundle] infoDictionary];
NSString *appDisplayName = [infoDictionary objectForKey:#"CFBundleDisplayName"];
Swift
let appDisplayName = Bundle.main.infoDictionary?["CFBundleDisplayName"]
Similarly, If you have added any custom key-value pair in the info.plist you can surely access it. Just replace the CFBundleDisplayName with your key.
Refere this
Short Answer: Yes.
Read: Custom Keys in Apple Docs

how to change ios APP font in Hindi or tamil

enter image description hereIn iOS, can we change App localization in hindi?
I tried following examples but its only support info.plist values
https://code.tutsplus.com/tutorials/ios-sdk-localization-with-nslocalizedstring--mobile-11603
Yes, we can have any language in app.
You need to have language selection inside the app instead of device's setting language.
Define all keywords in localization bundle.
NSString *selected_lng_code = #"hi"
NSString *path = [[NSBundle mainBundle] pathForResource:selected_lng_code ofType:#"lproj" ];
NSBundle *localizedBundle = [NSBundle bundleWithPath:path];
NSString *translated_word = [localizedBundle localizedStringForKey:#"Hello" value:#"" table:nil];
You have to create on InfoPlist.strings file for localizing Info.plist value. To do so, go to File -> New -> File, choose Strings File under Resource tab of iOS, name it InfoPlist, and create it. Now open that file and add Info.plist values you want to localize like:
"CFBundleDisplayName" = "<app name>". This will be your Base file.
For your desired language, select your localized files and add translated strings.

Objective-C Access Specific Item in Plist

I have created an app, and I am trying to access a URL that is stored in a plist file. At this state I am just trying to log the contents out. I am aware similar questions have been asked before, but I am asking specifically to my scenario how to I access Item 0. I am trying to access Item 0 inside InternalViaSafari this manifests itself inside the URLValidator and then that inside the Root. The code I have so far is:
NSString* filePath = [[NSBundle mainBundle] pathForResource:#"plist-file-name" ofType:#"plist"];
NSDictionary* plist = [NSDictionary dictionaryWithContentsOfFile:filePath];
NSString* name = [plist valueForKeyPath:#"URLValidator.InternalViaSafari"];
NSLog(name);
You cannot use keyPath like that for this as far as I know. InternalViaSafari is not a property of URLValidator dictionary. What's more InternalViaSafari is an Array not a String in your plist.
In order to get this string you'd need something like this :
NSArray *internalViaSafari = plist[#"URLValidator"][#"InternalViaSafari"];
NSString *name = internalViaSafari.firstObject;
What happens here, is that you get the value under the URLValidator key from your plist dictionary. This value is also a Dictionary (this can be clearly seen in the plist screenshot you shared), so you get the value under the InternalViaSafari key. This value is in turn an Array, which has Strings as its elements. In this example I extracted the first entry from this array.

How to change string in .plist

I need that a string in a .plist file could be changed within the app with a "save" button. My plist is a simple array of strings, I need that when the user taps "Save" the string could be modified. Sorry for my bad english. Thank you
You need to first extract your plist data and then you have to set the values and then again you have to write like that below:-
NSString *docPath =#"your Path";
NNSMutableDictionary* updateVal=[[NSMutableDictionary alloc]initWithContentsOfFile:docPath];
[updateVal setObject:#"yourString" forKey:#"key"];
[updateVal writeToFile:docPath atomically:YES];
Note:- As rmaddy mentioned in the comment that docPath should not be in the app's resource bundle.

Define ENUM values in a .plist in Xcode

I use a configuration.plist file to configure certain parameters in my application and initialise few classes based on the contents of this plist file.
However I want to expose to the developer a list of options that can be selected as below(per say),
I can this kind of option available in application info.plist file but I don't get to see anywhere else on how I can achieve this.
I'm looking at getting a drop down list showing the list of available options, Possibly an ENUM list.
Appreciate any assistance.
You can read from the .plist file:
NSDictionary* infoDictionary = [[NSBundle mainBundle] infoDictionary];
NSString* region = [infoDictionary objectForKey:(__bridge id) kCFBundleDevelopmentRegionKey];

Resources