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

fix(ios): Supported WKURLSchemeHandler to load custom url scheme (8_0_X) #10750

Merged
merged 5 commits into from
Mar 7, 2019
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 9 additions & 0 deletions iphone/Classes/TiUIWebView.h
Expand Up @@ -39,4 +39,13 @@

@end

#if IS_XCODE_9
@interface WebAppProtocolHandler : NSObject <WKURLSchemeHandler> {
}

+ (NSString *)specialProtocolScheme;

@end
#endif

#endif
66 changes: 66 additions & 0 deletions iphone/Classes/TiUIWebView.m
Expand Up @@ -23,6 +23,7 @@
#import <TitaniumKit/TiUtils.h>
#import <TitaniumKit/Webcolor.h>

#import <Foundation/Foundation.h>
#import <objc/runtime.h>

extern NSString *const TI_APPLICATION_ID;
Expand Down Expand Up @@ -74,6 +75,15 @@ - (WKWebView *)webView
[controller addScriptMessageHandler:self name:@"_Ti_"];

[config setUserContentController:controller];

#if IS_XCODE_9
if ([TiUtils isIOSVersionOrGreater:@"11.0"]) {
if (![WKWebView handlesURLScheme:[WebAppProtocolHandler specialProtocolScheme]]) {
[config setURLSchemeHandler:[[WebAppProtocolHandler alloc] init] forURLScheme:[WebAppProtocolHandler specialProtocolScheme]];
}
}
#endif

_willHandleTouches = [TiUtils boolValue:[[self proxy] valueForKey:@"willHandleTouches"] def:YES];

_webView = [[WKWebView alloc] initWithFrame:[self bounds] configuration:config];
Expand Down Expand Up @@ -1314,4 +1324,60 @@ - (NSHTTPCookie *)cookieForString:(NSString *)cookieStr
}

@end

#if IS_XCODE_9

@implementation WebAppProtocolHandler

+ (NSString *)specialProtocolScheme
{
return @"app";
}

- (void)webView:(WKWebView *)webView startURLSchemeTask:(id<WKURLSchemeTask>)urlSchemeTask
{
NSURLRequest *request = [urlSchemeTask request];
NSURL *url = [request URL];
DebugLog(@"[DEBUG] Requested resource via app protocol, loading: %@", url);

// see if it's a compiled resource
NSData *data = [TiUtils loadAppResource:url];
if (data == nil) {
// check to see if it's a local resource in the bundle, could be
// a bundled image, etc. - or we could be running from XCode :)
NSString *urlpath = [url path];
if ([urlpath characterAtIndex:0] == '/') {
if ([[NSFileManager defaultManager] fileExistsAtPath:urlpath]) {
data = [[[NSData alloc] initWithContentsOfFile:urlpath] autorelease];
}
}
if (data == nil) {
NSString *resourceurl = [TiHost resourcePath];
NSString *path = [NSString stringWithFormat:@"%@%@", resourceurl, urlpath];
data = [[[NSData alloc] initWithContentsOfFile:path] autorelease];
}
}

if (data != nil) {
NSURLCacheStoragePolicy caching = NSURLCacheStorageAllowedInMemoryOnly;
NSString *mime = [Mimetypes mimeTypeForExtension:[url path]];
NSURLResponse *response = [[NSURLResponse alloc] initWithURL:url MIMEType:mime expectedContentLength:[data length] textEncodingName:@"utf-8"];
[urlSchemeTask didReceiveResponse:response];
[urlSchemeTask didReceiveData:data];
[urlSchemeTask didFinish];
[response release];
} else {
NSLog(@"[ERROR] Error loading %@", url);
[urlSchemeTask didFailWithError:[NSError errorWithDomain:NSURLErrorDomain code:NSURLErrorResourceUnavailable userInfo:nil]];
[urlSchemeTask didFinish];
}
}

- (void)webView:(nonnull WKWebView *)webView stopURLSchemeTask:(nonnull id<WKURLSchemeTask>)urlSchemeTask
{
}

@end
#endif

#endif