Skip to content

Commit

Permalink
feat(ios): Make iOS app Scheme configurable with a preference (#307)
Browse files Browse the repository at this point in the history
Allows to use a custom scheme instead of the ionic default one

fix #282
  • Loading branch information
jcesarmobile authored and mlynch committed Feb 18, 2019
1 parent 1e9d03a commit d52d37e
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 11 deletions.
18 changes: 16 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,20 @@ Other possible values are `1` (`MIXED_CONTENT_NEVER_ALLOW`) and `2` (`MIXED_CONT

Preferences only available for iOS platform

#### iosScheme

```xml
<preference name="iosScheme" value="httpsionic" />
```

Default value is `ionic`

Configures the Scheme the app uses to load the content.

Values like `http`, `https` or `file` are not valid and will use default value instead.

If you change it, you'll need to add a new `allow-navigation` entry in the `config.xml` for the configured scheme (i.e `<allow-navigation href="httpsionic://*"/>` if `iosScheme` is set to `httpsionic`).

#### WKSuspendInBackground

```xml
Expand Down Expand Up @@ -128,9 +142,9 @@ Whether to use a dark styled keyboard on iOS

* The default origin for requests from the Android WebView is `http://localhost`. If `Hostname` and `Scheme` preferences are set, then origin will be `schemeValue://HostnameValue`.

1. Apps are now served from `ionic://` scheme on iOS.
1. Apps are now served from `ionic://` scheme on iOS by default.

* The default origin for requests from the iOS WebView is `ionic://localhost`. If `Hostname` preference is set, then origin will be `ionic://HostnameValue`.
* The default origin for requests from the iOS WebView is `ionic://localhost`. If `Hostname` and `iosScheme` preferences are set, then origin will be `iosSchemeValue://HostnameValue`.

1. Replace any usages of `window.Ionic.normalizeURL()` with `window.Ionic.WebView.convertFileSrc()`.

Expand Down
2 changes: 0 additions & 2 deletions src/ios/CDVWKWebViewEngine.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@
@property (nonatomic, strong, readonly) id <WKUIDelegate> uiDelegate;
@property (nonatomic, strong) NSString * basePath;

extern NSString * const IONIC_SCHEME;

-(void)setServerBasePath:(CDVInvokedUrlCommand*)command;
-(void)getServerBasePath:(CDVInvokedUrlCommand*)command;

Expand Down
13 changes: 7 additions & 6 deletions src/ios/CDVWKWebViewEngine.m
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,6 @@ @implementation CDVWKWebViewEngine

NSTimer *timer;

NSString * const IONIC_SCHEME = @"ionic";

- (instancetype)initWithFrame:(CGRect)frame
{
self = [super init];
Expand Down Expand Up @@ -197,7 +195,11 @@ - (void)pluginInitialize
if(bind == nil){
bind = @"localhost";
}
self.CDV_LOCAL_SERVER = [NSString stringWithFormat:@"%@://%@",IONIC_SCHEME, bind];
NSString *scheme = [settings cordovaSettingForKey:@"iosScheme"];
if(scheme == nil || [scheme isEqualToString:@"http"] || [scheme isEqualToString:@"https"] || [scheme isEqualToString:@"file"]){
scheme = @"ionic";
}
self.CDV_LOCAL_SERVER = [NSString stringWithFormat:@"%@://%@", scheme, bind];

self.uiDelegate = [[CDVWKWebViewUIDelegate alloc] initWithTitle:[[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleDisplayName"]];

Expand Down Expand Up @@ -238,9 +240,8 @@ - (void)pluginInitialize
WKWebViewConfiguration* configuration = [self createConfigurationFromSettings:settings];
configuration.userContentController = userContentController;

self.handler = [[IONAssetHandler alloc] init];
[self.handler setAssetPath:[self getStartPath]];
[configuration setURLSchemeHandler:self.handler forURLScheme:IONIC_SCHEME];
self.handler = [[IONAssetHandler alloc] initWithBasePath:[self getStartPath] andScheme:scheme];
[configuration setURLSchemeHandler:self.handler forURLScheme:scheme];

// re-create WKWebView, since we need to update configuration
// remove from keyWindow before recreating
Expand Down
3 changes: 3 additions & 0 deletions src/ios/IONAssetHandler.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@
@interface IONAssetHandler : NSObject <WKURLSchemeHandler>

@property (nonatomic, strong) NSString * basePath;
@property (nonatomic, strong) NSString * scheme;

-(void)setAssetPath:(NSString *)assetPath;
- (instancetype)initWithBasePath:(NSString *)basePath andScheme:(NSString *)scheme;


@end
11 changes: 10 additions & 1 deletion src/ios/IONAssetHandler.m
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,23 @@ -(void)setAssetPath:(NSString *)assetPath {
self.basePath = assetPath;
}

- (instancetype)initWithBasePath:(NSString *)basePath andScheme:(NSString *)scheme {
self = [super init];
if (self) {
_basePath = basePath;
_scheme = scheme;
}
return self;
}

- (void)webView:(WKWebView *)webView startURLSchemeTask:(id <WKURLSchemeTask>)urlSchemeTask
{
NSString * startPath = @"";
NSURL * url = urlSchemeTask.request.URL;
NSString * stringToLoad = url.path;
NSString * scheme = url.scheme;

if ([scheme isEqualToString:IONIC_SCHEME]) {
if ([scheme isEqualToString:self.scheme]) {
if ([stringToLoad hasPrefix:@"/_app_file_"]) {
startPath = [stringToLoad stringByReplacingOccurrencesOfString:@"/_app_file_" withString:@""];
} else {
Expand Down

0 comments on commit d52d37e

Please sign in to comment.