简体   繁体   中英

What is the best way to store strings in iPhone application?

I'm wondering what is the best way to store long strings in iPhone? eg I have 'about' page in my app which is basically an html page, and I load it to UIWebView.

Is there any better way so store it apart from store it as string in code? may be in resource file?

Thank you

You can add the html file directly as a resource in your bundle. Then when you want to use it you can use the following code

NSString* fileName = [[NSBundle mainBundle] fileForResource:@"myHtmlFile" withExtension:@"html"];
NSString* fileContents = [NSString stringWithContentsOfFile:fileName encoding:NSUTF8Encoding error:nil];
[webView loadHTMLString:fileContents baseURL:baseURL];

You can use a plist that is very flexible and fast to load. If you plan to localize this text into different language you should use String File (both are available under "Resource" when you choose to create a new file in xcode)

我自己还没有完成此操作,但是我猜测您可以简单地将HTML文件随包一起部署(只需将其作为资源添加到您的项目中),然后将UIWebView指向它即可。

If the string will never change throughout the life of the application, you should use a resource packaged in your application bundle.

As the previous posts alluded to, you have several options. You can make a plist, an HTML file, or any other kind of file resource.

A nice benefit of these options is that you have the ability to make localized versions.

If the string will change through use of the application, you may also consider storing the value of the string in a local database with SQLite and CoreData.

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