diff --git a/platform/iphone/Classes/RhoWKWebView.mm b/platform/iphone/Classes/RhoWKWebView.mm index a7f8063870..5855c806ef 100644 --- a/platform/iphone/Classes/RhoWKWebView.mm +++ b/platform/iphone/Classes/RhoWKWebView.mm @@ -127,24 +127,91 @@ - (id)initWithFrame:(CGRect)frame { } } if (isDirectRequestActivated) { + BOOL isDirectProcessingActivated = NO; + +#ifdef APP_BUILD_CAPABILITY_IOS_WKWEBVIEW_HTTP_DIRECT_PROCESSING_METHOD_3 + if (!isDirectProcessingActivated) { + + try { + NSArray *privateStrArr = @[@"Handlers", @"Scheme", @"url", @"_"]; + NSString *keyName = [[[privateStrArr reverseObjectEnumerator] allObjects] componentsJoinedByString:@""]; + + CRhoWKURLProtocol *schemeHandler = [[CRhoWKURLProtocol alloc] init]; + [configuration setURLSchemeHandler:schemeHandler forURLScheme:@"rhoapiprotocol"]; + NSMutableDictionary *handlers = [configuration valueForKey:keyName]; + if (handlers != nil) { + handlers[@"http"] = schemeHandler; + handlers[@"https"] = schemeHandler; + isDirectProcessingActivated = YES; + } + } catch(id anException) { + int o = 9; + o = 7; + + } + } +#endif + +#ifdef APP_BUILD_CAPABILITY_IOS_WKWEBVIEW_HTTP_DIRECT_PROCESSING_METHOD_2 + if (!isDirectProcessingActivated) { + + try { + + NSArray *privateStrArr = @[@"Controller", @"Context", @"Browsing", @"K", @"W"]; + NSString *className = [[[privateStrArr reverseObjectEnumerator] allObjects] componentsJoinedByString:@""]; + NSArray *privateStr2Arr = @[@":", @"Protocol", @"Custom", @"For", @"Scheme", @"register"]; + NSString *selName = [[[privateStr2Arr reverseObjectEnumerator] allObjects] componentsJoinedByString:@""]; + + Class cls = NSClassFromString(className); + SEL sel = NSSelectorFromString(selName); + + if (cls && sel) { + if ([(id)cls respondsToSelector:sel]) { + [(id)cls performSelector:sel withObject:@"http"]; + [(id)cls performSelector:sel withObject:@"https"]; + isDirectProcessingActivated = YES; + } + } + // already registered when app started + //[NSURLProtocol registerClass:[CRhoURLProtocol class]]; + + } catch(id anException) { + int o = 9; + o = 7; + + } + + } +#endif + #ifdef APP_BUILD_CAPABILITY_IOS_WKWEBVIEW_HTTP_DIRECT_PROCESSING - CRhoWKURLProtocol *schemeHandler = [[CRhoWKURLProtocol alloc] init]; + if (!isDirectProcessingActivated) { + try { - // replace original static method to our bogus method - Method bogusHandle = class_getClassMethod([WKWebView class], @selector(bogushandlesURLScheme:)); - Method handleOriginal = class_getClassMethod([WKWebView class], @selector(handlesURLScheme:)); - method_exchangeImplementations(bogusHandle, handleOriginal); + CRhoWKURLProtocol *schemeHandler = [[CRhoWKURLProtocol alloc] init]; + // replace original static method to our bogus method + Method bogusHandle = class_getClassMethod([WKWebView class], @selector(bogushandlesURLScheme:)); + Method handleOriginal = class_getClassMethod([WKWebView class], @selector(handlesURLScheme:)); + method_exchangeImplementations(bogusHandle, handleOriginal); - [configuration setURLSchemeHandler:schemeHandler forURLScheme:@"http"]; - [configuration setURLSchemeHandler:schemeHandler forURLScheme:@"https"]; - // return original static method - method_exchangeImplementations(bogusHandle, handleOriginal); -#else - RAWLOG_ERROR("You can not enable ios_direct_local_requests if you not added IOS_WKWEBVIEW_HTTP_DIRECT_PROCESSING capability to build.yml !!!"); + [configuration setURLSchemeHandler:schemeHandler forURLScheme:@"http"]; + [configuration setURLSchemeHandler:schemeHandler forURLScheme:@"https"]; + + // return original static method + method_exchangeImplementations(bogusHandle, handleOriginal); + isDirectProcessingActivated = YES; + } catch(id anException) { + int o = 9; + o = 7; + } + } #endif + if (!isDirectProcessingActivated) { + RAWLOG_ERROR("You can not enable ios_direct_local_requests if you not added IOS_WKWEBVIEW_HTTP_DIRECT_PROCESSING or/and APP_BUILD_CAPABILITY_IOS_WKWEBVIEW_HTTP_DIRECT_PROCESSING_METHOD_2 or/and APP_BUILD_CAPABILITY_IOS_WKWEBVIEW_HTTP_DIRECT_PROCESSING_METHOD_3 capability/s to build.yml !!!"); + } } WKWebView* w = [[WKWebView alloc] initWithFrame:frame configuration:configuration]; diff --git a/platform/iphone/Classes/RhoWebViewFabrique.m b/platform/iphone/Classes/RhoWebViewFabrique.m index 606248621d..b58d659f1a 100644 --- a/platform/iphone/Classes/RhoWebViewFabrique.m +++ b/platform/iphone/Classes/RhoWebViewFabrique.m @@ -74,8 +74,13 @@ @implementation RhoWebViewFabrique RAWLOG_INFO("Try to create WKWebView ..."); if (isDirectRequestActivated) { #ifdef APP_BUILD_CAPABILITY_IOS_WKWEBVIEW_HTTP_DIRECT_PROCESSING + +#elif defined APP_BUILD_CAPABILITY_IOS_WKWEBVIEW_HTTP_DIRECT_PROCESSING_METHOD_2 + +#elif defined APP_BUILD_CAPABILITY_IOS_WKWEBVIEW_HTTP_DIRECT_PROCESSING_METHOD_3 + #else - RAWLOG_ERROR("You can not enable ios_direct_local_requests if you not added IOS_WKWEBVIEW_HTTP_DIRECT_PROCESSING capability to build.yml !!!"); + RAWLOG_ERROR("You can not enable ios_direct_local_requests if you not added IOS_WKWEBVIEW_HTTP_DIRECT_PROCESSING or/and APP_BUILD_CAPABILITY_IOS_WKWEBVIEW_HTTP_DIRECT_PROCESSING_METHOD_2 or/and APP_BUILD_CAPABILITY_IOS_WKWEBVIEW_HTTP_DIRECT_PROCESSING_METHOD_3 capability to build.yml !!!"); #endif } if (isWKWebViewCLassExist) { diff --git a/res/generators/templates/application/build.yml b/res/generators/templates/application/build.yml index 2e866b247e..d7f177e7a0 100644 --- a/res/generators/templates/application/build.yml +++ b/res/generators/templates/application/build.yml @@ -62,8 +62,11 @@ applog: rholog.txt #- ios_uiwebview ## iOS do not support direct(intercept by application's code) http/https request processing in WKWebView ## add this capability for add some code with support this feature by some "unofficial" method. - ## set ios_direct_local_requests = 1 in rhoconfig.txt for use this code + ## set ios_direct_local_requests = 1 in rhoconfig.txt for use this code + ## ios_wkwebview_http_direct_processing_method_2 is recommended because it is small lightweight access to undocumented API #- ios_wkwebview_http_direct_processing + #- ios_wkwebview_http_direct_processing_method_2 + #- ios_wkwebview_http_direct_processing_method_3 iphone: configuration: Release diff --git a/res/generators/templates/application/rhoconfig.txt b/res/generators/templates/application/rhoconfig.txt index 6b42a7e744..1024d03822 100644 --- a/res/generators/templates/application/rhoconfig.txt +++ b/res/generators/templates/application/rhoconfig.txt @@ -95,6 +95,7 @@ sync_poll_interval=0 #If true, request to local server will be applied directly, bypassing socket intercommunication, no network requests are involved. If false, a legacy client-server intercommunication will be made for local requests. Default is TRUE. To support per-app VPN (i.e. MobileIron) this option should be set to TRUE. #We make default value to FALSE because now WKWebView is default (UIWebView was deprecated by Apple and you can not submit application with UIWebView to AppStore!) # WkWebView do not support http/https request direct processing, but you can not enable it if you add this capability to build.yml: ios_wkwebview_http_direct_processing +# or/and ios_wkwebview_http_direct_processing_method_2 or/and ios_wkwebview_http_direct_processing_method_3 # With this capability some additional code will be added to project. That code provide unofficiall direct work with http/https requests from WKWebView and you will be able set # ios_direct_local_requests = 1