2015-11-06 51 views
0

嗨我想從我的項目中加載html和.js文件。這些是下面的步驟,我通過框架加載HTML文件,但我無法擊中HTML文件。從框架加載html文件可能嗎?

接下來的步驟。

創建一個框架,其中webview作爲子視圖。

創建Resource.bundle並添加了我的所有html,.js文件和圖像。

當我將這個框架添加到我的項目中,並嘗試從包中訪問圖像我無法。 當我嘗試擊中html文件時,我無法在我的webview中放置html文件。

NSString *bundlePath = [[NSBundle mainBundle] pathForResource:@"Resource" ofType:@"bundle"]; 
    NSString *htmlFile = [[NSBundle bundleWithPath:bundlePath] pathForResource:@"simple" ofType:@"html"]; 

    NSString* htmlString = [NSString stringWithContentsOfFile:htmlFile encoding:NSUTF8StringEncoding error:nil]; 
    NSURL *url=[[NSBundle mainBundle] bundleURL]; 
    [webview loadHTMLString:htmlString baseURL:url]; 
    [self addSubview:webview]; 

有誰能告訴我爲什麼這不會工作,這是正確的方法?

+0

嘗試此[web視圖的loadRequest:的NSURLRequest requestWithURL:[NSURL fileURLWithPath:[[一個NSBundle mainBundle] pathForResource:@ 「測試」 ofType:@ 「HTML」] isDirectory:NO]]]; – Spynet

+0

您是否確認資源確實在捆綁中?檢查您的構建產品並使用Finder打開該包。 –

+0

@Joris Kluivers是資源在捆綁... – Abhilash

回答

0

我創建調用一個js用一個簡單的警報的index.html束,與此代碼我能夠瑟警報

NSString *bundlePath = [[NSBundle mainBundle] pathForResource:@"BundleName" ofType:@"bundle"]; 
NSString *htmlFile = [[NSBundle bundleWithPath:bundlePath] pathForResource:@"index" ofType:@"html"]; 
NSString* htmlString = [NSString stringWithContentsOfFile:htmlFile encoding:NSUTF8StringEncoding error:nil]; 

[_web loadHTMLString:htmlString baseURL:[[NSBundle mainBundle] bundleURL]]; 

在本地的HTML工作? webview被初始化?

+0

是的HTML工程在本地和webview被初始化... – Abhilash

+0

是的HTML工程在本地和webview被初始化...我試着上面的代碼,它不工作。 .. – Abhilash

相關問題