JS和OC互動的簡單應用

來源:互聯網
上載者:User

標籤:ble   school   btn   foo   strong   script   分享   思路   header   

先簡單地說一下思路吧

  • 需求:在App中嵌入一個webView,已經有了網址,但是需要去掉這個網頁中的一些內容
  • 解決方案:取到webView要載入的網址,對這個網址對應的網頁資訊進行修改
  • 簡單描述:
    1. 遵守webView的協議

      <UIWebViewDelegate>

    2. 設定代理
    3. 實現相應代理方法,對網頁資訊中要刪除的內容對應到節點,並刪除
      • 刪除的部分處理:擷取到相應的節點並執行刪除操作的語句寫法有三種方式:見http://www.w3school.com.cn/js/js_htmldom.asp

 

 1 #import "JSHanledWebViewController.h" 2  3 //1.遵守協議 4 @interface JSHanledWebViewController ()<UIWebViewDelegate> 5 @property (weak, nonatomic) IBOutlet UIWebView *webView; 6  7 @end 8  9 @implementation JSHanledWebViewController10 11 - (void)viewDidLoad {12     [super viewDidLoad];13     14     //2.設定代理15     self.webView.delegate = self;16     [self loadWebView];17     18 }19 20 #pragma mark - 載入webView21 - (void)loadWebView{22     23     [self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://m.dianping.com/tuan/deal/66666"]]];24     //關閉webView的彈簧效果25     self.webView.scrollView.bounces = NO;26     //隱藏捲軸27     self.webView.scrollView.showsVerticalScrollIndicator = NO;28 29 30 }31 32 //3.實現相應代理方法33 #pragma mark - Sent after a web view finishes loading a frame.34 - (void)webViewDidFinishLoad:(UIWebView *)webView{35     36     NSMutableString *jsStrM = [NSMutableString string];37     38     //參考w3c: http://www.w3school.com.cn/js/js_htmldom.asp39     //注意取出來的是個數組,注意指定下標40     //取到頂部的導覽列41     [jsStrM appendString:@"var header = document.getElementsByTagName(‘header‘)[0];"];42     [jsStrM appendString:@"header.parentNode.removeChild(header);"];43     44     //取到網頁輪播器下的 立即購買45     [jsStrM appendString:@"var costBox = document.getElementsByClassName(‘cost-box‘)[0];"];46     [jsStrM appendString:@"costBox.parentNode.removeChild(costBox);"];47     48     //取到灰色的立即購買49     [jsStrM appendString:@"var buy = document.getElementsByClassName(‘buy-now btn-gray‘)[0];"];50     [jsStrM appendString:@"buy.parentNode.removeChild(buy);"];51     52     //取到底部的首頁 點評團...53     [jsStrM appendString:@"var footer = document.getElementsByClassName(‘footer‘)[0];"];54     [jsStrM appendString:@"footer.parentNode.removeChild(footer);"];55     56     //取到黃色的立即購買按鈕57     [jsStrM appendString:@"var footerBtn = document.getElementsByClassName(‘footer-btn-fix‘)[0];"];58     [jsStrM appendString:@"footerBtn.parentNode.removeChild(footerBtn);"];59     60     //The stringByEvaluatingJavaScriptFromString: method waits synchronously for JavaScript evaluation to complete.61     [webView stringByEvaluatingJavaScriptFromString:jsStrM];62     63 64 }65 66 @end

 

  • 最終的效果如下所示 

如有問題,敬請指正!

JS和OC互動的簡單應用

聯繫我們

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