iOS webView loads local html, css, js files

Keywords: Attribute iOS Javascript network

1.h5 local html file, loaded on the web view, can also be loaded without network.
2.h5 Content Format Setting File Writing

Reprint: http://www.jianshu.com/p/c375ac056149

Article content
In IOS development, HTML files can be loaded through webView
The steps are as follows:
1. You need to have a webView. You can drag one or alloc one by storyboard (I dragged one here). Whether you want to set a delegate for webView or not depends on your needs (if only the display page can be ignored).

2. Create HTML files, CSS files, js files, the same way, but 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



3. Write our code in HTML file, css file and js file.
Write some elements into the HTML file

<!DOCTYPE html>
<html>
<head lang="zh">

    <meta charset="UTF-8">

    <title>First HTML</title>

    <link rel="stylesheet" type="text/css" href="index1.css">

    <script type="text/javascript" src="index1.js"></script>
</head>
<body>

    <h1>I am HTML</h1>

    <p id = "p">p Label</p>

    <img id = "img" src = "image.png" alt = "BaiduLOGO"><br/>

    <a id = "a" href="https://www.baidu.com">I want to go to Baidu</a>

    <br/><br/><br/>

    <button onclick = "hello()">Click on me to pop up hello</button>
</body>

</html>

Changing the attributes of elements in css files

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

    color:yellow;
}

Write a pop-up window function in the js file

function hello(){
    alert("hello");
}
//
//  ViewController.m
//  ios-loadHtml
//
//  Created by Xing Xinqing on 15-4-22.
//  Copyright (c) Xing Xianqing, 2015. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()<UIWebViewDelegate>
@property (weak, nonatomic) IBOutlet UIWebView *webView;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"Load HTML";
    [self loadHTMLFile];
}

//Load
-(void)loadHTMLFile{
    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];
}
#pragma mark UIWebViewDelegate
- (BOOL)webView:(UIWebView *)webView
shouldStartLoadWithRequest:(NSURLRequest *)request
 navigationType:(UIWebViewNavigationType)navigationType{

    NSURL* url = [request URL];
    NSString* urlstring = [NSString stringWithFormat:@"%@",url];
//    if ([urlstring isEqualToString:@"http://baidu.com/"]) {
//        return NO;
//    }
    NSLog(@"url = >%@",url);

    return YES;
}





@end

Project screenshots:

Posted by tinkertron on Sun, 10 Feb 2019 08:33:18 -0800