您的位置:首页 > 产品设计 > UI/UE

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

2015-01-20 11:43 381 查看
使用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,没有设备测试。因为是使用分类重载的方法,所以把这个分类直接拖到工程里,不需要添加任何代码就能实现想要的效果了。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  confirm UIAlertView iOS js
相关文章推荐