我只想直接上代码:
ViewController.m:
//
// ViewController.m
// HybridAppDemoOne
//
// Created by mac on 17/1/8.
// Copyright © 2017年 cai. All rights reserved.
//
#import "ViewController.h"
#import "SecondViewController.h"
//本地网页
@interface ViewController ()<UIWebViewDelegate>
@property (nonatomic, strong) UIWebView *webView;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.view.backgroundColor = [UIColor cyanColor];
self.title = @"本地网页";
self.navigationController.navigationBar.translucent = NO;
self.navigationController.navigationBar.barTintColor = [UIColor orangeColor];
[self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor grayColor], NSFontAttributeName:[UIFont systemFontOfSize:17]}];
[self createWebView];
//本地网页
NSString *path = [[NSBundle mainBundle] pathForResource:@"HelloWorld.html" ofType:nil];
[self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:path]]];
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
btn.frame = CGRectMake(0, self.view.frame.size.height - 50 - 64, self.view.frame.size.width, 50);
btn.backgroundColor = [UIColor purpleColor];
[btn setTitle:@"push" forState:UIControlStateNormal];
[btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[self.view insertSubview:btn aboveSubview:self.webView];
[btn addTarget:self action:@selector(tapAction) forControlEvents:UIControlEventTouchUpInside];
}
- (void)tapAction
{
SecondViewController *secVC = [[SecondViewController alloc] init];
[self.navigationController pushViewController:secVC animated:YES];
}
#pragma mark -create webView
- (void)createWebView
{
if (_webView == nil) {
_webView = [[UIWebView alloc] initWithFrame:[UIScreen mainScreen].bounds];
_webView.backgroundColor = [UIColor orangeColor];
_webView.delegate = self;
_webView.scalesPageToFit = YES;
[self.view addSubview:_webView];
}
}
- (void)webViewDidStartLoad:(UIWebView *)webView
{
NSLog(@"--webViewDidStartLoad");
}
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
NSLog(@"--webViewDidFinishLoad");
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
复制代码
忽略其他的,仅下面两句为干货
NSString *path = [[NSBundle mainBundle] pathForResource:@"HelloWorld.html" ofType:nil];
[self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:path]]];
复制代码
__本地网页:__拖进工程
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Hello World!</title>
</head>
<body>
Hello World!
</body>
</html>
复制代码
SecondViewController.m:
//
// SecondViewController.m
// HybridAppDemoOne
//
// Created by mac on 17/1/8.
// Copyright © 2017年 cai. All rights reserved.
//
#import "SecondViewController.h"
//在线网页
@interface SecondViewController ()<UIWebViewDelegate>
@property (nonatomic, strong) UIWebView *webView;
@end
@implementation SecondViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.view.backgroundColor = [UIColor grayColor];
[self createWebView];
self.title = @"在线网页";
//在线网页
NSURL *url = [NSURL URLWithString:@"https://www.baidu.com"];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[self.webView loadRequest:request];
}
#pragma mark -create webView
- (void)createWebView
{
if (_webView == nil) {
_webView = [[UIWebView alloc] initWithFrame:[UIScreen mainScreen].bounds];
_webView.backgroundColor = [UIColor orangeColor];
_webView.delegate = self;
_webView.scalesPageToFit = YES;
[self.view addSubview:_webView];
}
}
- (void)webViewDidStartLoad:(UIWebView *)webView
{
NSLog(@"--webViewDidStartLoad");
}
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
NSLog(@"--webViewDidFinishLoad");
}
- (void)dealloc
{
NSLog(@"---dealloc: %@", NSStringFromClass([self class]));
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
复制代码
干货:
NSURL *url = [NSURL URLWithString:@"https://www.baidu.com"];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[self.webView loadRequest:request];
复制代码
PS:简单,不一定不需要记