Linux教程網
iOS開發:加載本地文件到UIWebView的方法
關鍵是生成“文件URL”
- NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUSErDomainMask, YES);
- NSString *documentsDirectory = [paths objectAtIndex:0];
- NSString *path = [documentsDirectory stringByAppendingPathComponent:docName];
- NSURL *url = [NSURL fileURLWithPath:path];
- NSURLRequest *request = [NSURLRequest requestWithURL:url];
-
- self.myWebView.scalesPageToFit = YES;
-
- [self.myWebView loadRequest:request];
如果是資源文件,則用獲取路徑
- NSString *mainBundleDirectory = [[NSBundle mainBundle] bundlePath];
- NSString *path = [mainBundleDirectory stringByAppendingPathComponent:docName];
Copyright ©
Linux教程網 All Rights Reserved