UIWebView相关

#import "ViewController.h"

@interface ViewController ()<UIWebViewDelegate>

@property (weak, nonatomic) IBOutlet UIWebView *webView;
@property (weak, nonatomic) IBOutlet UIBarButtonItem *goBackBtn;
@property (weak, nonatomic) IBOutlet UIBarButtonItem *goForwardBtn;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    //用UIWebView加载本地文件
    //NSURL *fileUrl = [NSURL fileURLWithPath:@"/Users/zhangfan/Downloads/Invoice_example-UnityAds Basic Invoice.pdf"];
    //NSURL *fileUrl = [NSURL fileURLWithPath:@"/Users/zhangfan/Downloads/IMG_4738.MP4"];
    //[self.webView loadRequest:[NSURLRequest requestWithURL:fileUrl]];

    //用UIWebView加载网址
    NSURL *webUrl = [NSURL URLWithString:@"http://www.baidu.com"];
    [self.webView loadRequest:[NSURLRequest requestWithURL:webUrl]];

    self.webView.scalesPageToFit = YES;//网页根据浏览器尺寸自动适应

    self.webView.delegate = self;
}

- (IBAction)goBack:(id)sender {
    [self.webView goBack];
}

- (IBAction)goForward:(id)sender {
    [self.webView goForward];
}

- (IBAction)refrush:(id)sender {
    [self.webView reload];
}

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
    NSLog(@"%@",request.URL.absoluteString);
    return YES;
}

- (void)webViewDidStartLoad:(UIWebView *)webView
{
    NSLog(@"%s",__func__);
}

- (void)webViewDidFinishLoad:(UIWebView *)webView
{
    NSLog(@"%s",__func__);

    [self.goBackBtn setEnabled:[self.webView canGoBack]];
    [self.goForwardBtn setEnabled:[self.webView canGoForward]];
}

- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
{
    NSLog(@"%s",__func__);
}

@end

results matching ""

    No results matching ""