iphone - initWithContentsOfFile 已弃用

标签 iphone cocoa-touch webview

我想知道我可以使用什么代码来代替 initWithContentsOfFile: 因为我一直在寻找未弃用但找不到任何内容的东西。我正在尝试在 web View 中显示本地 HTML 文件,下面是代码:

- (void)viewDidLoad {
    [super viewDidLoad];

    NSString *htmlPath = [[NSBundle mainBundle] pathForResource:@"index" ofType:@"html"];
    NSURL *bundleUrl = [NSURL fileURLWithPath:[[NSBundle mainBundle] bundlePath]];
    NSString *html = [[NSString alloc] initWithContentsOfFile:htmlPath];
    [self.myWebView loadHTMLString:html baseURL:bundleUrl];

}

我收到一条警告,指出 initWithContentsOfFile: 已弃用,我想知道如何更新它。

最佳答案

试试这个:

NSError *error = nil;
NSString *string = [[NSString alloc] initWithContentsOfFile:@"/path/to/file.txt" encoding:NSUTF8StringEncoding error:&error];

关于iphone - initWithContentsOfFile 已弃用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8984308/

相关文章:

iphone - iOS:如何动态设置 UITableView 的高度

android - 在 Android WebView 中加载页面时如何使用后退按钮?

iphone - UITextField > 强制光标停留在字段末尾

cocoa - 无法将 xcode 依赖项更改为 iOS SDK 4.1

javascript - react-native-webview 调用时崩溃应用程序

Android PhoneGap 和菜单与 webview 崩溃

iphone - 确定今天的日期是否在 iOS 上的两个日期范围内

iphone - 增加图像缩放动画的持续时间(CGAffineTransformMakeScale)

ios - 从 html 链接重定向到 IOS 应用程序

iphone - 将数据从 View Controller 传递到另一个的断点