************html
#import "HMViewController.h" @interface HMViewController () <UIWebViewDelegate> @property (nonatomic, weak) UIActivityIndicatorView *loadingView; @end @implementation HMViewController /** test.html存在於服務器,裏面的html和js代碼,咱們是沒法修改的 若是test.html顯示在手機端,把那個ul去掉 */ - (void)viewDidLoad { [super viewDidLoad]; // 1.webView UIWebView *webView = [[UIWebView alloc] init]; webView.frame = self.view.bounds; webView.delegate = self; // 伸縮頁面至填充整個webView webView.scalesPageToFit = YES; // 隱藏scrollView webView.scrollView.hidden = YES; [self.view addSubview:webView]; // 2.加載網頁 NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://m.dianping.com/tuan/deal/5501525"]]; [webView loadRequest:request]; // 3.建立圈圈 UIActivityIndicatorView *loadingView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge]; [loadingView startAnimating]; loadingView.center = CGPointMake(160, 240); [self.view addSubview:loadingView]; self.loadingView = loadingView; } // OC -> JS // 在OC中調用JS #pragma mark - UIWebViewDelegate - (void)webViewDidFinishLoad:(UIWebView *)webView { // 執行JS代碼,將大衆點評網頁裏面的多餘的節點刪掉 // var html = document.body.innerHTML; // NSString *html = [webView stringByEvaluatingJavaScriptFromString:@"document.body.innerHTML;"]; // NSLog(@"%@", html); NSMutableString *js1 = [NSMutableString string]; // 0.刪除頂部的導航條 [js1 appendString:@"var header = document.getElementsByTagName('header')[0];"]; [js1 appendString:@"header.parentNode.removeChild(header);"]; // 1.刪除底部的連接 [js1 appendString:@"var footer = document.getElementsByTagName('footer')[0];"]; [js1 appendString:@"footer.parentNode.removeChild(footer);"]; [webView stringByEvaluatingJavaScriptFromString:js1]; dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.25 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ NSMutableString *js2 = [NSMutableString string]; // 2.刪除浮動的廣告 [js2 appendString:@"var list = document.body.childNodes;"]; [js2 appendString:@"var len = list.length;"]; [js2 appendString:@"var banner = list[len - 1];"]; [js2 appendString:@"banner.parentNode.removeChild(banner);"]; [webView stringByEvaluatingJavaScriptFromString:js2]; // 顯示scrollView webView.scrollView.hidden = NO; // 刪除圈圈 [self.loadingView removeFromSuperview]; }); } @end