Xcode UIWebView local HTML

19,040

Solution 1

You have 2 options:

Insert HTML directly like this:

UIWebView *wv = [[UIWebView alloc] init]; 
[wv loadHTMLString:@"<html><body>YOUR-TEXT-HERE</body></html>" baseURL:nil];

Load an html file that exists in your project:

UIWebView *wv = [[UIWebView alloc] init];
NSURL *htmlFile = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"test" ofType:@"html"] isDirectory:NO];
[wv loadRequest:[NSURLRequest requestWithURL:htmlFile]]; 

If you want your example to work, then replace the code you sent with this:

[super viewDidLoad];
NSURL *htmlFile = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"index" ofType:@"html"]];
[_viewWeb loadRequest:[NSURLRequest requestWithURL:htmlFile]];

Solution 2

For Swift 3:

@IBOutlet var webView: UIWebView!
override func viewDidLoad() {
super.viewDidLoad()
let URL = Bundle.main.url(forResource: "file name", withExtension: "html")
let request = NSURLRequest(url: URL! as URL)
webView.loadRequest(request as URLRequest)
}
Share:
19,040
eds1999
Author by

eds1999

I'm a 13 years old developer. I create sites, iOS apps, jailbreak tweaks and themes …! Made iCard Background HD. Follow me on Twitter @eds1999 :)

Updated on June 04, 2022

Comments

  • eds1999
    eds1999 almost 2 years

    I know HTML, javascript, CSS… but I wanted to make a native/hybrid iPhone app using HTML5 but without using something like PhoneGap or Nimblekit. I never wrote a real (not web app) iPhone app before, so i don't really know anything about Xcode. I already made a UIWebView with a tutorial that i found, but it displays a website (apple.com). How can i make this display a local file (index.html)?

    My code in ViewController.m under (void)viewDidLoad:

        [super viewDidLoad];
        NSString *fullURL = @"html/index.html";
        NSURL *url = [NSURL URLWithString:fullURL];
        NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
        [_viewWeb loadRequest:requestObj];