ios UIWebView自定義Alert風格的彈框


之前開發過一個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;
    }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM