The use and proxy of ios-network-uiwebview

Source: Internet
Author: User
Tags reserved


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 

Related Article

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.