解决js的confirm转换为iOS UIAlertView的title问题

本文介绍如何在iOS中使用UIWebView重载JavaScript确认框方法,实现自定义样式,并通过监听线程来获取用户的选择。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

使用cordova做跨平台开发的时候,碰到了js调用confirm转换成iOS的UIAlertView的title显示成xxxx.html的问题。

我想到的第一种办法就是监听UIApplication的windows,然后手动修改当前alert的title。以前做过类似的监听系统通知,修改样式的。不过这样跟JS交互起来效率就低了好多。

另一种思路就是如果能够找到confirm转化成alert的方法不是更好?这样就可以重载该方法替换成任意的样式了。

在stackoverflow上还真找到了,哇哈哈。

@interface UIWebView (JavaScriptAlert)

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

- (BOOL)webView:(UIWebView *)sender runJavaScriptConfirmPanelWithMessage:(NSString *)message initiatedByFrame:(id)frame;

- (NSString *) webView:(UIWebView *)view runJavaScriptTextInputPanelWithPrompt:(NSString *)prompt defaultText:(NSString *)text initiatedByFrame:(id)frame;

@end


之后我又在csdn上搜了下找到了别人的实现,最终确定是在这个

- (BOOL)webView:(UIWebView *)sender runJavaScriptConfirmPanelWithMessage:(NSString *)message initiatedByFrame:(id)frame {


里边修改alert的样式,研究了下,这个方法需要返回当前选中的alert的index,这就意味着必须在点击alert之前必须挂起当前线程。这就用到运行循环 runloop了。

网上找到的方法基本都过时了,我稍微修改了下:

#import "UIWebView+JavaScriptAlert.h"


@implementation UIWebView (JavaScriptAlert)

static BOOL status = NO;

static BOOL isEnd = NO;


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

    

    UIAlertView* customAlert = [[UIAlertView alloc] initWithTitle:nil

                                                          message:message

                                                         delegate:nil

                                                cancelButtonTitle:@"确定"

                                                otherButtonTitles:nil];

    

    [customAlert show];

    

}


- (NSString *) webView:(UIWebView *)view runJavaScriptTextInputPanelWithPrompt:(NSString *)prompt defaultText:(NSString *)text initiatedByFrame:(id)frame {

    return @"";

}


- (BOOL)webView:(UIWebView *)sender runJavaScriptConfirmPanelWithMessage:(NSString *)message initiatedByFrame:(id)frame {

    UIAlertView *confirmDiag = [[UIAlertView alloc] initWithTitle:nil

                                                          message:message

                                                         delegate:self

                                                cancelButtonTitle:@"取消"

                                                otherButtonTitles:@"确定", nil];

    

    [confirmDiag show];

    

    CGFloat version = [[[UIDevice currentDevice] systemVersion] floatValue];

    if (version >= 7.) {

        while (isEnd == NO) {

            [[NSRunLoop mainRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:0.01f]];


        }

    }else

    {

        while (isEnd == NO && confirmDiag.superview != nil) {

            [[NSRunLoop mainRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:0.01f]];

        }

    }

    isEnd = NO;

    return status;

}


- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{

    status = buttonIndex;

    isEnd = YES;

}

@end


版本兼容那块没测试,因为我只做iOS7、8,没有设备测试。因为是使用分类重载的方法,所以把这个分类直接拖到工程里,不需要添加任何代码就能实现想要的效果了。大笑




评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值