forked from react-native-webview/react-native-webview
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRNCWebView.mm
More file actions
547 lines (497 loc) · 28.8 KB
/
RNCWebView.mm
File metadata and controls
547 lines (497 loc) · 28.8 KB
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
// This guard prevent the code from being compiled in the old architecture
#ifdef RCT_NEW_ARCH_ENABLED
#import "RNCWebView.h"
#import "RNCWebViewImpl.h"
#import <react/renderer/components/RNCWebViewSpec/ComponentDescriptors.h>
#import <react/renderer/components/RNCWebViewSpec/EventEmitters.h>
#import <react/renderer/components/RNCWebViewSpec/Props.h>
#import <react/renderer/components/RNCWebViewSpec/RCTComponentViewHelpers.h>
#import <React/RCTFabricComponentsPlugins.h>
using namespace facebook::react;
auto stringToOnShouldStartLoadWithRequestNavigationTypeEnum(std::string value) {
if (value == "click") return RNCWebViewEventEmitter::OnShouldStartLoadWithRequestNavigationType::Click;
if (value == "formsubmit") return RNCWebViewEventEmitter::OnShouldStartLoadWithRequestNavigationType::Formsubmit;
if (value == "backforward") return RNCWebViewEventEmitter::OnShouldStartLoadWithRequestNavigationType::Backforward;
if (value == "reload") return RNCWebViewEventEmitter::OnShouldStartLoadWithRequestNavigationType::Reload;
if (value == "formresubmit") return RNCWebViewEventEmitter::OnShouldStartLoadWithRequestNavigationType::Formresubmit;
return RNCWebViewEventEmitter::OnShouldStartLoadWithRequestNavigationType::Other;
}
auto stringToOnLoadingStartNavigationTypeEnum(std::string value) {
if (value == "click") return RNCWebViewEventEmitter::OnLoadingStartNavigationType::Click;
if (value == "formsubmit") return RNCWebViewEventEmitter::OnLoadingStartNavigationType::Formsubmit;
if (value == "backforward") return RNCWebViewEventEmitter::OnLoadingStartNavigationType::Backforward;
if (value == "reload") return RNCWebViewEventEmitter::OnLoadingStartNavigationType::Reload;
if (value == "formresubmit") return RNCWebViewEventEmitter::OnLoadingStartNavigationType::Formresubmit;
return RNCWebViewEventEmitter::OnLoadingStartNavigationType::Other;
}
auto stringToOnLoadingFinishNavigationTypeEnum(std::string value) {
if (value == "click") return RNCWebViewEventEmitter::OnLoadingFinishNavigationType::Click;
if (value == "formsubmit") return RNCWebViewEventEmitter::OnLoadingFinishNavigationType::Formsubmit;
if (value == "backforward") return RNCWebViewEventEmitter::OnLoadingFinishNavigationType::Backforward;
if (value == "reload") return RNCWebViewEventEmitter::OnLoadingFinishNavigationType::Reload;
if (value == "formresubmit") return RNCWebViewEventEmitter::OnLoadingFinishNavigationType::Formresubmit;
return RNCWebViewEventEmitter::OnLoadingFinishNavigationType::Other;
}
@interface RNCWebView () <RCTRNCWebViewViewProtocol>
@end
@implementation RNCWebView {
RNCWebViewImpl * _view;
}
+ (ComponentDescriptorProvider)componentDescriptorProvider
{
return concreteComponentDescriptorProvider<RNCWebViewComponentDescriptor>();
}
#if !TARGET_OS_OSX
// Reproduce the idea from here: https://github.com/facebook/react-native/blob/8bd3edec88148d0ab1f225d2119435681fbbba33/React/Fabric/Mounting/ComponentViews/InputAccessory/RCTInputAccessoryComponentView.mm#L142
- (void)prepareForRecycle {
[super prepareForRecycle];
[_view destroyWebView];
}
#endif // !TARGET_OS_OSX
// Needed because of this: https://github.com/facebook/react-native/pull/37274
+ (void)load
{
[super load];
}
- (instancetype)initWithFrame:(CGRect)frame
{
if (self = [super initWithFrame:frame]) {
static const auto defaultProps = std::make_shared<const RNCWebViewProps>();
_props = defaultProps;
_view = [[RNCWebViewImpl alloc] init];
_view.onShouldStartLoadWithRequest = [self](NSDictionary* dictionary) {
if (_eventEmitter) {
auto webViewEventEmitter = std::static_pointer_cast<RNCWebViewEventEmitter const>(_eventEmitter);
facebook::react::RNCWebViewEventEmitter::OnShouldStartLoadWithRequest data = {
.url = std::string([[dictionary valueForKey:@"url"] UTF8String]),
.lockIdentifier = [[dictionary valueForKey:@"lockIdentifier"] doubleValue],
.title = std::string([[dictionary valueForKey:@"title"] UTF8String]),
.navigationType = stringToOnShouldStartLoadWithRequestNavigationTypeEnum(std::string([[dictionary valueForKey:@"navigationType"] UTF8String])),
.canGoBack = static_cast<bool>([[dictionary valueForKey:@"canGoBack"] boolValue]),
.canGoForward = static_cast<bool>([[dictionary valueForKey:@"canGoForward"] boolValue]),
.isTopFrame = static_cast<bool>([[dictionary valueForKey:@"isTopFrame"] boolValue]),
.loading = static_cast<bool>([[dictionary valueForKey:@"loading"] boolValue]),
.mainDocumentURL = std::string([[dictionary valueForKey:@"mainDocumentURL"] UTF8String])
};
webViewEventEmitter->onShouldStartLoadWithRequest(data);
};
};
_view.onLoadingStart = [self](NSDictionary* dictionary) {
if (_eventEmitter) {
auto webViewEventEmitter = std::static_pointer_cast<RNCWebViewEventEmitter const>(_eventEmitter);
facebook::react::RNCWebViewEventEmitter::OnLoadingStart data = {
.url = std::string([[dictionary valueForKey:@"url"] UTF8String]),
.lockIdentifier = [[dictionary valueForKey:@"lockIdentifier"] doubleValue],
.title = std::string([[dictionary valueForKey:@"title"] UTF8String]),
.navigationType = stringToOnLoadingStartNavigationTypeEnum(std::string([[dictionary valueForKey:@"navigationType"] UTF8String])),
.canGoBack = static_cast<bool>([[dictionary valueForKey:@"canGoBack"] boolValue]),
.canGoForward = static_cast<bool>([[dictionary valueForKey:@"canGoForward"] boolValue]),
.loading = static_cast<bool>([[dictionary valueForKey:@"loading"] boolValue]),
.mainDocumentURL = std::string([[dictionary valueForKey:@"mainDocumentURL"] UTF8String], [[dictionary valueForKey:@"mainDocumentURL"] lengthOfBytesUsingEncoding:NSUTF8StringEncoding])
};
webViewEventEmitter->onLoadingStart(data);
}
};
_view.onLoadingError = [self](NSDictionary* dictionary) {
if (_eventEmitter) {
auto webViewEventEmitter = std::static_pointer_cast<RNCWebViewEventEmitter const>(_eventEmitter);
facebook::react::RNCWebViewEventEmitter::OnLoadingError data = {
.url = std::string([[dictionary valueForKey:@"url"] UTF8String]),
.lockIdentifier = [[dictionary valueForKey:@"lockIdentifier"] doubleValue],
.title = std::string([[dictionary valueForKey:@"title"] UTF8String]),
.code = [[dictionary valueForKey:@"code"] intValue],
.description = std::string([[dictionary valueForKey:@"description"] UTF8String] ?: ""),
.canGoBack = static_cast<bool>([[dictionary valueForKey:@"canGoBack"] boolValue]),
.canGoForward = static_cast<bool>([[dictionary valueForKey:@"canGoForward"] boolValue]),
.loading = static_cast<bool>([[dictionary valueForKey:@"loading"] boolValue]),
.domain = std::string([[dictionary valueForKey:@"domain"] UTF8String])
};
webViewEventEmitter->onLoadingError(data);
}
};
_view.onMessage = [self](NSDictionary* dictionary) {
if (_eventEmitter) {
auto webViewEventEmitter = std::static_pointer_cast<RNCWebViewEventEmitter const>(_eventEmitter);
facebook::react::RNCWebViewEventEmitter::OnMessage data = {
.url = std::string([[dictionary valueForKey:@"url"] UTF8String]),
.lockIdentifier = [[dictionary valueForKey:@"lockIdentifier"] doubleValue],
.title = std::string([[dictionary valueForKey:@"title"] UTF8String]),
.canGoBack = static_cast<bool>([[dictionary valueForKey:@"canGoBack"] boolValue]),
.canGoForward = static_cast<bool>([[dictionary valueForKey:@"canGoForward"] boolValue]),
.loading = static_cast<bool>([[dictionary valueForKey:@"loading"] boolValue]),
.data = std::string([[dictionary valueForKey:@"data"] UTF8String])
};
webViewEventEmitter->onMessage(data);
}
};
_view.onLoadingFinish = [self](NSDictionary* dictionary) {
if (_eventEmitter) {
auto webViewEventEmitter = std::static_pointer_cast<RNCWebViewEventEmitter const>(_eventEmitter);
facebook::react::RNCWebViewEventEmitter::OnLoadingFinish data = {
.url = std::string([[dictionary valueForKey:@"url"] UTF8String]),
.lockIdentifier = [[dictionary valueForKey:@"lockIdentifier"] doubleValue],
.title = std::string([[dictionary valueForKey:@"title"] UTF8String]),
.navigationType = stringToOnLoadingFinishNavigationTypeEnum(std::string([[dictionary valueForKey:@"navigationType"] UTF8String], [[dictionary valueForKey:@"navigationType"] lengthOfBytesUsingEncoding:NSUTF8StringEncoding])),
.canGoBack = static_cast<bool>([[dictionary valueForKey:@"canGoBack"] boolValue]),
.canGoForward = static_cast<bool>([[dictionary valueForKey:@"canGoForward"] boolValue]),
.loading = static_cast<bool>([[dictionary valueForKey:@"loading"] boolValue]),
.mainDocumentURL = std::string([[dictionary valueForKey:@"mainDocumentURL"] UTF8String], [[dictionary valueForKey:@"mainDocumentURL"] lengthOfBytesUsingEncoding:NSUTF8StringEncoding])
};
webViewEventEmitter->onLoadingFinish(data);
}
};
_view.onLoadingProgress = [self](NSDictionary* dictionary) {
if (_eventEmitter) {
auto webViewEventEmitter = std::static_pointer_cast<RNCWebViewEventEmitter const>(_eventEmitter);
facebook::react::RNCWebViewEventEmitter::OnLoadingProgress data = {
.url = std::string([[dictionary valueForKey:@"url"] UTF8String]),
.lockIdentifier = [[dictionary valueForKey:@"lockIdentifier"] doubleValue],
.title = std::string([[dictionary valueForKey:@"title"] UTF8String]),
.canGoBack = static_cast<bool>([[dictionary valueForKey:@"canGoBack"] boolValue]),
.canGoForward = static_cast<bool>([[dictionary valueForKey:@"canGoForward"] boolValue]),
.loading = static_cast<bool>([[dictionary valueForKey:@"loading"] boolValue]),
.progress = [[dictionary valueForKey:@"progress"] doubleValue]
};
webViewEventEmitter->onLoadingProgress(data);
}
};
_view.onContentProcessDidTerminate = [self](NSDictionary* dictionary) {
if (_eventEmitter) {
auto webViewEventEmitter = std::static_pointer_cast<RNCWebViewEventEmitter const>(_eventEmitter);
facebook::react::RNCWebViewEventEmitter::OnContentProcessDidTerminate data = {
.url = std::string([[dictionary valueForKey:@"url"] UTF8String]),
.lockIdentifier = [[dictionary valueForKey:@"lockIdentifier"] doubleValue],
.title = std::string([[dictionary valueForKey:@"title"] UTF8String]),
.canGoBack = static_cast<bool>([[dictionary valueForKey:@"canGoBack"] boolValue]),
.canGoForward = static_cast<bool>([[dictionary valueForKey:@"canGoForward"] boolValue]),
.loading = static_cast<bool>([[dictionary valueForKey:@"loading"] boolValue])
};
webViewEventEmitter->onContentProcessDidTerminate(data);
}
};
_view.onCustomMenuSelection = [self](NSDictionary* dictionary) {
if (_eventEmitter) {
auto webViewEventEmitter = std::static_pointer_cast<RNCWebViewEventEmitter const>(_eventEmitter);
facebook::react::RNCWebViewEventEmitter::OnCustomMenuSelection data = {
.selectedText = std::string([[dictionary valueForKey:@"selectedText"] UTF8String]),
.key = std::string([[dictionary valueForKey:@"key"] UTF8String]),
.label = std::string([[dictionary valueForKey:@"label"] UTF8String])
};
webViewEventEmitter->onCustomMenuSelection(data);
}
};
_view.onScroll = [self](NSDictionary* dictionary) {
if (_eventEmitter) {
NSDictionary* contentOffset = [dictionary valueForKey:@"contentOffset"];
NSDictionary* contentInset = [dictionary valueForKey:@"contentInset"];
NSDictionary* contentSize = [dictionary valueForKey:@"contentSize"];
NSDictionary* layoutMeasurement = [dictionary valueForKey:@"layoutMeasurement"];
double zoomScale = [[dictionary valueForKey:@"zoomScale"] doubleValue];
auto webViewEventEmitter = std::static_pointer_cast<RNCWebViewEventEmitter const>(_eventEmitter);
facebook::react::RNCWebViewEventEmitter::OnScroll data = {
.contentOffset = {
.x = [[contentOffset valueForKey:@"x"] doubleValue],
.y = [[contentOffset valueForKey:@"y"] doubleValue]
},
.contentInset = {
.left = [[contentInset valueForKey:@"left"] doubleValue],
.right = [[contentInset valueForKey:@"right"] doubleValue],
.top = [[contentInset valueForKey:@"top"] doubleValue],
.bottom = [[contentInset valueForKey:@"bottom"] doubleValue]
},
.contentSize = {
.width = [[contentSize valueForKey:@"width"] doubleValue],
.height = [[contentSize valueForKey:@"height"] doubleValue]
},
.layoutMeasurement = {
.width = [[layoutMeasurement valueForKey:@"width"] doubleValue],
.height = [[layoutMeasurement valueForKey:@"height"] doubleValue] },
.zoomScale = zoomScale
};
webViewEventEmitter->onScroll(data);
}
};
_view.onHttpError = [self](NSDictionary* dictionary) {
if (_eventEmitter) {
auto webViewEventEmitter = std::static_pointer_cast<RNCWebViewEventEmitter const>(_eventEmitter);
facebook::react::RNCWebViewEventEmitter::OnHttpError data = {
.url = std::string([[dictionary valueForKey:@"url"] UTF8String]),
.lockIdentifier = [[dictionary valueForKey:@"lockIdentifier"] doubleValue],
.title = std::string([[dictionary valueForKey:@"title"] UTF8String]),
.statusCode = [[dictionary valueForKey:@"statusCode"] intValue],
.description = std::string([[dictionary valueForKey:@"description"] UTF8String] ?: ""),
.canGoBack = static_cast<bool>([[dictionary valueForKey:@"canGoBack"] boolValue]),
.canGoForward = static_cast<bool>([[dictionary valueForKey:@"canGoForward"] boolValue]),
.loading = static_cast<bool>([[dictionary valueForKey:@"loading"] boolValue])
};
webViewEventEmitter->onHttpError(data);
}
};
self.contentView = _view;
}
return self;
}
- (void)updateEventEmitter:(EventEmitter::Shared const &)eventEmitter
{
[super updateEventEmitter:eventEmitter];
}
- (void)updateProps:(Props::Shared const &)props oldProps:(Props::Shared const &)oldProps
{
const auto &oldViewProps = *std::static_pointer_cast<RNCWebViewProps const>(_props);
const auto &newViewProps = *std::static_pointer_cast<RNCWebViewProps const>(props);
#define REMAP_WEBVIEW_PROP(name) \
if (oldViewProps.name != newViewProps.name) { \
_view.name = newViewProps.name; \
}
#define REMAP_WEBVIEW_STRING_PROP(name) \
if (oldViewProps.name != newViewProps.name) { \
_view.name = RCTNSStringFromString(newViewProps.name); \
}
REMAP_WEBVIEW_PROP(scrollEnabled)
REMAP_WEBVIEW_STRING_PROP(injectedJavaScript)
REMAP_WEBVIEW_STRING_PROP(injectedJavaScriptBeforeContentLoaded)
REMAP_WEBVIEW_PROP(injectedJavaScriptForMainFrameOnly)
REMAP_WEBVIEW_PROP(injectedJavaScriptBeforeContentLoadedForMainFrameOnly)
REMAP_WEBVIEW_STRING_PROP(injectedJavaScriptObject)
REMAP_WEBVIEW_PROP(javaScriptEnabled)
REMAP_WEBVIEW_PROP(javaScriptCanOpenWindowsAutomatically)
REMAP_WEBVIEW_PROP(suppressJavaScriptDialogs)
REMAP_WEBVIEW_PROP(allowFileAccessFromFileURLs)
REMAP_WEBVIEW_PROP(allowUniversalAccessFromFileURLs)
REMAP_WEBVIEW_PROP(allowsInlineMediaPlayback)
REMAP_WEBVIEW_PROP(allowsPictureInPictureMediaPlayback)
REMAP_WEBVIEW_PROP(webviewDebuggingEnabled)
REMAP_WEBVIEW_PROP(allowsAirPlayForMediaPlayback)
REMAP_WEBVIEW_PROP(mediaPlaybackRequiresUserAction)
REMAP_WEBVIEW_PROP(automaticallyAdjustContentInsets)
REMAP_WEBVIEW_PROP(autoManageStatusBarEnabled)
REMAP_WEBVIEW_PROP(hideKeyboardAccessoryView)
REMAP_WEBVIEW_PROP(allowsBackForwardNavigationGestures)
REMAP_WEBVIEW_PROP(incognito)
REMAP_WEBVIEW_PROP(pagingEnabled)
REMAP_WEBVIEW_STRING_PROP(applicationNameForUserAgent)
REMAP_WEBVIEW_PROP(cacheEnabled)
REMAP_WEBVIEW_PROP(allowsLinkPreview)
REMAP_WEBVIEW_STRING_PROP(allowingReadAccessToURL)
REMAP_WEBVIEW_PROP(messagingEnabled)
#if !TARGET_OS_OSX
REMAP_WEBVIEW_PROP(fraudulentWebsiteWarningEnabled)
#endif // !TARGET_OS_OSX
REMAP_WEBVIEW_PROP(enableApplePay)
REMAP_WEBVIEW_PROP(pullToRefreshEnabled)
REMAP_WEBVIEW_PROP(refreshControlLightMode)
REMAP_WEBVIEW_PROP(bounces)
REMAP_WEBVIEW_PROP(useSharedProcessPool)
REMAP_WEBVIEW_STRING_PROP(userAgent)
REMAP_WEBVIEW_PROP(sharedCookiesEnabled)
#if !TARGET_OS_OSX
REMAP_WEBVIEW_PROP(decelerationRate)
#endif // !TARGET_OS_OSX
REMAP_WEBVIEW_PROP(directionalLockEnabled)
REMAP_WEBVIEW_PROP(showsHorizontalScrollIndicator)
REMAP_WEBVIEW_PROP(showsVerticalScrollIndicator)
REMAP_WEBVIEW_PROP(keyboardDisplayRequiresUserAction)
#if defined(__IPHONE_OS_VERSION_MAX_ALLOWED) && __IPHONE_OS_VERSION_MAX_ALLOWED >= 130000 /* __IPHONE_13_0 */
REMAP_WEBVIEW_PROP(automaticallyAdjustContentInsets)
#endif
#if defined(__IPHONE_OS_VERSION_MAX_ALLOWED) && __IPHONE_OS_VERSION_MAX_ALLOWED >= 140000 /* iOS 14 */
REMAP_WEBVIEW_PROP(limitsNavigationsToAppBoundDomains)
#endif
#if defined(__IPHONE_OS_VERSION_MAX_ALLOWED) && __IPHONE_OS_VERSION_MAX_ALLOWED >= 140500 /* iOS 14.5 */
REMAP_WEBVIEW_PROP(textInteractionEnabled)
#endif
#if !TARGET_OS_OSX
if (oldViewProps.dataDetectorTypes != newViewProps.dataDetectorTypes) {
WKDataDetectorTypes dataDetectorTypes = WKDataDetectorTypeNone;
if (dataDetectorTypes & RNCWebViewDataDetectorTypes::Address) {
dataDetectorTypes |= WKDataDetectorTypeAddress;
} else if (dataDetectorTypes & RNCWebViewDataDetectorTypes::Link) {
dataDetectorTypes |= WKDataDetectorTypeLink;
} else if (dataDetectorTypes & RNCWebViewDataDetectorTypes::CalendarEvent) {
dataDetectorTypes |= WKDataDetectorTypeCalendarEvent;
} else if (dataDetectorTypes & RNCWebViewDataDetectorTypes::TrackingNumber) {
dataDetectorTypes |= WKDataDetectorTypeTrackingNumber;
} else if (dataDetectorTypes & RNCWebViewDataDetectorTypes::FlightNumber) {
dataDetectorTypes |= WKDataDetectorTypeFlightNumber;
} else if (dataDetectorTypes & RNCWebViewDataDetectorTypes::LookupSuggestion) {
dataDetectorTypes |= WKDataDetectorTypeLookupSuggestion;
} else if (dataDetectorTypes & RNCWebViewDataDetectorTypes::PhoneNumber) {
dataDetectorTypes |= WKDataDetectorTypePhoneNumber;
} else if (dataDetectorTypes & RNCWebViewDataDetectorTypes::All) {
dataDetectorTypes |= WKDataDetectorTypeAll;
} else if (dataDetectorTypes & RNCWebViewDataDetectorTypes::None) {
dataDetectorTypes = WKDataDetectorTypeNone;
}
[_view setDataDetectorTypes:dataDetectorTypes];
}
#endif // !TARGET_OS_OSX
if (oldViewProps.contentInset.top != newViewProps.contentInset.top || oldViewProps.contentInset.left != newViewProps.contentInset.left || oldViewProps.contentInset.right != newViewProps.contentInset.right || oldViewProps.contentInset.bottom != newViewProps.contentInset.bottom) {
UIEdgeInsets edgesInsets = {
.top = newViewProps.contentInset.top,
.left = newViewProps.contentInset.left,
.right = newViewProps.contentInset.right,
.bottom = newViewProps.contentInset.bottom
};
[_view setContentInset: edgesInsets];
}
if (oldViewProps.basicAuthCredential.username != newViewProps.basicAuthCredential.username || oldViewProps.basicAuthCredential.password != newViewProps.basicAuthCredential.password) {
[_view setBasicAuthCredential: @{
@"username": RCTNSStringFromString(newViewProps.basicAuthCredential.username),
@"password": RCTNSStringFromString(newViewProps.basicAuthCredential.password)
}];
}
#if !TARGET_OS_OSX
if (oldViewProps.contentInsetAdjustmentBehavior != newViewProps.contentInsetAdjustmentBehavior) {
if (newViewProps.contentInsetAdjustmentBehavior == RNCWebViewContentInsetAdjustmentBehavior::Never) {
[_view setContentInsetAdjustmentBehavior: UIScrollViewContentInsetAdjustmentNever];
} else if (newViewProps.contentInsetAdjustmentBehavior == RNCWebViewContentInsetAdjustmentBehavior::Automatic) {
[_view setContentInsetAdjustmentBehavior: UIScrollViewContentInsetAdjustmentAutomatic];
} else if (newViewProps.contentInsetAdjustmentBehavior == RNCWebViewContentInsetAdjustmentBehavior::ScrollableAxes) {
[_view setContentInsetAdjustmentBehavior: UIScrollViewContentInsetAdjustmentScrollableAxes];
} else if (newViewProps.contentInsetAdjustmentBehavior == RNCWebViewContentInsetAdjustmentBehavior::Always) {
[_view setContentInsetAdjustmentBehavior: UIScrollViewContentInsetAdjustmentAlways];
}
}
#endif // !TARGET_OS_OSX
if (oldViewProps.menuItems != newViewProps.menuItems) {
NSMutableArray *newMenuItems = [NSMutableArray array];
for (const auto &menuItem: newViewProps.menuItems) {
[newMenuItems addObject:@{
@"key": RCTNSStringFromString(menuItem.key),
@"label": RCTNSStringFromString(menuItem.label),
}];
}
[_view setMenuItems:newMenuItems];
}
if(oldViewProps.suppressMenuItems != newViewProps.suppressMenuItems) {
NSMutableArray *suppressMenuItems = [NSMutableArray array];
for (const auto &menuItem: newViewProps.suppressMenuItems) {
[suppressMenuItems addObject: RCTNSStringFromString(menuItem)];
}
[_view setSuppressMenuItems:suppressMenuItems];
}
if (oldViewProps.hasOnFileDownload != newViewProps.hasOnFileDownload) {
if (newViewProps.hasOnFileDownload) {
_view.onFileDownload = [self](NSDictionary* dictionary) {
if (_eventEmitter) {
auto webViewEventEmitter = std::static_pointer_cast<RNCWebViewEventEmitter const>(_eventEmitter);
facebook::react::RNCWebViewEventEmitter::OnFileDownload data = {
.downloadUrl = std::string([[dictionary valueForKey:@"downloadUrl"] UTF8String])
};
webViewEventEmitter->onFileDownload(data);
}
};
} else {
_view.onFileDownload = nil;
}
}
if (oldViewProps.hasOnOpenWindowEvent != newViewProps.hasOnOpenWindowEvent) {
if (newViewProps.hasOnOpenWindowEvent) {
_view.onOpenWindow = [self](NSDictionary* dictionary) {
if (_eventEmitter) {
auto webViewEventEmitter = std::static_pointer_cast<RNCWebViewEventEmitter const>(_eventEmitter);
facebook::react::RNCWebViewEventEmitter::OnOpenWindow data = {
.targetUrl = std::string([[dictionary valueForKey:@"targetUrl"] UTF8String])
};
webViewEventEmitter->onOpenWindow(data);
}
};
} else {
_view.onOpenWindow = nil;
}
}
//
#if defined(__IPHONE_OS_VERSION_MAX_ALLOWED) && __IPHONE_OS_VERSION_MAX_ALLOWED >= 130000 /* iOS 13 */
if (oldViewProps.contentMode != newViewProps.contentMode) {
if (newViewProps.contentMode == RNCWebViewContentMode::Recommended) {
[_view setContentMode: WKContentModeRecommended];
} else if (newViewProps.contentMode == RNCWebViewContentMode::Mobile) {
[_view setContentMode:WKContentModeMobile];
} else if (newViewProps.contentMode == RNCWebViewContentMode::Desktop) {
[_view setContentMode:WKContentModeDesktop];
}
}
#endif
#if defined(__IPHONE_OS_VERSION_MAX_ALLOWED) && __IPHONE_OS_VERSION_MAX_ALLOWED >= 150000 /* iOS 15 */
if (oldViewProps.mediaCapturePermissionGrantType != newViewProps.mediaCapturePermissionGrantType) {
if (newViewProps.mediaCapturePermissionGrantType == RNCWebViewMediaCapturePermissionGrantType::Prompt) {
[_view setMediaCapturePermissionGrantType:RNCWebViewPermissionGrantType_Prompt];
} else if (newViewProps.mediaCapturePermissionGrantType == RNCWebViewMediaCapturePermissionGrantType::Grant) {
[_view setMediaCapturePermissionGrantType:RNCWebViewPermissionGrantType_Grant];
} else if (newViewProps.mediaCapturePermissionGrantType == RNCWebViewMediaCapturePermissionGrantType::Deny) {
[_view setMediaCapturePermissionGrantType:RNCWebViewPermissionGrantType_Deny];
}else if (newViewProps.mediaCapturePermissionGrantType == RNCWebViewMediaCapturePermissionGrantType::GrantIfSameHostElsePrompt) {
[_view setMediaCapturePermissionGrantType:RNCWebViewPermissionGrantType_GrantIfSameHost_ElsePrompt];
}else if (newViewProps.mediaCapturePermissionGrantType == RNCWebViewMediaCapturePermissionGrantType::GrantIfSameHostElseDeny) {
[_view setMediaCapturePermissionGrantType:RNCWebViewPermissionGrantType_GrantIfSameHost_ElseDeny];
}
}
#endif
NSMutableDictionary* source = [[NSMutableDictionary alloc] init];
if (!newViewProps.newSource.uri.empty()) {
[source setValue:RCTNSStringFromString(newViewProps.newSource.uri) forKey:@"uri"];
}
NSMutableDictionary* headers = [[NSMutableDictionary alloc] init];
for (auto & element : newViewProps.newSource.headers) {
[headers setValue:RCTNSStringFromString(element.value) forKey:RCTNSStringFromString(element.name)];
}
if (headers.count > 0) {
[source setObject:headers forKey:@"headers"];
}
if (!newViewProps.newSource.baseUrl.empty()) {
[source setValue:RCTNSStringFromString(newViewProps.newSource.baseUrl) forKey:@"baseUrl"];
}
if (!newViewProps.newSource.body.empty()) {
[source setValue:RCTNSStringFromString(newViewProps.newSource.body) forKey:@"body"];
}
if (!newViewProps.newSource.html.empty()) {
[source setValue:RCTNSStringFromString(newViewProps.newSource.html) forKey:@"html"];
}
if (!newViewProps.newSource.method.empty()) {
[source setValue:RCTNSStringFromString(newViewProps.newSource.method) forKey:@"method"];
}
[_view setSource:source];
[super updateProps:props oldProps:oldProps];
}
- (void)handleCommand:(nonnull const NSString *)commandName args:(nonnull const NSArray *)args {
RCTRNCWebViewHandleCommand(self, commandName, args);
}
Class<RCTComponentViewProtocol> RNCWebViewCls(void)
{
return RNCWebView.class;
}
- (void)goBack {
[_view goBack];
}
- (void)goForward {
[_view goForward];
}
- (void)injectJavaScript:(nonnull NSString *)javascript {
[_view injectJavaScript:javascript];
}
- (void)loadUrl:(nonnull NSString *)url {
// android only
}
- (void)postMessage:(nonnull NSString *)data {
[_view postMessage:data];
}
- (void)reload {
[_view reload];
}
- (void)requestFocus {
[_view requestFocus];
}
- (void)stopLoading {
[_view stopLoading];
}
- (void)clearFormData {
// android only
}
- (void)clearCache:(BOOL)includeDiskFiles {
// android only
}
- (void)clearHistory {
// android only
}
@end
#endif