-
Notifications
You must be signed in to change notification settings - Fork 391
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(ios): Add URLSchemeHandler for iOS 11+ (#221)
* Add URLSchemeHandler for iOS 11+ * Make Scheme usage configurable
- Loading branch information
1 parent
ade4f78
commit 4a973f4
Showing
6 changed files
with
161 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,4 +14,5 @@ Thumbs.db | |
|
||
node_modules | ||
xcuserdata | ||
package-lock.json | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
#import <Foundation/Foundation.h> | ||
#import <WebKit/WebKit.h> | ||
|
||
@interface IONAssetHandler : NSObject <WKURLSchemeHandler> | ||
|
||
@property (nonatomic, strong) NSString * basePath; | ||
|
||
-(void)setAssetPath:(NSString *)assetPath; | ||
|
||
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
#import "IONAssetHandler.h" | ||
#import <MobileCoreServices/MobileCoreServices.h> | ||
|
||
@implementation IONAssetHandler | ||
|
||
-(void)setAssetPath:(NSString *)assetPath { | ||
self.basePath = assetPath; | ||
} | ||
|
||
- (void)webView:(WKWebView *)webView startURLSchemeTask:(id <WKURLSchemeTask>)urlSchemeTask | ||
API_AVAILABLE(ios(11.0)){ | ||
NSString * startPath = @""; | ||
NSURL * url = urlSchemeTask.request.URL; | ||
NSString * stringToLoad = url.path; | ||
NSString * scheme = url.scheme; | ||
if ([scheme isEqualToString:@"ionic"]) { | ||
startPath = self.basePath; | ||
if ([stringToLoad isEqualToString:@""] || !url.pathExtension) { | ||
startPath = [startPath stringByAppendingString:@"/index.html"]; | ||
} else { | ||
startPath = [startPath stringByAppendingString:stringToLoad]; | ||
} | ||
} else { | ||
if (![stringToLoad isEqualToString:@""]) { | ||
startPath = stringToLoad; | ||
} | ||
} | ||
|
||
NSData * data = [[NSData alloc] initWithContentsOfFile:startPath]; | ||
NSInteger statusCode = 200; | ||
if (!data) { | ||
statusCode = 404; | ||
} | ||
NSURL * localUrl = [NSURL URLWithString:url.absoluteString]; | ||
NSString * mimeType = [self getMimeType:url.pathExtension]; | ||
id response = nil; | ||
if (data && [self isMediaExtension:url.pathExtension]) { | ||
response = [[NSURLResponse alloc] initWithURL:localUrl MIMEType:mimeType expectedContentLength:data.length textEncodingName:nil]; | ||
} else { | ||
NSDictionary * headers = @{ @"Content-Type" : mimeType, @"Cache-Control": @"no-cache"}; | ||
response = [[NSHTTPURLResponse alloc] initWithURL:localUrl statusCode:statusCode HTTPVersion:nil headerFields:headers]; | ||
} | ||
|
||
[urlSchemeTask didReceiveResponse:response]; | ||
[urlSchemeTask didReceiveData:data]; | ||
[urlSchemeTask didFinish]; | ||
|
||
} | ||
|
||
- (void)webView:(nonnull WKWebView *)webView stopURLSchemeTask:(nonnull id<WKURLSchemeTask>)urlSchemeTask API_AVAILABLE(ios(11.0)){ | ||
NSLog(@"stop"); | ||
} | ||
|
||
-(NSString *) getMimeType:(NSString *)fileExtension { | ||
if (fileExtension && ![fileExtension isEqualToString:@""]) { | ||
NSString *UTI = (__bridge_transfer NSString *)UTTypeCreatePreferredIdentifierForTag(kUTTagClassFilenameExtension, (__bridge CFStringRef)fileExtension, NULL); | ||
NSString *contentType = (__bridge_transfer NSString *)UTTypeCopyPreferredTagWithClass((__bridge CFStringRef)UTI, kUTTagClassMIMEType); | ||
return contentType ? contentType : @"application/octet-stream"; | ||
} else { | ||
return @"text/html"; | ||
} | ||
} | ||
|
||
-(BOOL) isMediaExtension:(NSString *) pathExtension { | ||
NSArray * mediaExtensions = @[@"m4v", @"mov", @"mp4", | ||
@"aac", @"ac3", @"aiff", @"au", @"flac", @"m4a", @"mp3", @"wav"]; | ||
if ([mediaExtensions containsObject:pathExtension]) { | ||
return YES; | ||
} | ||
return NO; | ||
} | ||
|
||
|
||
@end |