Skip to content

Commit

Permalink
iOS: add another methods for provide direct http/https request with W…
Browse files Browse the repository at this point in the history
…KWebView
  • Loading branch information
dmitrys committed Dec 7, 2021
1 parent f3b1924 commit 174149e
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 13 deletions.
89 changes: 78 additions & 11 deletions platform/iphone/Classes/RhoWKWebView.mm
Original file line number Diff line number Diff line change
Expand Up @@ -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];

Expand Down
7 changes: 6 additions & 1 deletion platform/iphone/Classes/RhoWebViewFabrique.m
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
5 changes: 4 additions & 1 deletion res/generators/templates/application/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions res/generators/templates/application/rhoconfig.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit 174149e

Please sign in to comment.