-
Notifications
You must be signed in to change notification settings - Fork 101
/
Copy pathJavaScriptTextInputPanelViewController.swift
55 lines (49 loc) · 1.89 KB
/
JavaScriptTextInputPanelViewController.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
//
// JavaScriptTextInputPanelViewController.swift
// Example
//
// Created by TTOzzi on 2021/05/29.
// Copyright © 2021 RxSwift Community. All rights reserved.
//
import UIKit
import WebKit
import RxWebKit
import RxSwift
import RxCocoa
class JavaScriptTextInputPanelViewController: UIViewController {
let bag = DisposeBag()
let wkWebView = WKWebView()
override func viewDidLoad() {
super.viewDidLoad()
view.addSubview(wkWebView)
wkWebView.load(URLRequest(url: URL(string: "https://www.w3schools.com/jsref/tryit.asp?filename=tryjsref_prompt")!))
wkWebView.rx
.javaScriptTextInputPanel
.debug("javaScriptTextInputPanel")
.subscribe(onNext: { [weak self] webView, prompt, defaultText, frame, handler in
guard let self = self else { return }
let alert = UIAlertController(title: "JavaScriptTextInput", message: prompt, preferredStyle: .alert)
alert.addTextField { textField in
textField.text = defaultText
}
alert.addAction(
UIAlertAction(title: "Confirm", style: .default, handler: { _ in
let text = alert.textFields?.first?.text
handler(text)
})
)
alert.addAction(
UIAlertAction(title: "Cancel", style: .cancel, handler: { _ in
handler(nil)
})
)
self.present(alert, animated: true, completion: nil)
})
.disposed(by: bag)
}
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
let originY = UIApplication.shared.statusBarFrame.maxY
wkWebView.frame = CGRect(x: 0, y: originY, width: view.bounds.width, height: view.bounds.height)
}
}