iOS開發webView的使用二

來源:互聯網
上載者:User

標籤:

#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];        //載入網頁    [self.webView loadRequest:request];        //設定代理    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//即將載入某個請求的時候調用-(BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType{    NSLog(@"%@",request.URL.absoluteString);    //簡單的請求攔截處理    NSString *strM = request.URL.absoluteString;    if ([strM containsString:@"360"]) {        return NO;    }    return YES;}//1.開始載入網頁的時候調用-(void)webViewDidStartLoad:(UIWebView *)webView{    NSLog(@"webViewDidStartLoad");}//2.載入完成的時候調用-(void)webViewDidFinishLoad:(UIWebView *)webView{    NSLog(@"webViewDidFinishLoad");        self.goBack.enabled = self.webView.canGoBack;    self.goForward.enabled = self.webView.canGoForward;}//3.載入失敗的時候調用-(void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error{    NSLog(@"didFailLoadWithError");}@end

#####8 WebView的基本使用

 

```objc

1 概念性知識

    01 webView是有缺點的,會導致記憶體泄露,而且這個問題是它系統本身的問題。

    02 手機上面的safai其實就是用webView來實現的

    03 現在的開發並不完全是原生的開發,而更加傾向於原生+Html5的方式

    04 webView是OC代碼和html代碼之間進行互動的橋樑

 

2 代碼相關

/*A*網頁操控相關方法**/

    [self.webView goBack];      回退

    [self.webView goForward];   前進

    [self.webView reload];      重新整理

 

    //設定是否能夠前進和回退

    self.goBackBtn.enabled = webView.canGoBack;

    self.fowardBtn.enabled = webView.canGoForward;

 

/*B*常用的屬性設定**/

    self.webView.scalesPageToFit = YES; 設定網頁自動適應

    self.webView.dataDetectorTypes = UIDataDetectorTypeAll; 設定檢測網頁中的格式類型,all表示檢測所有類型包括超連結、電話號碼、地址等。

    self.webView.scrollView.contentInset = UIEdgeInsetsMake(50, 0, 0, 0);

 

/*C*相關代理方法**/

    //每當將載入請求的時候調用該方法,返回YES 表示載入該請求,返回NO 表示不載入該請求

    //可以在該方法中攔截請求

    -(BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType

    {

        return ![request.URL.absoluteString containsString:@"dushu"];

    }

 

    //開始載入網頁,不僅監聽我們指定的請求,還會監聽內部發送的請求

    -(void)webViewDidStartLoad:(UIWebView *)webView

 

    //網頁載入完畢之後會調用該方法

    -(void)webViewDidFinishLoad:(UIWebView *)webView

 

    //網頁載入失敗調用該方法

    -(void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error

 

/*D*其它知識點-載入本地資源**/

    NSURL *url = [[NSBundle mainBundle] URLForResource:@"text.html" withExtension:nil];

    [self.webView loadRequest:[NSURLRequest requestWithURL:url]];

```

 

#####9 HTML

```objc

1.Html決定網頁的內容,css決定網頁的樣式,js決定網頁的事件

2.html學習網站:http://www.w3school.com.cn

 

iOS開發webView的使用二

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.