iOS UIWebView重寫或者監(jiān)聽js Alter/Confirm/Prompt函數(shù)

最近公司在開發(fā)web型的app,涉及許多交互,剛好,我遇到js中 的Alter/Confirm/Prompt三個彈框提示函數(shù)的坑,今天記錄解決的辦法下來,供大家使用。

1、在開發(fā)過程中,前端使用了他們自己的彈框框架,結(jié)果在iOS和Android端展示這樣子,多了http。

77F9F95B-E8C0-4A10-A931-964055A4E9BD.png

2、為了解決這樣的問題,百度了許多,最終借助百度上的資料和官方文檔,試驗成功了。
3、其實不難解決這個問題,新建個UIWebView類別,在m文件寫上如下代碼,即可。

  • (void)webView:(UIWebView *)sender runJavaScriptAlertPanelWithMessage:(NSString *)message initiatedByFrame:(CGRect *)frame {
UIAlertView* customAlert = [[UIAlertView alloc] initWithTitle:@"溫馨提示" message:message delegate:nil cancelButtonTitle:@"確定" otherButtonTitles:nil];

[customAlert show];

}
static BOOL diagStat = NO;

static NSInteger bIdx = -1;

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

    diagStat = NO;

    bIdx = -1;

    UIAlertView *confirmDiag = [[UIAlertView alloc] initWithTitle:@"溫馨提示"
    message:message
    delegate:self
    cancelButtonTitle:@"取消"
    otherButtonTitles:@"確定", nil];

    [confirmDiag show];

    while (bIdx==-1) {
    //[NSThread sleepForTimeInterval:0.2];
    [[NSRunLoop mainRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:0.1f]];
    }
    if (bIdx == 0){//取消;
    diagStat = NO;
    }
    else if (bIdx == 1) {//確定;
    diagStat = YES;
    }
    return diagStat;
    }

  • (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    bIdx = buttonIndex;
    }

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

    diagStat = NO;

    bIdx = -1;
    UIAlertView *confirmDiag = [[UIAlertView alloc] initWithTitle:@"溫馨提示" message:prompt
    delegate:self
    cancelButtonTitle:@"取消"
    otherButtonTitles:@"確定", nil];

[confirmDiag setAlertViewStyle:UIAlertViewStylePlainTextInput];

UITextField *textField = [confirmDiag textFieldAtIndex:0];
textField.placeholder = @"請輸入信息";
textField.keyboardType = UIKeyboardTypeDefault;


[confirmDiag show];

while (bIdx==-1) {
    //[NSThread sleepForTimeInterval:0.2];
    [[NSRunLoop mainRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:0.1f]];
}
if (bIdx == 0){//取消;
    diagStat = NO;
}
else if (bIdx == 1) {//確定;
    diagStat = YES;
}
return textField.text;

效果如下:


7616FF80-91D4-498B-B439-DB7660AE8DED.png

}

最后編輯于
?著作權歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容