Cocoa WebView - Loading a local HTML page -
after browsing quite bit, still can't figure out. i've added html page , images directory project resources group in xcode (copied them over).
when try load webview following code, text displayed fine, images aren't loaded.
nsstring *path = [[[nsbundle mainbundle] resourcepath] stringbyappendingpathcomponent:@"index.html"]; nsstring *htmlcontent = [nsstring stringwithcontentsoffile:path]; nsurl *url = [nsurl fileurlwithpath:[[nsbundle mainbundle] bundlepath]]; [[tempwebview mainframe] loadhtmlstring:htmlcontent baseurl:url];
edit: sorry delay, here's basic html failed.
<html> <body> <img src="images/bg.png"></img> </body> </html>
and edited code looks -
nsstring *path = [[nsbundle mainbundle] pathforresource:@"index" oftype:@"html"]; nsurl *url = [nsurl fileurlwithpath:path]; [[webview1 mainframe] loadrequest:[nsurlrequest requestwithurl:url]];
edit2: realized issue path. apparently <img src = "images/bg.png">
doesn't work, if copy bg.png root directory , <img src = "bg.png">
works fine. i'm still not sure i'm going wrong this.
consider doing following:
- instead of getting resource path , stapling subpath onto yourself, ask bundle give correct path.
- instead of handling path @ all, ask bundle give correct url. (requires mac os x 10.6 or later.)
- instead of injecting html contents directly frame, ask webview load url.
this reduces code 2 lines that, if html code correct, work.
Comments
Post a Comment