WKWebView使用(一)彈框?qū)崿F(xiàn)

WKWebView文章匯總

WKWebView中js無(wú)法主動(dòng)彈出彈框损痰,因此需要通過(guò)WebKit提供的代理方法辽狈,在navite層彈出彈框车吹,并且將彈框的響應(yīng)返回給web層

  1. 代理方法說(shuō)明
  2. 具體實(shí)現(xiàn)

效果:

效果展示.gif

1、代理方法

WKUIDelegate協(xié)議中提供了彈框方法婚瓜,包括alert沼死、textInput阁吝、confirm。

該協(xié)議還提供了大量的與web交互的方法绷柒,可查看WKWebView詳解(二)- WebKit框架認(rèn)識(shí)

//顯示JavaScript的alert彈框面板志于。
- webView:runJavaScriptAlertPanelWithMessage:initiatedByFrame:completionHandler:

//顯示JavaScript的confirm彈框面板。
- webView:runJavaScriptConfirmPanelWithMessage:initiatedByFrame:completionHandler:

//顯示JavaScript的textInput彈框面板废睦。
webView:runJavaScriptTextInputPanelWithPrompt:defaultText:initiatedByFrame:completionHandler:

2伺绽、具體實(shí)現(xiàn)

2.1 寫一個(gè)簡(jiǎn)單的網(wǎng)頁(yè)

這里增加了彈框響應(yīng)給web層的操作,這里僅做簡(jiǎn)單的彈出顯示

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>標(biāo)題</title>
<script>
function alertTest(){
    window.alert("message");
}

function confirmTest(){
    var result = window.confirm("message");
    if (result == true){
        alert("true");
    } else {
        alert ("flase");
    }
}

function inputTest(){
    var result = window.prompt("message","嘿哈!");
    if (result != null && result != ""){
        alert(result);
    }
}

</script>
</head>
<body>

<button type="button" onclick="alertTest()" style="color:blue;margin-left:40px;font-size:50px;">alert</button>

<button type="button" onclick="confirmTest()" style="color:red;margin-left:40px;font-size:50px;">confirm</button>

<button type="button" onclick="inputTest()" style="color:green;margin-left:40px;font-size:50px;">input</button>

</body>
</html>

2.2 加載網(wǎng)頁(yè)

#import "ViewController.h"
#import <WebKit/WebKit.h>

@interface ViewController ()<WKURLSchemeHandler, WKNavigationDelegate, WKUIDelegate>
@property (nonatomic, strong) WKWebView *webView;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    WKWebViewConfiguration *configuration = [[WKWebViewConfiguration alloc] init];
    CGRect myFrame = self.view.frame;
    self.webView = [[WKWebView alloc] initWithFrame:myFrame configuration:configuration];
    self.webView.UIDelegate = self;
    NSString *path = [[NSBundle mainBundle ] pathForResource:@"first" ofType:@"html"];
    [self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:path]]];
    [self.view addSubview:self.webView];
}

說(shuō)明:

  • 彈出彈框需要遵守WKUIDelegate協(xié)議
  • 通過(guò)loadRequest方法直接加載本地文件

2.3 彈框?qū)崿F(xiàn)

//alert
- (void)webView:(WKWebView *)webView runJavaScriptAlertPanelWithMessage:(NSString *)message initiatedByFrame:(WKFrameInfo *)frame completionHandler:(void (^)(void))completionHandler{
    if (!message || message.length == 0) {
        completionHandler();
        return;
    }
    UIAlertController *controller = [UIAlertController alertControllerWithTitle:webView.title message:message preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"確定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        completionHandler();
    }];
    [controller addAction:okAction];

    [self presentViewController:controller animated:YES completion:nil];
}

//confirm
- (void)webView:(WKWebView *)webView runJavaScriptConfirmPanelWithMessage:(NSString *)message initiatedByFrame:(WKFrameInfo *)frame completionHandler:(void (^)(BOOL))completionHandler{
    if (!message || message.length == 0) {
        completionHandler(NO);
        return;
    }
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:webView.title message:message preferredStyle:UIAlertControllerStyleAlert];
    
    UIAlertAction * cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        completionHandler(NO);
    }];
    
    UIAlertAction * confirmAction = [UIAlertAction actionWithTitle:@"確定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        completionHandler(YES);
    }];
    
    [alertController addAction:cancelAction];
    [alertController addAction:confirmAction];
    
    [self presentViewController:alertController animated:YES completion:nil];
    
}

//input
- (void)webView:(WKWebView *)webView runJavaScriptTextInputPanelWithPrompt:(NSString *)prompt defaultText:(NSString *)defaultText initiatedByFrame:(WKFrameInfo *)frame completionHandler:(void (^)(NSString * _Nullable))completionHandler{
    if (!prompt || prompt.length == 0) {
        completionHandler(@"");
        return;
    }
    
    NSString *placeholderText = @"請(qǐng)輸入文本";
    UIAlertController * alertController = [UIAlertController alertControllerWithTitle:webView.title message:prompt preferredStyle:UIAlertControllerStyleAlert];
    [alertController addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
        textField.text = defaultText;
        textField.placeholder = placeholderText;
    }];
    UIAlertAction * action = [UIAlertAction actionWithTitle:@"確定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        completionHandler(alertController.textFields[0].text ? : @"");
    }];
    [alertController addAction:action];
    
    [self presentViewController:alertController animated:YES completion:nil];
}

說(shuō)明:

  • 如果傳入的message為空奈应,也需要有個(gè)空返回
  • 在代理方法中只需要按照OC方式實(shí)現(xiàn)彈框即可
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末澜掩,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子杖挣,更是在濱河造成了極大的恐慌肩榕,老刑警劉巖,帶你破解...
    沈念sama閱讀 221,695評(píng)論 6 515
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件惩妇,死亡現(xiàn)場(chǎng)離奇詭異株汉,居然都是意外死亡,警方通過(guò)查閱死者的電腦和手機(jī)歌殃,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,569評(píng)論 3 399
  • 文/潘曉璐 我一進(jìn)店門乔妈,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái),“玉大人氓皱,你說(shuō)我怎么就攤上這事路召。” “怎么了匀泊?”我有些...
    開封第一講書人閱讀 168,130評(píng)論 0 360
  • 文/不壞的土叔 我叫張陵优训,是天一觀的道長(zhǎng)。 經(jīng)常有香客問(wèn)我各聘,道長(zhǎng)揣非,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 59,648評(píng)論 1 297
  • 正文 為了忘掉前任躲因,我火速辦了婚禮早敬,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘大脉。我一直安慰自己搞监,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 68,655評(píng)論 6 397
  • 文/花漫 我一把揭開白布镰矿。 她就那樣靜靜地躺著琐驴,像睡著了一般。 火紅的嫁衣襯著肌膚如雪秤标。 梳的紋絲不亂的頭發(fā)上绝淡,一...
    開封第一講書人閱讀 52,268評(píng)論 1 309
  • 那天,我揣著相機(jī)與錄音苍姜,去河邊找鬼牢酵。 笑死,一個(gè)胖子當(dāng)著我的面吹牛衙猪,可吹牛的內(nèi)容都是我干的馍乙。 我是一名探鬼主播布近,決...
    沈念sama閱讀 40,835評(píng)論 3 421
  • 文/蒼蘭香墨 我猛地睜開眼,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼丝格!你這毒婦竟也來(lái)了撑瞧?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,740評(píng)論 0 276
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤显蝌,失蹤者是張志新(化名)和其女友劉穎季蚂,沒(méi)想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體琅束,經(jīng)...
    沈念sama閱讀 46,286評(píng)論 1 318
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡扭屁,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,375評(píng)論 3 340
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了涩禀。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片料滥。...
    茶點(diǎn)故事閱讀 40,505評(píng)論 1 352
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖艾船,靈堂內(nèi)的尸體忽然破棺而出葵腹,到底是詐尸還是另有隱情,我是刑警寧澤屿岂,帶...
    沈念sama閱讀 36,185評(píng)論 5 350
  • 正文 年R本政府宣布践宴,位于F島的核電站,受9級(jí)特大地震影響爷怀,放射性物質(zhì)發(fā)生泄漏阻肩。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,873評(píng)論 3 333
  • 文/蒙蒙 一运授、第九天 我趴在偏房一處隱蔽的房頂上張望烤惊。 院中可真熱鬧,春花似錦吁朦、人聲如沸柒室。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,357評(píng)論 0 24
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)雄右。三九已至,卻和暖如春纺讲,著一層夾襖步出監(jiān)牢的瞬間擂仍,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,466評(píng)論 1 272
  • 我被黑心中介騙來(lái)泰國(guó)打工刻诊, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留防楷,地道東北人牺丙。 一個(gè)月前我還...
    沈念sama閱讀 48,921評(píng)論 3 376
  • 正文 我出身青樓则涯,卻偏偏與公主長(zhǎng)得像复局,于是被迫代替她去往敵國(guó)和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子粟判,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,515評(píng)論 2 359

推薦閱讀更多精彩內(nèi)容