-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathwkwebview.py
756 lines (586 loc) · 29.5 KB
/
wkwebview.py
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
#coding: utf-8
'''
# WKWebView - modern webview for Pythonista
The underlying component used to implement ui.WebView in Pythonista is UIWebView, which has been deprecated since iOS 8. This module implements a Python webview API using the current iOS-provided view, WKWebView. Besides being Apple-supported, WKWebView brings other benefits such as better Javascript performance and an official communication channel from Javascript to Python. This implementation of a Python API also has the additional benefit of being inheritable.
Available as a [single file](https://github.com/mikaelho/youey/blob/master/youey/wkwebview.py) on GitHub. Run the file as-is to try out some of the capabilities; check the end of the file for demo code.
Credits: This would not exist without @JonB and @mithrendal.
## Basic usage
WKWebView matches ui.WebView API as defined in Pythonista docs. For example:
```
v = WKWebView()
v.present()
v.load_html('<body>Hello world</body>')
v.load_url('http://omz-software.com/pythonista/')
```
For compatibility, there is also the same delegate API that ui.WebView has, with `webview_should_start_load` etc. methods.
## Mismatches with ui.WebView
### Synchronous vs. asynchronous JS evaluation
Apple's WKWebView only provides an async Javascript evaliation function. This is available as an `eval_js_async` method, with an optional `callback` argument that will be called with a single argument containing the result of the JS evaluation (or None).
We also provide a synchronous `eval_js` method, which essentially waits for the callback before returning the result. For this to work, you have to call the `eval_js` method outside the main UI thread, e.g. from a method decorated with `ui.in_background`.
### Handling page scaling
UIWebView had a property called `scales_page_to_fit`, WKWebView does not. See below for the various `disable` methods that can be used instead.
## Additional features and notes
### http allowed
Looks like Pythonista has the specific plist entry required to allow fetching non-secure http urls.
### Other url schemes
If you try to open a url not natively supported by WKWebView, such as `tel:` for phone numbers, the `webbrowser` module is used to open it.
### Swipe navigation
There is a new property, `swipe_navigation`, False by default. If set to True, horizontal swipes navigate backwards and forwards in the browsing history.
Note that browsing history is only updated for calls to `load_url` - `load_html` is ignored (Apple feature that has some security justification).
### Data detection
By default, no Apple data detectors are active for WKWebView. You can activate them by including one or a tuple of the following values as the `data_detectors` argument to the constructor: NONE, PHONE_NUMBER, LINK, ADDRESS, CALENDAR_EVENT, TRACKING_NUMBER, FLIGHT_NUMBER, LOOKUP_SUGGESTION, ALL.
For example, activating just the phone and link detectors:
v = WKWebView(data_detectors=(WKWebView.PHONE_NUMBER, WKWebView.LINK))
### Messages from JS to Python
WKWebView comes with support for JS-to-container messages. Use this by subclassing WKWebView and implementing methods that start with `on_` and accept one message argument. These methods are then callable from JS with the pithy `window.webkit.messageHandler.<name>.postMessage` call, where `<name>` corresponds to whatever you have on the method name after the `on_` prefix.
Here's a minimal example:
class MagicWebView(WKWebView):
def on_magic(self, message):
print('WKWebView magic ' + message)
html = '<body><button onclick="window.webkit.messageHandlers.magic.postMessage(\'spell\')">Cast a spell</button></body>'
v = MagicWebView()
v.load_html(html)
Note that JS postMessage must have a parameter, and the message argument to the Python handler is always a string version of that parameter. For structured data, you need to use e.g. JSON at both ends.
### User scripts a.k.a. script injection
WKWebView supports defining JS scripts that will be automatically loaded with every page.
Use the `add_script(js_script, add_to_end=True)` method for this.
Scripts are added to all frames. Removing scripts is currently not implemented.
Following two convenience methods are also available:
* `add_style(css)` to add a style tag containing the given CSS style definition.
* `add_meta(name, content)` to add a meta tag with the given name and content.
### Making a web page behave more like an app
These methods set various style and meta tags to disable typical web interaction modes:
* `disable_zoom`
* `disable_user_selection`
* `disable_font_resizing`
* `disable_scrolling` (alias for setting `scroll_enabled` to False)
There is also a convenience method, `disable_all`, which calls all of the above.
Note that disabling user selection will also disable the automatic data detection of e.g. phone numbers, described earlier.
### Javascript debugging
Javascript errors and console messages are sent to Python side and printed to Pythonista console. Supported JS console methods are `log`, `info`, `warn` and `error`.
For further JS debugging and experimentation, there is a simple convenience command-line utility that can be used to evaluate load URLs and evaluate javascript. If you `present` your app as a 'panel', you can easily switch between the tabs for your web page and this console.
Or you can just create a WKWebView manually for quick experimentation, like in the usage example below.
>>> from wkwebview import *
>>> v = WKWebView(name='Demo')
>>> WKWebView.console()
Welcome to WKWebView console. Evaluate javascript in any active WKWebView. Special commands: list, switch #, load <url>, quit
js> list
0 - Demo -
js> load http://omz-software.com/pythonista/
js> list
0 - Demo - Pythonista for iOS
js> document.title
Pythonista for iOS
js> quit
>>> v2 = WKWebView(name='Other view')
>>> WKWebView.console()
Welcome to WKWebView console. Evaluate javascript in any active WKWebView. Special commands: list, switch #, load <url>, quit
js> list
0 - Demo - Pythonista for iOS
1 - Other view -
js> switch 1
js> load https://www.python.org
js> document.title
Welcome to Python.org
js> window.doesNotExist.wrongFunction()
ERROR: TypeError: undefined is not an object (evaluating 'window.doesNotExist.wrongFunction') (https://www.python.org/, line: 1, column: 20)
None
js> quit
### Customize Javascript popups
Javascript alert, confirm and prompt dialogs are now implemented with simple Pythonista equivalents. If you need something fancier or e.g. internationalization support, subclass WKWebView and re-implement the following methods as needed:
def _javascript_alert(self, host, message):
console.alert(host, message, 'OK', hide_cancel_button=True)
def _javascript_confirm(self, host, message):
try:
console.alert(host, message, 'OK')
return True
except KeyboardInterrupt:
return False
def _javascript_prompt(self, host, prompt, default_text):
try:
return console.input_alert(host, prompt, default_text, 'OK')
except KeyboardInterrupt:
return None
'''
from objc_util import *
import ui, console, webbrowser
import queue, weakref, ctypes, functools, time, os, json, re
from types import SimpleNamespace
# Helpers for invoking ObjC function blocks with no return value
class _block_descriptor (Structure):
_fields_ = [('reserved', c_ulong), ('size', c_ulong), ('copy_helper', c_void_p), ('dispose_helper', c_void_p), ('signature', c_char_p)]
def _block_literal_fields(*arg_types):
return [('isa', c_void_p), ('flags', c_int), ('reserved', c_int), ('invoke', ctypes.CFUNCTYPE(c_void_p, c_void_p, *arg_types)), ('descriptor', _block_descriptor)]
class WKWebView(ui.View):
# Data detector constants
NONE = 0
PHONE_NUMBER = 1
LINK = 1 << 1
ADDRESS = 1 << 2
CALENDAR_EVENT = 1 << 3
TRACKING_NUMBER = 1 << 4
FLIGHT_NUMBER = 1 << 5
LOOKUP_SUGGESTION = 1 << 6
ALL = 18446744073709551615 # NSUIntegerMax
# Global webview index for console
webviews = []
console_view = UIApplication.sharedApplication().\
keyWindow().rootViewController().\
accessoryViewController().\
consoleViewController()
def __init__(self, swipe_navigation=False, data_detectors=NONE, log_js_evals=False, respect_safe_areas=False, **kwargs):
WKWebView.webviews.append(self)
self.delegate = None
self.log_js_evals = log_js_evals
self.respect_safe_areas = respect_safe_areas
super().__init__(**kwargs)
self.eval_js_queue = queue.Queue()
custom_message_handler = WKWebView.CustomMessageHandler.new().autorelease()
retain_global(custom_message_handler)
custom_message_handler._pythonistawebview = weakref.ref(self)
user_content_controller = self.user_content_controller = WKWebView.WKUserContentController.new().autorelease()
for key in dir(self):
if key.startswith('on_'):
message_name = key[3:]
user_content_controller.addScriptMessageHandler_name_(custom_message_handler, message_name)
self.add_script(WKWebView.js_logging_script)
webview_config = WKWebView.WKWebViewConfiguration.new().autorelease()
webview_config.userContentController = user_content_controller
data_detectors = sum(data_detectors) if type(data_detectors) is tuple else data_detectors
# Must be set to True to get real js
# errors, in combination with setting a
# base directory in the case of load_html
webview_config.preferences().setValue_forKey_(True, 'allowFileAccessFromFileURLs')
webview_config.setDataDetectorTypes_(data_detectors)
nav_delegate = WKWebView.CustomNavigationDelegate.new()
retain_global(nav_delegate)
nav_delegate._pythonistawebview = weakref.ref(self)
ui_delegate = WKWebView.CustomUIDelegate.new()
retain_global(ui_delegate)
ui_delegate._pythonistawebview = weakref.ref(self)
self._create_webview(webview_config, nav_delegate, ui_delegate)
self.swipe_navigation = swipe_navigation
@on_main_thread
def _create_webview(self, webview_config, nav_delegate, ui_delegate):
self.webview = WKWebView.WKWebView.alloc().initWithFrame_configuration_(
((0,0), (self.width, self.height)), webview_config).autorelease()
self.webview.autoresizingMask = 2 + 16 # WH
self.webview.setNavigationDelegate_(nav_delegate)
self.webview.setUIDelegate_(ui_delegate)
self.objc_instance.addSubview_(self.webview)
def layout(self):
if self.respect_safe_areas:
self.update_safe_area_insets()
@on_main_thread
def load_url(self, url):
''' Loads the contents of the given url
asynchronously.
If the url starts with `file://`, loads a local file. If the remaining url starts with `/`, path starts from Pythonista root.
'''
if url.startswith('file://'):
file_path = url[7:]
if file_path.startswith('/'):
root = os.path.expanduser('~')
file_path = root + file_path
else:
current_working_directory = os.path.dirname(os.getcwd())
file_path = current_working_directory+'/' + file_path
dir_only = os.path.dirname(file_path)
file_path = NSURL.fileURLWithPath_(file_path)
dir_only = NSURL.fileURLWithPath_(dir_only)
self.webview.loadFileURL_allowingReadAccessToURL_(file_path, dir_only)
else:
self.webview.loadRequest_(WKWebView.NSURLRequest.requestWithURL_(nsurl(url)))
@on_main_thread
def load_html(self, html):
# Need to set a base directory to get
# real js errors
current_working_directory = os.path.dirname(os.getcwd())
root_dir = NSURL.fileURLWithPath_(current_working_directory)
#root_dir = NSURL.fileURLWithPath_(os.path.expanduser('~'))
self.webview.loadHTMLString_baseURL_(html, root_dir)
def eval_js(self, js):
self.eval_js_async(js, self._eval_js_sync_callback)
value = self.eval_js_queue.get()
return value
evaluate_javascript = eval_js
@on_main_thread
def _eval_js_sync_callback(self, value):
self.eval_js_queue.put(value)
@on_main_thread
def eval_js_async(self, js, callback=None):
if self.log_js_evals:
self.console.message({'level': 'code', 'content': js})
handler = functools.partial(WKWebView._handle_completion, callback, self)
block = ObjCBlock(handler, restype=None, argtypes=[c_void_p, c_void_p, c_void_p])
retain_global(block)
self.webview.evaluateJavaScript_completionHandler_(js, block)
# Javascript evaluation completion handler
def _handle_completion(callback, webview, _cmd, _obj, _err):
result = str(ObjCInstance(_obj)) if _obj else None
if webview.log_js_evals:
webview._message({'level': 'raw', 'content': str(result)})
if callback:
callback(result)
def add_script(self, js_script, add_to_end=True):
location = 1 if add_to_end else 0
wk_script = WKWebView.WKUserScript.alloc().initWithSource_injectionTime_forMainFrameOnly_(js_script, location, False)
self.user_content_controller.addUserScript_(wk_script)
def add_style(self, css):
"Convenience method to add a style tag with the given css, to every page loaded by the view."
css = css.replace("'", "\'")
js = f"var style = document.createElement('style');style.innerHTML = '{css}';document.getElementsByTagName('head')[0].appendChild(style);"
self.add_script(js, add_to_end=True)
def add_meta(self, name, content):
"Convenience method to add a meta tag with the given name and content, to every page loaded by the view."
name = name.replace("'", "\'")
content = content.replace("'", "\'")
js = f"var meta = document.createElement('meta'); meta.setAttribute('name', '{name}'); meta.setAttribute('content', '{content}'); document.getElementsByTagName('head')[0].appendChild(meta);"
self.add_script(js, add_to_end=True)
def disable_zoom(self):
name = 'viewport'
content = 'width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no'
self.add_meta(name, content)
def disable_user_selection(self):
css = '* { -webkit-user-select: none; }'
self.add_style(css)
def disable_font_resizing(self):
css = 'body { -webkit-text-size-adjust: none; }'
self.add_style(css)
def disable_scrolling(self):
"Included for consistency with the other `disable_x` methods, this is equivalent to setting `scroll_enabled` to false."
self.scroll_enabled = False
def disable_all(self):
"Convenience method that calls all the `disable_x` methods to make the loaded pages act more like an app."
self.disable_zoom()
self.disable_scrolling()
self.disable_user_selection()
self.disable_font_resizing()
@property
def user_agent(self):
"Must be called outside main thread"
return self.eval_js('navigator.userAgent')
@on_main_thread
def _get_user_agent2(self):
return str(self.webview.customUserAgent())
@user_agent.setter
def user_agent(self, value):
value = str(value)
self._set_user_agent(value)
@on_main_thread
def _set_user_agent(self, value):
self.webview.setCustomUserAgent_(value)
@on_main_thread
def go_back(self):
self.webview.goBack()
@on_main_thread
def go_forward(self):
self.webview.goForward()
@on_main_thread
def reload(self):
self.webview.reload()
@on_main_thread
def stop(self):
self.webview.stopLoading()
@property
def scales_page_to_fit(self):
raise NotImplementedError('Not supported on iOS. Use the "disable_" methods instead.')
@scales_page_to_fit.setter
def scales_page_to_fit(self, value):
raise NotImplementedError('Not supported on iOS. Use the "disable_" methods instead.')
@property
def swipe_navigation(self):
return self.webview.allowsBackForwardNavigationGestures()
@swipe_navigation.setter
def swipe_navigation(self, value):
self.webview.setAllowsBackForwardNavigationGestures_(value == True)
@property
def scroll_enabled(self):
'''Controls whether scrolling is enabled.
Disabling scrolling is applicable for pages that need to look like an app.'''
return self.webview.scrollView().scrollEnabled()
@scroll_enabled.setter
def scroll_enabled(self, value):
self.webview.scrollView().setScrollEnabled_(value == True)
def update_safe_area_insets(self):
insets = self.objc_instance.safeAreaInsets()
self.frame = self.frame.inset(insets.top, insets.left, insets.bottom, insets.right)
def _javascript_alert(self, host, message):
console.alert(host, message, 'OK', hide_cancel_button=True)
def _javascript_confirm(self, host, message):
try:
console.alert(host, message, 'OK')
return True
except KeyboardInterrupt:
return False
def _javascript_prompt(self, host, prompt, default_text):
try:
return console.input_alert(host, prompt, default_text, 'OK')
except KeyboardInterrupt:
return None
js_logging_script = 'console = new Object(); console.info = function(message) { window.webkit.messageHandlers.javascript_console_message.postMessage(JSON.stringify({ level: "info", content: message})); return false; }; console.log = function(message) { window.webkit.messageHandlers.javascript_console_message.postMessage(JSON.stringify({ level: "log", content: message})); return false; }; console.warn = function(message) { window.webkit.messageHandlers.javascript_console_message.postMessage(JSON.stringify({ level: "warn", content: message})); return false; }; console.error = function(message) { window.webkit.messageHandlers.javascript_console_message.postMessage(JSON.stringify({ level: "error", content: message})); return false; }; window.onerror = (function(error, url, line, col, errorobj) { console.error("" + error + " (" + url + ", line: " + line + ", column: " + col + ")"); });'
def on_javascript_console_message(self, message):
log_message = json.loads(message)
#self.console.message(log_message)
self._message(log_message)
def _message(self, message):
level, content = message['level'], message['content']
if level == 'code':
print('>>> ' + content)
elif level == 'raw':
print(content)
else:
print(level.upper() + ': ' + content)
class Theme:
@classmethod
def get_theme(cls):
theme_dict = json.loads(cls.clean_json(cls.get_theme_data()))
theme = SimpleNamespace(**theme_dict)
theme.dict = theme_dict
return theme
@classmethod
def get_theme_data(cls):
# Name of current theme
defaults = ObjCClass("NSUserDefaults").standardUserDefaults()
name = str(defaults.objectForKey_("ThemeName"))
# Theme is user-created
if name.startswith("User:"):
home = os.getenv("CFFIXED_USER_HOME")
user_themes_path = os.path.join(home,
"Library/Application Support/Themes")
theme_path = os.path.join(user_themes_path, name[5:] + ".json")
# Theme is built-in
else:
res_path = str(ObjCClass("NSBundle").mainBundle().resourcePath())
theme_path = os.path.join(res_path, "Themes2/%s.json" % name)
# Read theme file
with open(theme_path, "r") as f:
data = f.read()
# Return contents
return data
@classmethod
def clean_json(cls, string):
# From http://stackoverflow.com/questions/23705304
string = re.sub(",[ \t\r\n]+}", "}", string)
string = re.sub(",[ \t\r\n]+\]", "]", string)
return string
@classmethod
def console(self, webview_index=0):
webview = WKWebView.webviews[webview_index]
theme = WKWebView.Theme.get_theme()
print('Welcome to WKWebView console.')
print('Evaluate javascript in any active WKWebView.')
print('Special commands: list, switch #, load <url>, quit')
console.set_color(*ui.parse_color(theme.tint)[:3])
while True:
value = input('js> ').strip()
self.console_view.history().insertObject_atIndex_(ns(value+'\n'),0)
if value == 'quit':
break
if value == 'list':
for i in range(len(WKWebView.webviews)):
wv = WKWebView.webviews[i]
print(i, '-', wv.name, '-', wv.eval_js('document.title'))
elif value.startswith('switch '):
i = int(value[len('switch '):])
webview = WKWebView.webviews[i]
elif value.startswith('load '):
url = value[len('load '):]
webview.load_url(url)
else:
print(webview.eval_js(value))
console.set_color(*ui.parse_color(theme.default_text)[:3])
# MAIN OBJC SECTION
WKWebView = ObjCClass('WKWebView')
UIViewController = ObjCClass('UIViewController')
WKWebViewConfiguration = ObjCClass('WKWebViewConfiguration')
WKUserContentController = ObjCClass('WKUserContentController')
NSURLRequest = ObjCClass('NSURLRequest')
WKUserScript = ObjCClass('WKUserScript')
# Navigation delegate
class _block_decision_handler(Structure):
_fields_ = _block_literal_fields(ctypes.c_long)
def webView_decidePolicyForNavigationAction_decisionHandler_(_self, _cmd, _webview, _navigation_action, _decision_handler):
delegate_instance = ObjCInstance(_self)
webview = delegate_instance._pythonistawebview()
deleg = webview.delegate
nav_action = ObjCInstance(_navigation_action)
ns_url = nav_action.request().URL()
url = str(ns_url)
nav_type = int(nav_action.navigationType())
allow = True
if deleg is not None:
if hasattr(deleg, 'webview_should_start_load'):
allow = deleg.webview_should_start_load(webview, url, nav_type)
scheme = str(ns_url.scheme())
if not WKWebView.WKWebView.handlesURLScheme_(scheme):
allow = False
webbrowser.open(url)
allow_or_cancel = 1 if allow else 0
decision_handler = ObjCInstance(_decision_handler)
retain_global(decision_handler)
blk = WKWebView._block_decision_handler.from_address(_decision_handler)
blk.invoke(_decision_handler, allow_or_cancel)
f = webView_decidePolicyForNavigationAction_decisionHandler_
f.argtypes = [c_void_p]*3
f.restype = None
f.encoding = b'v@:@@@?'
# https://developer.apple.com/library/archive/documentation/Cocoa/Conceptual/ObjCRuntimeGuide/Articles/ocrtTypeEncodings.html
def webView_didCommitNavigation_(_self, _cmd, _webview, _navigation):
delegate_instance = ObjCInstance(_self)
webview = delegate_instance._pythonistawebview()
deleg = webview.delegate
if deleg is not None:
if hasattr(deleg, 'webview_did_start_load'):
deleg.webview_did_start_load(webview)
def webView_didFinishNavigation_(_self, _cmd, _webview, _navigation):
delegate_instance = ObjCInstance(_self)
webview = delegate_instance._pythonistawebview()
deleg = webview.delegate
if deleg is not None:
if hasattr(deleg, 'webview_did_finish_load'):
deleg.webview_did_finish_load(webview)
def webView_didFailNavigation_withError_(_self, _cmd, _webview, _navigation, _error):
delegate_instance = ObjCInstance(_self)
webview = delegate_instance._pythonistawebview()
deleg = webview.delegate
err = ObjCInstance(_error)
error_code = int(err.code())
error_msg = str(err.localizedDescription())
if deleg is not None:
if hasattr(deleg, 'webview_did_fail_load'):
deleg.webview_did_fail_load(webview, error_code, error_msg)
return
raise RuntimeError(f'WKWebView load failed with code {error_code}: {error_msg}')
def webView_didFailProvisionalNavigation_withError_(_self, _cmd, _webview, _navigation, _error):
WKWebView.webView_didFailNavigation_withError_(_self, _cmd, _webview, _navigation, _error)
CustomNavigationDelegate = create_objc_class('CustomNavigationDelegate', superclass=NSObject, methods=[
webView_didCommitNavigation_,
webView_didFinishNavigation_,
webView_didFailNavigation_withError_,
webView_didFailProvisionalNavigation_withError_,
webView_decidePolicyForNavigationAction_decisionHandler_
],
protocols=['WKNavigationDelegate'])
# Script message handler
def userContentController_didReceiveScriptMessage_(_self, _cmd, _userContentController, _message):
controller_instance = ObjCInstance(_self)
webview = controller_instance._pythonistawebview()
wk_message = ObjCInstance(_message)
name = str(wk_message.name())
content = str(wk_message.body())
handler = getattr(webview, 'on_'+name, None)
if handler:
handler(content)
else:
raise Exception(f'Unhandled message from script - name: {name}, content: {content}')
CustomMessageHandler = create_objc_class('CustomMessageHandler', UIViewController, methods=[
userContentController_didReceiveScriptMessage_
], protocols=['WKScriptMessageHandler'])
# UI delegate (for alerts etc.)
class _block_alert_completion(Structure):
_fields_ = _block_literal_fields()
def webView_runJavaScriptAlertPanelWithMessage_initiatedByFrame_completionHandler_(_self, _cmd, _webview, _message, _frame, _completion_handler):
delegate_instance = ObjCInstance(_self)
webview = delegate_instance._pythonistawebview()
message = str(ObjCInstance(_message))
host = str(ObjCInstance(_frame).request().URL().host())
webview._javascript_alert(host, message)
#console.alert(host, message, 'OK', hide_cancel_button=True)
completion_handler = ObjCInstance(_completion_handler)
retain_global(completion_handler)
blk = WKWebView._block_alert_completion.from_address(_completion_handler)
blk.invoke(_completion_handler)
f = webView_runJavaScriptAlertPanelWithMessage_initiatedByFrame_completionHandler_
f.argtypes = [c_void_p]*4
f.restype = None
f.encoding = b'v@:@@@@?'
class _block_confirm_completion(Structure):
_fields_ = _block_literal_fields(ctypes.c_bool)
def webView_runJavaScriptConfirmPanelWithMessage_initiatedByFrame_completionHandler_(_self, _cmd, _webview, _message, _frame, _completion_handler):
delegate_instance = ObjCInstance(_self)
webview = delegate_instance._pythonistawebview()
message = str(ObjCInstance(_message))
host = str(ObjCInstance(_frame).request().URL().host())
result = webview._javascript_confirm(host, message)
completion_handler = ObjCInstance(_completion_handler)
retain_global(completion_handler)
blk = WKWebView._block_confirm_completion.from_address(_completion_handler)
blk.invoke(_completion_handler, result)
f = webView_runJavaScriptConfirmPanelWithMessage_initiatedByFrame_completionHandler_
f.argtypes = [c_void_p]*4
f.restype = None
f.encoding = b'v@:@@@@?'
class _block_text_completion(Structure):
_fields_ = _block_literal_fields(c_void_p)
def webView_runJavaScriptTextInputPanelWithPrompt_defaultText_initiatedByFrame_completionHandler_(_self, _cmd, _webview, _prompt, _default_text, _frame, _completion_handler):
delegate_instance = ObjCInstance(_self)
webview = delegate_instance._pythonistawebview()
prompt = str(ObjCInstance(_prompt))
default_text = str(ObjCInstance(_default_text))
host = str(ObjCInstance(_frame).request().URL().host())
result = webview._javascript_prompt(host, prompt, default_text)
completion_handler = ObjCInstance(_completion_handler)
retain_global(completion_handler)
blk = WKWebView._block_text_completion.from_address(_completion_handler)
blk.invoke(_completion_handler, ns(result))
f = webView_runJavaScriptTextInputPanelWithPrompt_defaultText_initiatedByFrame_completionHandler_
f.argtypes = [c_void_p]*5
f.restype = None
f.encoding = b'v@:@@@@@?'
CustomUIDelegate = create_objc_class('CustomUIDelegate', superclass=NSObject, methods=[
webView_runJavaScriptAlertPanelWithMessage_initiatedByFrame_completionHandler_,
webView_runJavaScriptConfirmPanelWithMessage_initiatedByFrame_completionHandler_,
webView_runJavaScriptTextInputPanelWithPrompt_defaultText_initiatedByFrame_completionHandler_
],
protocols=['WKUIDelegate'])
if __name__ == '__main__':
class MyWebViewDelegate:
def webview_should_start_load(self, webview, url, nav_type):
"See nav_type options at https://developer.apple.com/documentation/webkit/wknavigationtype?language=objc"
print('Will start loading', url)
return True
def webview_did_start_load(self, webview):
print('Started loading')
@ui.in_background
def webview_did_finish_load(self, webview):
print('Finished loading ' + webview.eval_js('document.title'))
class MyWebView(WKWebView):
def on_greeting(self, message):
console.alert(message, 'Message passed to Python', 'OK', hide_cancel_button=True)
html = '''
<html>
<head>
<title>WKWebView tests</title>
<script>
function initialize() {
//result = prompt('Initialized', 'Yes, indeed');
//if (result) {
//window.webkit.messageHandlers.greeting.postMessage(result ? result : "<Dialog cancelled>");
//}
}
</script>
</head>
<body onload="initialize()" style="font-size: xx-large; text-align: center">
<p>
Hello world
</p>
<p>
<a href="http://omz-software.com/pythonista/">Pythonista home page</a>
</p>
<p>
+358 40 1234567
</p>
<p>
http://omz-software.com/pythonista/
</p>
</body>
'''
r = ui.View(background_color='black')
v = MyWebView(name='DemoWKWebView', delegate=MyWebViewDelegate(), swipe_navigation=True, data_detectors=(WKWebView.PHONE_NUMBER,WKWebView.LINK), frame=r.bounds, flex='WH')
r.add_subview(v)
r.present() # Use 'panel' if you want to view console in another tab
#v.disable_all()
v.load_html(html)
#v.load_url('http://omz-software.com/pythonista/')
#v.load_url('file://some/local/file.html')