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