0
# Modern WebKit Components
1
2
Modern WKWebView-based web browsing components introduced in macOS 10.10, providing improved security, performance, and feature set compared to legacy WebView. The WK-prefixed classes represent Apple's current WebKit implementation with process isolation, enhanced security policies, and better integration with modern web standards.
3
4
## Capabilities
5
6
### Core Web View
7
8
#### WKWebView
9
10
The primary web view component for displaying web content with modern security and performance characteristics.
11
12
```python { .api }
13
class WKWebView:
14
def initWithFrame_configuration_(self, frame: CGRect, configuration: WKWebViewConfiguration) -> WKWebView: ...
15
def loadRequest_(self, request: NSURLRequest) -> WKNavigation: ...
16
def loadHTMLString_baseURL_(self, string: str, baseURL: NSURL) -> WKNavigation: ...
17
def loadFileURL_allowingReadAccessToURL_(self, URL: NSURL, readAccessURL: NSURL) -> WKNavigation: ...
18
def loadData_MIMEType_characterEncodingName_baseURL_(self, data: NSData, MIMEType: str, characterEncodingName: str, baseURL: NSURL) -> WKNavigation: ...
19
20
# Navigation
21
def goBack(self) -> WKNavigation: ...
22
def goForward(self) -> WKNavigation: ...
23
def goToBackForwardListItem_(self, item: WKBackForwardListItem) -> WKNavigation: ...
24
def reload(self) -> WKNavigation: ...
25
def reloadFromOrigin(self) -> WKNavigation: ...
26
def stopLoading(self): ...
27
28
# JavaScript execution
29
def evaluateJavaScript_completionHandler_(self, javaScriptString: str, completionHandler: callable): ...
30
def callAsyncJavaScript_arguments_inFrame_inContentWorld_completionHandler_(self, functionBody: str, arguments: dict, frame: WKFrameInfo, contentWorld: WKContentWorld, completionHandler: callable): ...
31
32
# Properties
33
@property
34
def configuration(self) -> WKWebViewConfiguration: ...
35
@property
36
def navigationDelegate(self) -> WKNavigationDelegate: ...
37
@property
38
def UIDelegate(self) -> WKUIDelegate: ...
39
@property
40
def backForwardList(self) -> WKBackForwardList: ...
41
@property
42
def title(self) -> str: ...
43
@property
44
def URL(self) -> NSURL: ...
45
@property
46
def isLoading(self) -> bool: ...
47
@property
48
def estimatedProgress(self) -> float: ...
49
@property
50
def hasOnlySecureContent(self) -> bool: ...
51
@property
52
def certificateChain(self) -> list: ...
53
@property
54
def canGoBack(self) -> bool: ...
55
@property
56
def canGoForward(self) -> bool: ...
57
@property
58
def allowsBackForwardNavigationGestures(self) -> bool: ...
59
@property
60
def customUserAgent(self) -> str: ...
61
@property
62
def allowsMagnification(self) -> bool: ...
63
@property
64
def magnification(self) -> float: ...
65
66
# Find in page
67
def findString_withConfiguration_completionHandler_(self, string: str, configuration: WKFindConfiguration, completionHandler: callable): ...
68
69
# PDF and Snapshots
70
def createPDFWithConfiguration_completionHandler_(self, configuration: WKPDFConfiguration, completionHandler: callable): ...
71
def takeSnapshotWithConfiguration_completionHandler_(self, configuration: WKSnapshotConfiguration, completionHandler: callable): ...
72
73
# Media Control
74
def pauseAllMediaPlayback_(self, completionHandler: callable): ...
75
def suspendAllMediaPlayback_(self, completionHandler: callable): ...
76
def resumeAllMediaPlayback_(self, completionHandler: callable): ...
77
def requestMediaPlaybackState_(self, completionHandler: callable): ...
78
def setMicrophoneCaptureState_completionHandler_(self, state: WKMediaCaptureState, completionHandler: callable): ...
79
def setCameraCaptureState_completionHandler_(self, state: WKMediaCaptureState, completionHandler: callable): ...
80
81
# Security
82
@property
83
def serverTrust(self) -> SecTrust: ...
84
85
# Media
86
@property
87
def mediaType(self) -> str: ...
88
@property
89
def pageZoom(self) -> float: ...
90
@property
91
def microphoneCaptureState(self) -> WKMediaCaptureState: ...
92
@property
93
def cameraCaptureState(self) -> WKMediaCaptureState: ...
94
```
95
96
### Configuration
97
98
#### WKWebViewConfiguration
99
100
Configuration object that defines the behavior and capabilities of a WKWebView instance.
101
102
```python { .api }
103
class WKWebViewConfiguration:
104
def init(self) -> WKWebViewConfiguration: ...
105
106
# Content controllers
107
@property
108
def userContentController(self) -> WKUserContentController: ...
109
110
# Preferences
111
@property
112
def preferences(self) -> WKPreferences: ...
113
@property
114
def defaultWebpagePreferences(self) -> WKWebpagePreferences: ...
115
116
# Data storage
117
@property
118
def websiteDataStore(self) -> WKWebsiteDataStore: ...
119
120
# Process pool
121
@property
122
def processPool(self) -> WKProcessPool: ...
123
124
# Media configuration
125
@property
126
def mediaTypesRequiringUserAction(self) -> WKAudiovisualMediaTypes: ...
127
@property
128
def allowsInlineMediaPlayback(self) -> bool: ...
129
@property
130
def allowsAirPlayForMediaPlayback(self) -> bool: ...
131
@property
132
def allowsPictureInPictureMediaPlayback(self) -> bool: ...
133
134
# JavaScript configuration
135
@property
136
def suppressesIncrementalRendering(self) -> bool: ...
137
@property
138
def applicationNameForUserAgent(self) -> str: ...
139
140
# Selection
141
@property
142
def selectionGranularity(self) -> WKSelectionGranularity: ...
143
144
# Data detection
145
@property
146
def dataDetectorTypes(self) -> WKDataDetectorTypes: ...
147
148
# URL scheme handlers
149
def setURLSchemeHandler_forURLScheme_(self, urlSchemeHandler: WKURLSchemeHandler, urlScheme: str): ...
150
def urlSchemeHandlerForURLScheme_(self, urlScheme: str) -> WKURLSchemeHandler: ...
151
```
152
153
#### WKPreferences
154
155
Preferences object for configuring web view behavior.
156
157
```python { .api }
158
class WKPreferences:
159
def init(self) -> WKPreferences: ...
160
161
@property
162
def minimumFontSize(self) -> float: ...
163
@property
164
def javaScriptEnabled(self) -> bool: ...
165
@property
166
def javaScriptCanOpenWindowsAutomatically(self) -> bool: ...
167
@property
168
def isFraudulentWebsiteWarningEnabled(self) -> bool: ...
169
@property
170
def tabFocusesLinks(self) -> bool: ...
171
@property
172
def textInteractionEnabled(self) -> bool: ...
173
```
174
175
#### WKWebpagePreferences
176
177
Per-navigation preferences for web content.
178
179
```python { .api }
180
class WKWebpagePreferences:
181
def init(self) -> WKWebpagePreferences: ...
182
183
@property
184
def allowsContentJavaScript(self) -> bool: ...
185
@property
186
def preferredContentMode(self) -> WKContentMode: ...
187
```
188
189
### Process Management
190
191
#### WKProcessPool
192
193
Shared process pool for web content processes.
194
195
```python { .api }
196
class WKProcessPool:
197
def init(self) -> WKProcessPool: ...
198
```
199
200
### Security and Privacy
201
202
#### WKSecurityOrigin
203
204
Represents the security origin of web content.
205
206
```python { .api }
207
class WKSecurityOrigin:
208
@property
209
def protocol(self) -> str: ...
210
@property
211
def host(self) -> str: ...
212
@property
213
def port(self) -> int: ...
214
```
215
216
#### WKFrameInfo
217
218
Information about a web page frame.
219
220
```python { .api }
221
class WKFrameInfo:
222
@property
223
def isMainFrame(self) -> bool: ...
224
@property
225
def request(self) -> NSURLRequest: ...
226
@property
227
def securityOrigin(self) -> WKSecurityOrigin: ...
228
@property
229
def webView(self) -> WKWebView: ...
230
```
231
232
### Window Management
233
234
#### WKWindowFeatures
235
236
Window features for popup windows.
237
238
```python { .api }
239
class WKWindowFeatures:
240
@property
241
def menuBarVisibility(self) -> bool: ...
242
@property
243
def statusBarVisibility(self) -> bool: ...
244
@property
245
def toolbarsVisibility(self) -> bool: ...
246
@property
247
def allowsResizing(self) -> bool: ...
248
@property
249
def x(self) -> int: ...
250
@property
251
def y(self) -> int: ...
252
@property
253
def width(self) -> int: ...
254
@property
255
def height(self) -> int: ...
256
```
257
258
### Content Worlds
259
260
#### WKContentWorld
261
262
Isolated JavaScript execution environment.
263
264
```python { .api }
265
class WKContentWorld:
266
@classmethod
267
def pageWorld(cls) -> WKContentWorld: ...
268
@classmethod
269
def defaultClientWorld(cls) -> WKContentWorld: ...
270
@classmethod
271
def worldWithName_(cls, name: str) -> WKContentWorld: ...
272
273
@property
274
def name(self) -> str: ...
275
```
276
277
### Downloads
278
279
#### WKDownload
280
281
Represents a download operation initiated from a web view.
282
283
```python { .api }
284
class WKDownload:
285
@property
286
def originalRequest(self) -> NSURLRequest: ...
287
@property
288
def delegate(self) -> WKDownloadDelegate: ...
289
@property
290
def progress(self) -> NSProgress: ...
291
292
def cancel_(self, completionHandler: callable): ...
293
```
294
295
### Find in Page
296
297
#### WKFindConfiguration
298
299
Configuration for text search operations.
300
301
```python { .api }
302
class WKFindConfiguration:
303
def init(self) -> WKFindConfiguration: ...
304
305
@property
306
def backwards(self) -> bool: ...
307
@property
308
def caseSensitive(self) -> bool: ...
309
@property
310
def wraps(self) -> bool: ...
311
```
312
313
#### WKFindResult
314
315
Result of a text search operation.
316
317
```python { .api }
318
class WKFindResult:
319
@property
320
def matchFound(self) -> bool: ...
321
```
322
323
### PDF and Snapshot Generation
324
325
#### WKPDFConfiguration
326
327
Configuration for PDF generation.
328
329
```python { .api }
330
class WKPDFConfiguration:
331
def init(self) -> WKPDFConfiguration: ...
332
333
@property
334
def rect(self) -> CGRect: ...
335
```
336
337
#### WKSnapshotConfiguration
338
339
Configuration for taking screenshots of web content.
340
341
```python { .api }
342
class WKSnapshotConfiguration:
343
def init(self) -> WKSnapshotConfiguration: ...
344
345
@property
346
def rect(self) -> CGRect: ...
347
@property
348
def snapshotWidth(self) -> float: ...
349
@property
350
def afterScreenUpdates(self) -> bool: ...
351
```
352
353
### Permissions and Context
354
355
#### WKContextMenuElementInfo
356
357
Information about an element in a context menu.
358
359
```python { .api }
360
class WKContextMenuElementInfo:
361
@property
362
def linkURL(self) -> NSURL: ...
363
```
364
365
#### WKOpenPanelParameters
366
367
Parameters for file selection panels.
368
369
```python { .api }
370
class WKOpenPanelParameters:
371
@property
372
def allowsMultipleSelection(self) -> bool: ...
373
@property
374
def allowsDirectories(self) -> bool: ...
375
```
376
377
### Delegate Protocols
378
379
While these are protocol definitions in Objective-C, they represent the methods you need to implement in your delegate classes:
380
381
#### WKNavigationDelegate
382
383
```python { .api }
384
# Navigation delegate methods (implement in your delegate class)
385
def webView_decidePolicyForNavigationAction_decisionHandler_(self, webView: WKWebView, navigationAction: WKNavigationAction, decisionHandler: callable): ...
386
def webView_decidePolicyForNavigationResponse_decisionHandler_(self, webView: WKWebView, navigationResponse: WKNavigationResponse, decisionHandler: callable): ...
387
def webView_didStartProvisionalNavigation_(self, webView: WKWebView, navigation: WKNavigation): ...
388
def webView_didReceiveServerRedirectForProvisionalNavigation_(self, webView: WKWebView, navigation: WKNavigation): ...
389
def webView_didFailProvisionalNavigation_withError_(self, webView: WKWebView, navigation: WKNavigation, error: NSError): ...
390
def webView_didCommitNavigation_(self, webView: WKWebView, navigation: WKNavigation): ...
391
def webView_didFinishNavigation_(self, webView: WKWebView, navigation: WKNavigation): ...
392
def webView_didFailNavigation_withError_(self, webView: WKWebView, navigation: WKNavigation, error: NSError): ...
393
def webView_didReceiveAuthenticationChallenge_completionHandler_(self, webView: WKWebView, challenge: NSURLAuthenticationChallenge, completionHandler: callable): ...
394
def webViewWebContentProcessDidTerminate_(self, webView: WKWebView): ...
395
```
396
397
#### WKUIDelegate
398
399
```python { .api }
400
# UI delegate methods (implement in your delegate class)
401
def webView_createWebViewWithConfiguration_forNavigationAction_windowFeatures_(self, webView: WKWebView, configuration: WKWebViewConfiguration, navigationAction: WKNavigationAction, windowFeatures: WKWindowFeatures) -> WKWebView: ...
402
def webViewDidClose_(self, webView: WKWebView): ...
403
def webView_runJavaScriptAlertPanelWithMessage_initiatedByFrame_completionHandler_(self, webView: WKWebView, message: str, frame: WKFrameInfo, completionHandler: callable): ...
404
def webView_runJavaScriptConfirmPanelWithMessage_initiatedByFrame_completionHandler_(self, webView: WKWebView, message: str, frame: WKFrameInfo, completionHandler: callable): ...
405
def webView_runJavaScriptTextInputPanelWithPrompt_defaultText_initiatedByFrame_completionHandler_(self, webView: WKWebView, prompt: str, defaultText: str, frame: WKFrameInfo, completionHandler: callable): ...
406
def webView_contextMenuConfigurationForElement_completionHandler_(self, webView: WKWebView, elementInfo: WKContextMenuElementInfo, completionHandler: callable): ...
407
def webView_contextMenuWillPresentForElement_(self, webView: WKWebView, elementInfo: WKContextMenuElementInfo): ...
408
def webView_contextMenuForElement_willCommitWithAnimator_(self, webView: WKWebView, elementInfo: WKContextMenuElementInfo, animator: callable): ...
409
def webView_contextMenuDidEndForElement_(self, webView: WKWebView, elementInfo: WKContextMenuElementInfo): ...
410
```
411
412
#### WKDownloadDelegate
413
414
```python { .api }
415
# Download delegate methods (implement in your delegate class)
416
def download_decideDestinationUsingResponse_suggestedFilename_completionHandler_(self, download: WKDownload, response: NSURLResponse, suggestedFilename: str, completionHandler: callable): ...
417
def download_willPerformHTTPRedirection_newRequest_decisionHandler_(self, download: WKDownload, response: NSHTTPURLResponse, request: NSURLRequest, decisionHandler: callable): ...
418
def download_didReceiveAuthenticationChallenge_completionHandler_(self, download: WKDownload, challenge: NSURLAuthenticationChallenge, completionHandler: callable): ...
419
def download_didWriteData_totalBytesWritten_totalBytesExpectedToWrite_(self, download: WKDownload, bytesWritten: int, totalBytesWritten: int, totalBytesExpectedToWrite: int): ...
420
def downloadDidFinish_(self, download: WKDownload): ...
421
def download_didFailWithError_resumeData_(self, download: WKDownload, error: NSError, resumeData: NSData): ...
422
```
423
424
### Usage Examples
425
426
#### Creating a Basic Web View
427
428
```python
429
import WebKit
430
from Foundation import NSURL, NSRect
431
from AppKit import NSApplication, NSWindow
432
433
# Create configuration
434
config = WebKit.WKWebViewConfiguration.alloc().init()
435
436
# Create web view
437
frame = NSRect((0, 0), (800, 600))
438
web_view = WebKit.WKWebView.alloc().initWithFrame_configuration_(frame, config)
439
440
# Load URL
441
url = NSURL.URLWithString_("https://www.apple.com")
442
request = Foundation.NSURLRequest.requestWithURL_(url)
443
web_view.loadRequest_(request)
444
```
445
446
#### Executing JavaScript
447
448
```python
449
def handle_js_result(result, error):
450
if error:
451
print(f"JavaScript error: {error}")
452
else:
453
print(f"Result: {result}")
454
455
# Execute JavaScript
456
web_view.evaluateJavaScript_completionHandler_(
457
"document.title",
458
handle_js_result
459
)
460
```
461
462
#### Implementing Navigation Delegate
463
464
```python
465
class MyNavigationDelegate:
466
def webView_didFinishNavigation_(self, web_view, navigation):
467
print(f"Finished loading: {web_view.title()}")
468
469
def webView_decidePolicyForNavigationAction_decisionHandler_(self, web_view, navigation_action, decision_handler):
470
# Allow all navigation
471
decision_handler(WebKit.WKNavigationActionPolicyAllow)
472
473
delegate = MyNavigationDelegate()
474
web_view.setNavigationDelegate_(delegate)
475
```