你好,歡迎來到IOS教程網

 Ios教程網 >> IOS編程開發 >> IOS開發綜合 >> iOS中UIWebView網頁加載組件的基本及應用技能實例

iOS中UIWebView網頁加載組件的基本及應用技能實例

編輯:IOS開發綜合

根本用法示例

- (void)viewDidLoad
{
  [super viewDidLoad];
  // Do any additional setup after loading the view.

  UIWebView * webView = [[UIWebView alloc]initWithFrame:CGRectMake(0, 20, ScreenWidth, ScreenHeight-20)];
  // 主動隊頁面停止縮放以順應屏幕
  webView.scalesPageToFit = YES;
  webView.userInteractionEnabled = YES;
  webView.opaque = YES;

  [self.view addSubview:webView];

  NSURL * url = [NSURL URLWithString:@"http://www.youku.com"];
  NSURLRequest * request = [NSURLRequest requestWithURL:url];
  [webView loadRequest:request];
//  NSString * myHT = @"優酷";
//  [webView loadHTMLString:myHT baseURL:url];
/*
  [webView goBack];    // 前往
  [webView goForward];   // 前去
  [webView reload];
  [webView stopLoading];
 */
  webView.delegate = self;

  //移除轉動後的外邊暗影
  UIScrollView *scrollView = webView.scrollView;
  for (int i = 0; i < scrollView.subviews.count ; i++) {
    UIView *view = [scrollView.subviews objectAtIndex:i];
    if ([view isKindOfClass:[UIImageView class]]) {
      view.hidden = YES ;
    }
  }
}

#pragma mark - UIWebViewDelegate
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType{

  /**
   * typedef NS_ENUM(NSInteger, UIWebViewNavigationType) {
   * UIWebViewNavigationTypeLinkClicked,
   * UIWebViewNavigationTypeFormSubmitted,
   * UIWebViewNavigationTypeBackForward,
   * UIWebViewNavigationTypeReload,
   * UIWebViewNavigationTypeFormResubmitted,
   * UIWebViewNavigationTypeOther
   };
   */

  NSLOG_FUNCTION;

  return YES;
}

// 開端加載
- (void)webViewDidStartLoad:(UIWebView *)webView{

  NSLOG_FUNCTION;

}

// 完成加載
- (void)webViewDidFinishLoad:(UIWebView *)webView{

  NSLOG_FUNCTION;

}

// 加載掉敗,彈失足誤提醒
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error{

  UIAlertView *alterview = [[UIAlertView alloc] initWithTitle:@"" message:[error localizedDescription]
                            delegate:nil cancelButtonTitle:nil otherButtonTitles:@"OK", nil];
  [alterview show];
  [alterview release];
  NSLOG_FUNCTION;

}


以下是關於它的一些應用技能:

1.讓網頁順應手機屏幕寬度

假如用UIWebView顯示一些pc站的網頁,會發明網頁會超越屏幕,顯得很欠好看,這時候可以在webViewDidFinishLoad這個署理外面經由過程js添加一個meta:

- (void)webViewDidFinishLoad:(UIWebView *)webView

{

  NSString *meta = [NSString stringWithFormat:@"document.getElementsByName(\"viewport\")[0].content = \"width=%f, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no\"", IPHONE_WIDTH];

  [webView stringByEvaLuatingJavaScriptFromString:meta];

}

留意:應用這個辦法時要把UIWebView的scalesPageToFit設成NO

webView.scalesPageToFit = NO;

2.為網頁中的圖片添加點擊事宜,當點擊圖片時縮小檢查

思緒是給每個img標簽添加onclick事宜,在事宜中把img的src屬性封裝成一個特別的url,然落後行攔阻

假如是經由過程loadHTMLString去加載網頁的話,可以履行上面一句停止調換:

    html = [html stringByReplacingOccurrencesOfString:@"<img " withString:@"<img onclick=\"Window.location.href=('http://src.'+this.src);\" "];

假如是經由過程loadRequest,那就要再webViewDidFinishLoad中履行以下JS:

  NSString *js = @"var imgs = document.getElementsByTagName(\"img\");"

  "for(var i=0;i<imgs.length;i++){"

  "  var img = imgs[i];"

  "  img.onclick=function(){Window.location.href=('http://src.'+this.src);}"

  "}";

  [webView stringByEvaLuatingJavaScriptFromString:js];

然後經由過程webview的署理辦法去攔阻,拿到圖片的url,以後便可以做各類處置了

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

{

  NSString *url = request.URL.absoluteString;

  if ([url hasprefix:@"http://src."])

  {

    url = [url stringByReplacingOccurrencesOfString:@"http://src." withString:@""];

    // Do something..

    return NO;

  }

  return YES;

}

3.為UIWebView添加一個追隨網頁轉動的頁頭

UIWebView裡包括一個scrollview,可以向scrollview裡添加一個頁頭以到達追隨網頁轉動的後果

CGFloat headerHeight = 36.0f;

// 留意:y坐標必需是正數,IPHONE_WIDTH是屏幕寬度

UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, -headerHeight, IPHONE_WIDTH, headerHeight)];

[_webView.scrollView addSubview:_headerView];

// 修正webView的scrollView的contentInset,讓頂部留出一點空間

UIEdgeInsets edgeInset = _webView.scrollView.contentInset;

_webView.scrollView.contentInset = UIEdgeInsetsMake(headerView.frameHeight, edgeInset.left, edgeInset.bottom, edgeInset.right);

【iOS中UIWebView網頁加載組件的基本及應用技能實例】的相關資料介紹到這裡,希望對您有所幫助! 提示:不會對讀者因本文所帶來的任何損失負責。如果您支持就請把本站添加至收藏夾哦!

  1. 上一頁:
  2. 下一頁:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © Ios教程網 All Rights Reserved