iOS_捕捉JSAlert/confirm事件

本文介绍了一种在iOS应用中使用原生代码优化加载HTML时出现的警告框和确认框的方法。通过重写UIWebView代理方法,可以自定义这些对话框的样式,提升用户体验。

利用iOS原生代码为壳,加载Html代码的过程中,进行alert或者confirm进行信息提示的时候,会出现下图的情况:
这里写图片描述

这个时候,就需要捕捉alert或者confirm的事件,以便处理提示框显示的样式。


捕捉alert或者confirm的事件,代码如下:

@implementation UIWebView (JavaScriptAlert)

- (void)webView:(UIWebView *)sender runJavaScriptAlertPanelWithMessage:(NSString *)message initiatedByFrame:(id)frame {
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"温馨提示" message:message preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *ensureAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:nil];
    [alertController addAction:ensureAction];

    UIViewController *rootViewController = [UIApplication sharedApplication].windows.firstObject.rootViewController;
    [rootViewController presentViewController:alertController animated:YES completion:nil];
}
static BOOL clickState = NO;
static NSInteger btnIndex = -1;
- (BOOL)webView:(UIWebView *)sender runJavaScriptConfirmPanelWithMessage:(NSString *)message initiatedByFrame:(id)frame {
    NSString *otherButtonText = @"确定";
    NSString *cancelButtonText = @"取消";
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"温馨提示" message:message preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:cancelButtonText style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [self clickActionButton:0];
    }];
    UIAlertAction *ensureAction = [UIAlertAction actionWithTitle:otherButtonText style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [self clickActionButton:1];
    }];
    [alertController addAction:cancelAction];
    [alertController addAction:ensureAction];
    UIViewController *rootViewController = [UIApplication sharedApplication].windows.firstObject.rootViewController;
    [rootViewController presentViewController:alertController animated:YES completion:nil];
    btnIndex = -1;
    while (btnIndex == -1) {
        [[NSRunLoop mainRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:0.1f]];
    }
    if (btnIndex == 0){
        // 取消
        clickState = NO;
    }else if (btnIndex == 1) {
        // 确定
        clickState = YES;
    }
    return clickState;
}
- (void)clickActionButton:(NSInteger)buttonIndex{
    btnIndex = buttonIndex;
}


@end

经测试结果如图所示:
这里写图片描述


代码地址:
https://github.com/FlyingKuiKui/JSMutuallObjectC.git

`variable 'confirm_btn_class' has initializer but incomplete type` 错误通常意味着在初始化变量 `confirm_btn_class` 时,`lv_obj_class_t` 类型未被完整定义。以下是一些可能的解决办法: ### 检查头文件包含 要保证在 `confirm_btn.c` 文件里包含了定义 `lv_obj_class_t` 类型的头文件。一般而言,这个头文件是与 LVGL 库相关的。示例代码如下: ```c #include "lvgl/lvgl.h" // 假设这是定义 lv_obj_class_t 的头文件 const lv_obj_class_t confirm_btn_class = { // 初始化内容 }; ``` ### 检查头文件路径 要确保编译器能够找到定义 `lv_obj_class_t` 类型的头文件。可以通过 `-I` 选项来指定头文件的搜索路径。例如,在 Makefile 里添加如下内容: ```makefile CFLAGS += -I/path/to/lvgl/include ``` ### 检查库的安装 要保证 LVGL 库已经正确安装,并且版本兼容。有时候,库的版本不兼容会引发类型定义不完整的问题。 ### 检查头文件依赖关系 要确保定义 `lv_obj_class_t` 类型的头文件没有依赖其他未包含的头文件。有些头文件可能需要其他头文件的支持才能完整定义类型。 ### 检查代码逻辑 要确保在初始化 `confirm_btn_class` 之前,`lv_obj_class_t` 类型已经被完整定义。有时候,代码逻辑的问题会导致类型定义不完整。 ### 示例代码 以下是一个简单的示例,展示了如何正确初始化 `lv_obj_class_t` 类型的变量: ```c #include "lvgl/lvgl.h" // 定义一个自定义的对象类 static void confirm_btn_constructor(lv_obj_t * obj) { // 构造函数逻辑 } const lv_obj_class_t confirm_btn_class = { .constructor = confirm_btn_constructor, // 其他初始化内容 }; ```
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值