Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature errorlogging #3

Open
wants to merge 3 commits into
base: feature_demo
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/ios/CDVWKInAppBrowser.h
Original file line number Diff line number Diff line change
Expand Up @@ -80,4 +80,6 @@

- (id)initWithBrowserOptions: (CDVInAppBrowserOptions*) browserOptions andSettings:(NSDictionary*) settings;

- (void)webView:(WKWebView *)webView decidePolicyForNavigationResponse:(WKNavigationResponse *)navigationResponse decisionHandler:(void (^)(WKNavigationResponsePolicy))decisionHandler;

@end
109 changes: 102 additions & 7 deletions src/ios/CDVWKInAppBrowser.m
Original file line number Diff line number Diff line change
Expand Up @@ -745,7 +745,79 @@ - (void)createViews
configuration.processPool = [[CDVWKProcessPoolFactory sharedFactory] sharedProcessPool];
#endif
[configuration.userContentController addScriptMessageHandler:self name:IAB_BRIDGE_NAME];

//Added the script that will inject at initial load time.

// NSString *consoleLogScript = @"function captureLog(msg) {"
// " window.webkit.messageHandlers.logHandler.postMessage(msg);"
// "}"
// "console.log = function(msg) {"
// " captureLog('Log: ' + msg);"
// "};"
// "console.error = function(msg) {"
// " captureLog('Error: ' + msg);"
// "};"
// "console.warn = function(msg) {"
// " captureLog('Warning: ' + msg);"
// "};"
// "console.info = function(msg) {"
// " captureLog('Info: ' + msg);"
// "};";
//
NSString *consoleLogScript = @"(function() {"
" const OriginalXMLHttpRequest = XMLHttpRequest;"
""
" function InterceptedXMLHttpRequest() {"
" const xhr = new OriginalXMLHttpRequest();"
""
" xhr.addEventListener('readystatechange', function() {"
" if (xhr.readyState === 4 && xhr.status >= 400) {"
" const errorMsg = 'Intercepted failed XMLHttpRequest: ' + xhr.responseURL + ' | Response: ' + xhr.responseText;"
" console.log(errorMsg);"
" window.webkit.messageHandlers.logHandler.postMessage(errorMsg);"
" }"
" });"
""
" return xhr;"
" }"
""
" InterceptedXMLHttpRequest.prototype = OriginalXMLHttpRequest.prototype;"
" for (const prop in OriginalXMLHttpRequest) {"
" if (OriginalXMLHttpRequest.hasOwnProperty(prop)) {"
" InterceptedXMLHttpRequest[prop] = OriginalXMLHttpRequest[prop];"
" }"
" }"
""
" XMLHttpRequest = InterceptedXMLHttpRequest;"
"})();"
""
"(function() {"
" const originalFetch = fetch;"
""
" window.fetch = function() {"
" const requestArguments = arguments;"
""
" return originalFetch.apply(this, requestArguments)"
" .then(function(response) {"
" if (!response.ok) {"
" const errorMsg = 'Intercepted failed fetch request: ' + response.url + ' | Response: ' + response.clone().text();"
" console.log(errorMsg);"
" window.webkit.messageHandlers.logHandler.postMessage(errorMsg);"
" }"
" return response;"
" });"
" };"
""
"})();";




WKUserScript *consoleLogUserScript = [[WKUserScript alloc] initWithSource:consoleLogScript injectionTime:WKUserScriptInjectionTimeAtDocumentStart forMainFrameOnly:YES];
[configuration.userContentController addUserScript:consoleLogUserScript];
[configuration.userContentController addScriptMessageHandler:self name:@"logHandler"];



//WKWebView options
configuration.allowsInlineMediaPlayback = _browserOptions.allowinlinemediaplayback;
if (IsAtLeastiOSVersion(@"10.0")) {
Expand All @@ -760,12 +832,7 @@ - (void)createViews
}

if (@available(iOS 13.0, *)) {
NSString *contentMode = [self settingForKey:@"PreferredContentMode"];
if ([contentMode isEqual: @"mobile"]) {
configuration.defaultWebpagePreferences.preferredContentMode = WKContentModeMobile;
} else if ([contentMode isEqual: @"desktop"]) {
configuration.defaultWebpagePreferences.preferredContentMode = WKContentModeDesktop;
}
configuration.defaultWebpagePreferences.preferredContentMode = WKContentModeMobile;
}
self.webView = [[WKWebView alloc] initWithFrame:webViewBounds configuration:configuration];

Expand Down Expand Up @@ -1251,6 +1318,12 @@ - (BOOL)shouldAutorotate
return YES;
}

- (void)handleConsoleLogMessage:(NSString *)message {
// you can forward the console log message to anywhere
NSLog(@"Log from WebView: %@", message);
// You can replace the NSLog call to webservices call
}

- (UIInterfaceOrientationMask)supportedInterfaceOrientations
{
if ((self.orientationDelegate != nil) && [self.orientationDelegate respondsToSelector:@selector(supportedInterfaceOrientations)]) {
Expand Down Expand Up @@ -1283,5 +1356,27 @@ - (void)popupBridge:(POPPopupBridge *)bridge requestsDismissalOfViewController:(
[viewController dismissViewControllerAnimated:YES completion:nil];
}

- (void)webView:(WKWebView *)webView decidePolicyForNavigationResponse:(WKNavigationResponse *)navigationResponse decisionHandler:(void (^)(WKNavigationResponsePolicy))decisionHandler {

if ([navigationResponse.response isKindOfClass:[NSHTTPURLResponse class]]) {
NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)navigationResponse.response;

// You can check the URL and status code here
NSURL *url = httpResponse.URL;
NSInteger statusCode = httpResponse.statusCode;

// You can also access response headers if needed
NSDictionary *headers = httpResponse.allHeaderFields;

NSLog(@"Intercepted URL response: %@, Status code: %ld", url, (long)statusCode);

// Perform any additional actions or checks with the intercepted response
// ...

}

// Allow the navigation to continue
decisionHandler(WKNavigationResponsePolicyAllow);
}

@end //CDVWKInAppBrowserViewController
2 changes: 2 additions & 0 deletions src/ios/POPPopUpBridge.m
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ + (void)setReturnURLScheme:(NSString *)returnURLScheme {
}

- (id)initWithWebView:(WKWebView *)webView delegate:(id<POPPopupBridgeDelegate>)delegate {
scheme = [NSString stringWithFormat:@"%@.popupbridge", [[NSBundle mainBundle] bundleIdentifier]];

if (!scheme) {
[NSException raise:@"POPPopupBridgeSchemeNotSet" format:@"PopupBridge requires a URL scheme to be set"];
return nil;
Expand Down