One. Basic use
//
// ViewController.m
// 12-Master-Basic usage of UIWebView
//
// Created by xiaomage on 16/2/26.
// Copyright © 2016 小 码 哥. All rights reserved.
//
#import "ViewController.h"
@interface ViewController ()
@property (weak, nonatomic) IBOutlet UIWebView * webView;
@end
@implementation ViewController
-(void) viewDidLoad {
[super viewDidLoad];
[self test5];
}
-(void) test1
{
NSURL * url = [NSURL URLWithString: @ "http://www.baidu.com"];
// Load web page
[self.webView loadRequest: [NSURLRequest requestWithURL: url]];
}
-(void) test2
{
NSURL * url = [NSURL URLWithString: @ "http://www.baidu.com"];
// Load web page
[self.webView loadRequest: [NSURLRequest requestWithURL: url]];
self.webView.scrollView.contentInset = UIEdgeInsetsMake (40, 0, 0, 0);
}
// Load the local file
-(void) test3
{
NSURL * url = [NSURL fileURLWithPath: @ "/ Users / xiaomage / Desktop / 07-NSURLSession.pptx"];
// Load web page
[self.webView loadRequest: [NSURLRequest requestWithURL: url]];
}
-(void) test4
{
NSURL * url = [NSURL URLWithString: @ "http://www.520it.com/"];
// Load web page
[self.webView loadRequest: [NSURLRequest requestWithURL: url]];
// Adaptive when setting
self.webView.scalesPageToFit = YES;
}
-(void) test5
{
NSURL * url = [[NSBundle mainBundle] URLForResource: @ "text.html" withExtension: nil];
// Load web page
[self.webView loadRequest: [NSURLRequest requestWithURL: url]];
// Adaptive when setting
self.webView.dataDetectorTypes = UIDataDetectorTypeAll;
}
@end
Two. The agent
//
// ViewController.m
// 13-Master-UIWebView Application Case
//
// Created by xiaomage on 16/2/26.
// Copyright © 2016 小 码 哥. All rights reserved.
//
#import "ViewController.h"
@interface ViewController () <UIWebViewDelegate>
@property (weak, nonatomic) IBOutlet UIWebView * webView;
@property (weak, nonatomic) IBOutlet UIBarButtonItem * goBack;
@property (weak, nonatomic) IBOutlet UIBarButtonItem * goForward;
@end
@implementation ViewController
#pragma mark ----------------------
#pragma mark Life Cycle
-(void) viewDidLoad
{
[super viewDidLoad];
NSURL * url = [NSURL URLWithString: @ "http://www.baidu.com"];
NSURLRequest * request = [NSURLRequest requestWithURL: url];
// Load web page
[self.webView loadRequest: request];
// Set proxy
self.webView.delegate = self;
}
#pragma mark ----------------------
#pragma mark Events
-(IBAction) goBackBtnClick: (id) sender
{
[self.webView goBack];
}
-(IBAction) goForwardBtnClick: (id) sender
{
[self.webView goForward];
}
-(IBAction) reloadBtnClick: (id) sender
{
[self.webView reload];
}
#pragma mark ----------------------
#pragma mark UIWebViewDelegate
// Called when a request is about to be loaded
-(BOOL) webView: (UIWebView *) webView shouldStartLoadWithRequest: (NSURLRequest *) request navigationType: (UIWebViewNavigationType) navigationType
{
NSLog (@ "% @", request.URL.absoluteString);
// Simple request interception processing
NSString * strM = request.URL.absoluteString;
if ([strM containsString: @ "360"]) {
return NO;
}
return YES;
}
// 1. Called when starting to load a web page
-(void) webViewDidStartLoad: (UIWebView *) webView
{
NSLog (@ "webViewDidStartLoad");
}
// 2. Called when loading is complete
-(void) webViewDidFinishLoad: (UIWebView *) webView
{
NSLog (@ "webViewDidFinishLoad");
self.goBack.enabled = self.webView.canGoBack;
self.goForward.enabled = self.webView.canGoForward;
}
// 3. Called when loading fails
-(void) webView: (UIWebView *) webView didFailLoadWithError: (NSError *) error
{
NSLog (@ "didFailLoadWithError");
}
@end