ios UIWebView自訂Alert風格的彈框

來源:互聯網
上載者:User

標籤:erb   oid   rtp   relative   類別   沒有   自己   bsp   簡單   

之前開發過一個App,因為公司之前寫好了網頁版的內容和安卓版本的App,我進去後老闆要求我ios直接用網頁的內容,而不需要自己再搭建架構。我一聽,偷笑了,這不就是一個UIWebView嗎?簡單!

  但是,TMD(O^O)!事情並沒有這麼簡單,要求我要與網頁互動,首先就遇到了一個自訂網頁彈框的問題,思想:找到網頁彈框進行攔截,替換成自己寫的彈框。

一、建立UIWebView的類別UIWebView+JavaScriptAlert

  1、在.h中添加方法

-(void)webView:(UIWebView *)sender runJavaScriptAlertPanelWithMessage:(NSString *)message initiatedByFrame:(id)frame;

  2、在.m中建立彈框並更改標題

@implementation UIWebView (JavaScriptAlert)-(void)webView:(UIWebView *)sender runJavaScriptAlertPanelWithMessage:(NSString *)message initiatedByFrame:(id)frame{    UIAlertView * customAlert = [[UIAlertView alloc]initWithTitle:@"你想到更改的標題" message:message delegate:nil cancelButtonTitle:@"確定" otherButtonTitles:nil, nil];    [customAlert show];    }@end

二、在UIWebView的代理方法

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

中攔截

if ([request.mainDocumentURL.relativePath isEqualToString:@"/alert"]) {        [webView webView:webView runJavaScriptAlertPanelWithMessage:@"123" initiatedByFrame:nil];                return NO;    }

 

ios UIWebView自訂Alert風格的彈框

聯繫我們

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