UIWebView加载本地或在线网页--最简单使用

本文通过两个示例展示了如何在iOS应用中使用WebView加载本地和在线网页。首先创建了一个加载本地HTML文件的ViewController,并设置了一个按钮用于跳转到另一个加载在线网页的SecondViewController。文章提供了完整的Swift代码实现。

我只想直接上代码:

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:简单,不一定不需要记

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值