swift -> WKWebview -> alert / confirm/ prompt

 

 

    //alert
    func webView(_ webView: WKWebView, runJavaScriptAlertPanelWithMessage message: String, initiatedByFrame frame: WKFrameInfo, completionHandler: @escaping () -> Void) {
        let alertController = UIAlertController(title: nil, message: message, preferredStyle: .alert)
        alertController.addAction(UIAlertAction(title: "OK", style: .default, handler: { (action) in
            completionHandler()
        }))
        present(alertController, animated: true, completion: nil)
        print("dsf")
    }
    //confirm
    func webView(_ webView: WKWebView, runJavaScriptConfirmPanelWithMessage message: String, initiatedByFrame frame: WKFrameInfo, completionHandler: @escaping (Bool) -> Void) {
        let alertController = UIAlertController(title: nil, message: message, preferredStyle: .alert)
        alertController.addAction(UIAlertAction(title: "OK", style: .default, handler: { (action) in
            completionHandler(true)
        }))
        alertController.addAction(UIAlertAction(title: "Cancel", style: .default, handler: { (action) in
            completionHandler(false)
        }))
        present(alertController, animated: true, completion: nil)
    }
    //弹出输入框 js, onclick = prompt('xxxx','');
    func webView(_ webView: WKWebView, runJavaScriptTextInputPanelWithPrompt prompt: String, defaultText: String?, initiatedByFrame frame: WKFrameInfo,
                 completionHandler: @escaping (String?) -> Void) {
        let alertController = UIAlertController(title: nil, message: prompt, preferredStyle: .alert)
        alertController.addTextField { (textField) in
            textField.text = defaultText
        }
        alertController.addAction(UIAlertAction(title: "OK", style: .default, handler: { (action) in
            if let text = alertController.textFields?.first?.text {
                completionHandler(text)
            } else {
                completionHandler(defaultText)
            }
        }))
        alertController.addAction(UIAlertAction(title: "Cancel", style: .default, handler: { (action) in
            completionHandler(nil)
        }))
        present(alertController, animated: true, completion: nil)
    }
    //\\=======wkWebView alert / conifrm=============

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
样式优化美化<!DOCTYPE html> {% load static %} <html> <head> <title>个人中心</title> <style> .container { display: flex; flex-wrap: wrap; justify-content: space-between; } .module { width: calc(25% - 20px); margin-bottom: 20px; padding: 20px; border: 1px solid #ccc; border-radius: 5px; } @media (max-width: 768px) { .module { width: calc(50% - 20px); } } @media (max-width: 480px) { .module { width: 100%; } } </style> </head> <body> <div class="container"> <div class="module personal-info"> <!-- 个人信息模块 --> <h2>个人信息</h2> <form> <label for="username">用户名:</label> <input type="text" id="username"> <label for="email">邮箱:</label> <input type="email" id="email"> <button type="submit">保存</button> </form> </div> <div class="module change-password"> <!-- 修改密码模块 --> <h2>修改密码</h2> <form> <label for="old-password">旧密码:</label> <input type="password" id="old-password"> <label for="new-password">新密码:</label> <input type="password" id="new-password"> <label for="confirm-password">确认密码:</label> <input type="password" id="confirm-password"> <button type="submit">保存</button> </form> </div> <div class="module bind-phone"> <!-- 绑定手机模块 --> <h2>绑定手机</h2> <form> <label for="phone">手机号:</label> <input type="tel" id="phone"> <button type="submit">保存</button> </form> </div> <div class="module my-messages"> <!-- 我的消息模块 --> <h2>我的消息</h2> <ul> <li><a href="#">系统消息1</a></li> <li><a href="#">系统消息2</a></li> <li><a href="#">好友消息1</a></li> <li><a href="#">好友消息2</a></li> </ul> </div> </div> </body> </html>
最新发布
06-09
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值