Closed GymotgM closed 7 months ago
CDVURLProtocol
only worked with the UIWebView
which Apple doesn't allow to use since the end of 2020.
Cordova uses the WKWebView
and custom scheme handlers, plugins can have an overrideSchemeTask
method what Cordova's scheme handler check and let the plugin intercept the request instead of doing the default behavior.
Note that this only work for internal requests, for external requests you'll first need to replace the urls to point to the local scheme so they can intercepted, something like what proxy plugin does https://github.com/GEDYSIntraWare/cordova-plugin-webview-proxy/blob/master/src/ios/WebviewProxy.m#L22
How to use WebviewProxy? Pod install cannot find this plugin, so I can only manually put the file into the project. After putting it into the project, how can I proceed to the - (BOOL) overrideSchemeTask: (id
@jcesarmobile
Apache Cordova uses GitHub Issues as a feature request and bug tracker only. For usage and support questions, please check out the resources below. Thanks!
You can get answers to your usage and support questions about Apache Cordova on:
Slack Community Chat: https://cordova.slack.com (you can sign-up at https://s.apache.org/cordova-slack)
StackOverflow: https://stackoverflow.com/questions/tagged/cordova using the tag
cordova
(BOOL)canInitWithRequest:(NSURLRequest)theRequest { NSURL theUrl = [theRequest URL];
if ([[theUrl absoluteString] hasPrefix:kEBCDVBundlePrefixes] || [[theUrl absoluteString] hasPrefix:kEBCDVFilePrefixes]) { return YES; } else { return [CDVURLProtocol canInitWithRequest:theRequest]; } }
(void)startLoading { NSURL url = [[self request] URL]; if ([[url absoluteString] hasPrefix:kEBCDVBundlePrefixes] || [[url absoluteString] hasPrefix:kEBCDVFilePrefixes]) { NSString scheme = url.scheme; NSString fileName = url.host; NSString filePath = nil; if ([scheme isEqualToString:kEBCDVBundlePrefixes]) { filePath = [[NSBundle mainBundle] pathForResource:fileName ofType:nil]; } else { __block NSString appId = nil; dispatch_sync(dispatch_get_main_queue(), ^{ UIViewController currentVC = [Common currentViewController]; if ([currentVC isKindOfClass:[EBCDVViewController class]]) { appId = [(EBCDVViewController *)currentVC appId]; } }); filePath = [[Common getDocDirectoryOfApp:appId] stringByAppendingPathComponent:fileName]; }
} else { [super startLoading]; } }
(void)sendResponseWithResponseCode:(NSInteger)statusCode data:(NSData)data mimeType:(NSString)mimeType { if (mimeType == nil) { mimeType = @"text/plain"; }
NSHTTPURLResponse* response = [[NSHTTPURLResponse alloc] initWithURL:[[self request] URL] statusCode:statusCode HTTPVersion:@"HTTP/1.1" headerFields:@{@"Content-Type" : mimeType}];
[[self client] URLProtocol:self didReceiveResponse:response cacheStoragePolicy:NSURLCacheStorageNotAllowed]; if (data != nil) { [[self client] URLProtocol:self didLoadData:data]; } [[self client] URLProtocolDidFinishLoading:self]; }
If you are using a tool that uses Cordova internally, like e.g. Ionic, check their support channels: