简体   繁体   中英

How to store strings in external file for app-wide use (iOS)

I am creating an iOS app that, as part of its functionality, loads many web pages in a UIWebView.

For ease of editing and grouping, I would like to list each URL String in an external file, and then access that string within the app. For example, the string file could look like:

NSString *google = @"http://www.google.com";
NSString *stackOverflow = @"http://www.stackoverflow.com";
NSString *cnn = @"http://www.cnn.com";
NSString *facebook = @"https://www.facebook.com"

and in the core application code, I would tell the UIWebview to load the URL contained in stackOverflow . That way, if StackOverflow ever decided to change their URL (this is a hypothetical example), I could easily change the URL in the external strings file, and not bother with the core application code.

How would I do so? I know there is a "Strings file" in Xcode, but I think that is only used for language localization.

Even for loading non-localized string resources, you use the same infrastructure you do for localizing strings. So for iOS if you put a WebLinks.strings file at the top level of your application bundle it will be treated as the non-localized version of the strings resource. Whenever you use any of the system functions to retrieve a localized string if no appropriate localized string resource is found, the bundle's loading code automatically chooses the appropriate non-localized string instead. Thus for example you could setup WebLinks.strings like a standard .strings file in the top level of your application bundle with lines like:

/* Google's URL */
"googleURL" = "http://www.google.com";

You could use the following code to retrieve a string:

NSString *google = NSLocalizedStringFromTable(@"googleURL", @"WebLinks", @"Google's URL");

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