UIWebView provides 3 ways to load pages:
- (void)loadRequest: (NSURLRequest *)request;
- (void)loaDHTMLString: (NSString *)string baseURL: (NSURL *)baseURL;
- ( void)loadData:(NSData *)data MIMEType:(NSString *)MIMEType textEncodingName:(NSString *)textEncodingName baseURL:(NSURL *)baseURL;
Here I will only talk about the first two, the last one should not be commonly used.
- (void)loadRequest:(NSURLRequest *)request
This method is often used to load web pages at the specified url, but in fact it can also be used to load local resources, and it is very convenient.
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"index" ofType:@"html" inDirectory:@"Htmls"];
[self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:filePath]] ];
This loads Htmls/index.html into the webview. It should be noted that the Htmls folder is introduced by "create folder references for any added folders" instead of by default. There are two advantages to doing this. First, after compilation, the directory structure of the resources will be consistent with the current project, and will not be scattered in the .app package. Therefore, the resource files can be easily found by HTML; second, the files in the finder Directory changes will be directly mapped to the project, without the need to manually add or delete files in Xcode.
In index.html, the front-end engineer quoted the style sheet
The path of the base.CSS file is actually xxx.app/Htmls/css/base.css, not xxx.app/css/base.css. It It can be found because the loadReqest method uses the path of the currently loaded html file as the baseURL.
- (void)loadHTMLString:(NSString *)string baseURL:(NSURL *)baseURL
This method is used to load html code directly. This method is recommended if the html does not exist in the file. Of course, you can also use this method to read the code from local html and then load it. But please note that the baseURL must be passed correctly at this time, otherwise the resources referenced in the html will not be found.
Continuing to use the above example, to load resources correctly, you have to write:
NSString *baseURL = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"Htmls"];
[self.webView loadHTMLString:htmlString baseURL :[NSURL fileURLWithPath:baseURL]];
In this way, the front-end engineer can become accustomed to working with you to develop~
The above is the content of how Html loads web pages. For more related articles, please pay attention to the PHP Chinese website (www.php .cn)!