1. 程式人生 > >關於iOS專案中引入Html5檔案(html/js/css)的路徑問題

關於iOS專案中引入Html5檔案(html/js/css)的路徑問題

iOS專案中使用專案中的html js css 檔案時,有時會遇到引用路徑出錯的問題,導致html js css image檔案無法載入的情況。

那麼,引入H5相關檔案的正確操作方式如下,這樣就不會出現資原始檔無法引入並正常使用的情況:

1 在專案目錄下建立存放Html5檔案資料夾:H5


2 將資料夾H5拖放至專案中對應的位置,此處注意要選擇:Create groups


3 將各種Html5檔案(html js css image等)拖放到H5資料夾中,此處注意選擇:Create folder references


PS:黃色為groups , 藍色為folder


4 UIWebView/WKWebView中的程式碼如下:

//建立要開啟的html檔案的完整路徑
    NSBundle *bundle = [NSBundle mainBundle];
    NSString *resPath = [bundle resourcePath];
    NSString *filePath = [resPath stringByAppendingPathComponent:@"index.html"];
    //初始化一個UIWebView例項
    myWebView = [[UIWebView alloc] initWithFrame:self.view.frame];
    //載入指定的html檔案
    [myWebView loadRequest:[[NSURLRequest alloc] initWithURL:[[NSURL alloc] initFileURLWithPath:filePath]]];
    [self.view addSubview:myWebView];
此時已經成功添加了Html5檔案到專案中,並在UIWebView中展示出來了。

相關內容可以參考:http://blog.csdn.net/fanjunxi1990/article/details/9352917