Skip to content

Commit

Permalink
Clear cache and media options
Browse files Browse the repository at this point in the history
  • Loading branch information
mikaelho committed Mar 18, 2019
1 parent d694f1e commit 667cfd4
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 18 deletions.
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,21 @@ For remote (non-file) `load_url` requests, there are two additional options:
* Set `no_cache` to `True` to skip the local cache, default is `False`
* Set `timeout` to a specific timeout value, default is 10 (seconds)

You can also explicitly clear all data types from the default data store with the `clear_cache` instance method. The method takes an optional parameter, a plain function that will be called when the async cache clearing operation is finished:

def cleared():
print('Cache cleared')

WKWebView().clear_cache(cleared)

### Media playback

Following media playback options are available as WKWebView constructor parameters:

* `inline_media` - whether HTML5 videos play inline or use the native full-screen controller. The default value for iPhone is False and the default value for iPad is True.
* `airplay_media` - whether AirPlay is allowed. The default value is True.
* `pip_media` - whether HTML5 videos can play picture-in-picture. The default value is True.

### 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.
Expand Down
61 changes: 43 additions & 18 deletions wkwebview.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,23 @@
# 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)]
_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)]

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):

Expand All @@ -38,7 +50,15 @@ class WKWebView(ui.View):
accessoryViewController().\
consoleViewController()

def __init__(self, swipe_navigation=False, data_detectors=NONE, log_js_evals=False, respect_safe_areas=False, **kwargs):
def __init__(self,
swipe_navigation=False,
data_detectors=NONE,
log_js_evals=False,
respect_safe_areas=False,
inline_media=None,
airplay_media=True,
pip_media=True,
**kwargs):

WKWebView.webviews.append(self)
self.delegate = None
Expand All @@ -64,12 +84,17 @@ def __init__(self, swipe_navigation=False, data_detectors=NONE, log_js_evals=Fal
webview_config.userContentController = user_content_controller

data_detectors = sum(data_detectors) if type(data_detectors) is tuple else data_detectors
webview_config.setDataDetectorTypes_(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)

if inline_media is not None:
webview_config.allowsInlineMediaPlayback = inline_media
webview_config.allowsAirPlayForMediaPlayback = airplay_media
webview_config.allowsPictureInPictureMediaPlayback = pip_media

nav_delegate = WKWebView.CustomNavigationDelegate.new()
retain_global(nav_delegate)
Expand Down Expand Up @@ -159,17 +184,14 @@ def eval_js_async(self, js, callback=None):
retain_global(block)
self.webview.evaluateJavaScript_completionHandler_(js, block)

def clear_cache(self):
'''
//// All kinds of data
//NSSet *websiteDataTypes = [WKWebsiteDataStore allWebsiteDataTypes];
//// Date from
NSDate *dateFrom = [NSDate dateWithTimeIntervalSince1970:0];
//// Execute
[[WKWebsiteDataStore defaultDataStore] removeDataOfTypes:websiteDataTypes modifiedSince:dateFrom completionHandler:^{
// Done
}];
'''
def clear_cache(self, completion_handler=None):
store = WKWebView.WKWebsiteDataStore.defaultDataStore()
data_types = WKWebView.WKWebsiteDataStore.allWebsiteDataTypes()
from_start = WKWebView.NSDate.dateWithTimeIntervalSince1970_(0)
def dummy_completion_handler():
pass
store.removeDataOfTypes_modifiedSince_completionHandler_(
data_types, from_start, completion_handler or dummy_completion_handler)

# Javascript evaluation completion handler

Expand Down Expand Up @@ -392,6 +414,8 @@ def console(self, webview_index=0):
WKUserContentController = ObjCClass('WKUserContentController')
NSURLRequest = ObjCClass('NSURLRequest')
WKUserScript = ObjCClass('WKUserScript')
WKWebsiteDataStore = ObjCClass('WKWebsiteDataStore')
NSDate = ObjCClass('NSDate')

# Navigation delegate

Expand Down Expand Up @@ -621,6 +645,7 @@ def on_greeting(self, message):
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/', no_cache=True, timeout=5)
#v.load_html(html)
v.load_url('http://omz-software.com/pythonista/', no_cache=False, timeout=5)
#v.load_url('file://some/local/file.html')
v.clear_cache()

0 comments on commit 667cfd4

Please sign in to comment.