Skip to content
This repository has been archived by the owner on May 12, 2022. It is now read-only.

Commit

Permalink
Fix handling hash change on iOS
Browse files Browse the repository at this point in the history
  • Loading branch information
andrepimenta authored Jun 12, 2020
1 parent 408df17 commit 3323d77
Showing 1 changed file with 41 additions and 3 deletions.
44 changes: 41 additions & 3 deletions ios/RNCWebView.m
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

static NSTimer *keyboardTimer;
static NSString *const MessageHandlerName = @"ReactNativeWebView";
static NSString *const HistoryShimName = @"ReactNativeHistoryShim";
static NSURLCredential* clientAuthenticationCredential;

// runtime trick to remove WKWebView keyboard default toolbar
Expand Down Expand Up @@ -130,6 +131,32 @@ - (void)didMoveToWindow
}
wkWebViewConfig.userContentController = [WKUserContentController new];

// Shim the HTML5 history API:
[wkWebViewConfig.userContentController addScriptMessageHandler:self name:HistoryShimName];
NSString *source = [NSString stringWithFormat:
@"(function(history) {\n"
" function notify(type) {\n"
" setTimeout(function() {\n"
" window.webkit.messageHandlers.%@.postMessage(type)\n"
" }, 0)\n"
" }\n"
" function shim(f) {\n"
" return function pushState() {\n"
" notify('other')\n"
" return f.apply(history, arguments)\n"
" }\n"
" }\n"
" history.pushState = shim(history.pushState)\n"
" history.replaceState = shim(history.replaceState)\n"
" window.addEventListener('popstate', function() {\n"
" notify('backforward')\n"
" })\n"
"})(window.history)\n", HistoryShimName
];
WKUserScript *script = [[WKUserScript alloc] initWithSource:source injectionTime:WKUserScriptInjectionTimeAtDocumentStart forMainFrameOnly:YES];
[wkWebViewConfig.userContentController addUserScript:script];


if (_messagingEnabled) {
[wkWebViewConfig.userContentController addScriptMessageHandler:self name:MessageHandlerName];

Expand Down Expand Up @@ -372,10 +399,21 @@ - (void)setContentInsetAdjustmentBehavior:(UIScrollViewContentInsetAdjustmentBeh
- (void)userContentController:(WKUserContentController *)userContentController
didReceiveScriptMessage:(WKScriptMessage *)message
{
if (_onMessage != nil) {
if ([message.name isEqualToString:HistoryShimName]) {
NSMutableDictionary<NSString *, id> *event = [self baseEvent];
[event addEntriesFromDictionary: @{@"data": message.body}];
_onMessage(event);
if (_onLoadingFinish) {
[event addEntriesFromDictionary: @{@"data": message.body}];
NSMutableDictionary<NSString *, id> *event = [self baseEvent];
_onMessage(event);
[event addEntriesFromDictionary: @{@"navigationType": message.body}];
_onLoadingFinish(event);
}
} else if ([message.name isEqualToString:MessageHandlerName]) {
if (_onMessage) {
NSMutableDictionary<NSString *, id> *event = [self baseEvent];
[event addEntriesFromDictionary: @{@"data": message.body}];
_onMessage(event);
}
}
}

Expand Down

0 comments on commit 3323d77

Please sign in to comment.