iOS中UIWebView網頁載入組件的基礎及提示執行個體_IOS

來源:互聯網
上載者:User

基本用法樣本

- (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);

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.