Xcode UIWebView Local HTML

I know HTML, javascript, CSS ... but I wanted to create my own / hybrid iPhone app using HTML5 but not using something like PhoneGap or Nimblekit. I had never written a real application (not a web application) for the iPhone, so I don't 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]; 
+4
source share
3 answers

You have 2 options:

Paste HTML directly as follows:

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

Download the 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, replace the resulting code as follows:

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

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) } 
+1
source

you can load the local html file from the project package, as shown below:

 NSURLRequest *requestObj = [NSURLRequest requestWithURL:[[NSBundle mainBundle] URLForResource:@"index" withExtension:@"html"]]; [myweb loadRequest:requestObj]; 
0
source

All Articles