iOS开发:加载本地文件到UIWebView的方法
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];
评论暂时关闭