IOS Load Local Html,css,js

Source: Internet
Author: User

In iOS development, HTML files can be loaded via WebView

The steps are as follows:

1. Need to have a webview, can be dragged through the storyboard one or alloc (I am here to drag a), whether to give WebView set delegate, according to their own needs to decide (if only the display page can be ignored).

WebView

2. Create HTML files, CSS files, JS files , the same way, only the suffix name is different.
New File, other->empty
Create HTML file suffix named: HTML, create css file suffix named CSS, create js file suffix named: JS

Create HTML files, CSS files, JS files

This is what it looks like after creation.


Create complete

3. Write our code in HTML file, CSS file, js file.

Write some elements in the HTML file

<!DOCTYPE html>

Changing the attributes of an element in a CSS file

#p{ color:red;}#img{ width:120px; height:50px;}#a{ color:yellow;}

A function to write a popup window in the JS file

function hello(){ alert("hello");}

So our three files are all written, can be loaded by WebView.

4. Load these three files via WebView
Write the code in Viewcontroller's Viewdidload method

NSString *path = [[NSBundle mainBundle] bundlePath];NSURL *baseURL = [NSURL fileURLWithPath:path];NSString * htmlPath = [[NSBundle mainBundle] pathForResource:@"index1" ofType:@"html"];NSString * htmlCont = [NSString stringWithContentsOfFile:htmlPath encoding:NSUTF8StringEncoding error:nil];[self.webView loadHTMLString:htmlCont baseURL:baseURL];

After you finish writing, you can see the effect command+r run! (I have a navgationcontroller here, if you do not add, there is no navigation bar)


So the HTML file is loaded, the style of the page element is also defined by the CSS file, then we click on the pageClick I pop up Hellobutton, you can pop up a hello pop-up box,
Popup Hello pop-up box
Now our html,css,js three files have been verified by the ~

5. Capturing HTML Interactions
If we have an interaction in the HTML page, we can get the link to the operation via WebView delegate (the delegate of the WebView is not set in the first step, now we need to set the ~)
Compliance with Uiwebviewdelegate Protocol
Implement this method in uiwebviewdelegate in Viewcontroller
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType

Look at the implementation code:
-(BOOL) WebView: (UIWebView ) WebView
Shouldstartloadwithrequest: (nsurlrequest ) request
Navigationtype: (Uiwebviewnavigationtype) navigationtype{
Nsurl
url = [request URL];
NSString
urlstring = [NSString stringwithformat:@ "%@", url];
NSLog (@ "url = >%@", url);
return YES;
}
OK, run, click on the page I want to go to Baidu this hyperlink, see if the link is lost out

Output link Address

So you can do something according to your needs ....

Attached Source Address: Https://github.com/xingxianqing/loadHtmlCssJsDemo

Tired of, entertainment ...

The Divine Question together



Brothel
Links: http://www.jianshu.com/p/c375ac056149
Source: Pinterest
Copyright belongs to the author. Commercial reprint please contact the author for authorization, non-commercial reprint please specify the source.

IOS Load Local Html,css,js

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.