iOS項目中使用項目中的html js css 文件時,有時會遇到引用路徑出錯的問題,致使html js css image文件沒法加載的狀況。css
那麼,引入H5相關文件的正確操做方式以下,這樣就不會出現資源文件沒法引入並正常使用的狀況:html
1 在項目目錄下建立存放Html5文件文件夾:H5spa
2 將文件夾H5拖放至項目中對應的位置,此處注意要選擇:Create groups.net
3 將各類Html5文件(html js css image等)拖放到H5文件夾中,此處注意選擇:Create folder referencescode
PS:黃色爲groups , 藍色爲folderhtm
4 UIWebView/WKWebView中的代碼以下:blog
1 //建立要打開的html文件的完整路徑 2 NSBundle *bundle = [NSBundle mainBundle]; 3 NSString *resPath = [bundle resourcePath]; 4 NSString *filePath = [resPath stringByAppendingPathComponent:@"index.html"]; 5 //初始化一個UIWebView實例 6 myWebView = [[UIWebView alloc] initWithFrame:self.view.frame]; 7 //加載指定的html文件 8 [myWebView loadRequest:[[NSURLRequest alloc] initWithURL:[[NSURL alloc] initFileURLWithPath:filePath]]]; 9 [self.view addSubview:myWebView];
此時已經成功添加了Html5文件到項目中,並在UIWebView中展現出來了。資源
相關內容能夠參考:http://blog.csdn.net/fanjunxi1990/article/details/9352917string