iOS开发:加载本地文件到UIWebView的方法


关键是生成“文件URL”

  1. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUSErDomainMask, YES);  
  2.     NSString *documentsDirectory = [paths objectAtIndex:0];      
  3.     NSString *path = [documentsDirectory stringByAppendingPathComponent:docName];      
  4.     NSURL *url = [NSURL fileURLWithPath:path];  
  5.     NSURLRequest *request = [NSURLRequest requestWithURL:url];  
  6.    
  7.     self.myWebView.scalesPageToFit = YES;  
  8.    
  9.     [self.myWebView loadRequest:request];  

如果是资源文件,则用获取路径  

  1. NSString *mainBundleDirectory = [[NSBundle mainBundle] bundlePath];  
  2. NSString *path = [mainBundleDirectory   stringByAppendingPathComponent:docName];  

相关内容