Skip to content

Commit d52d37e

Browse files
jcesarmobilemlynch
authored andcommitted
feat(ios): Make iOS app Scheme configurable with a preference (#307)
Allows to use a custom scheme instead of the ionic default one fix #282
1 parent 1e9d03a commit d52d37e

File tree

5 files changed

+36
-11
lines changed

5 files changed

+36
-11
lines changed

README.md

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,20 @@ Other possible values are `1` (`MIXED_CONTENT_NEVER_ALLOW`) and `2` (`MIXED_CONT
9393

9494
Preferences only available for iOS platform
9595

96+
#### iosScheme
97+
98+
```xml
99+
<preference name="iosScheme" value="httpsionic" />
100+
```
101+
102+
Default value is `ionic`
103+
104+
Configures the Scheme the app uses to load the content.
105+
106+
Values like `http`, `https` or `file` are not valid and will use default value instead.
107+
108+
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`).
109+
96110
#### WKSuspendInBackground
97111

98112
```xml
@@ -128,9 +142,9 @@ Whether to use a dark styled keyboard on iOS
128142
129143
* 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`.
130144
131-
1. Apps are now served from `ionic://` scheme on iOS.
145+
1. Apps are now served from `ionic://` scheme on iOS by default.
132146
133-
* The default origin for requests from the iOS WebView is `ionic://localhost`. If `Hostname` preference is set, then origin will be `ionic://HostnameValue`.
147+
* 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`.
134148
135149
1. Replace any usages of `window.Ionic.normalizeURL()` with `window.Ionic.WebView.convertFileSrc()`.
136150

src/ios/CDVWKWebViewEngine.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,6 @@
2525
@property (nonatomic, strong, readonly) id <WKUIDelegate> uiDelegate;
2626
@property (nonatomic, strong) NSString * basePath;
2727

28-
extern NSString * const IONIC_SCHEME;
29-
3028
-(void)setServerBasePath:(CDVInvokedUrlCommand*)command;
3129
-(void)getServerBasePath:(CDVInvokedUrlCommand*)command;
3230

src/ios/CDVWKWebViewEngine.m

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -118,8 +118,6 @@ @implementation CDVWKWebViewEngine
118118

119119
NSTimer *timer;
120120

121-
NSString * const IONIC_SCHEME = @"ionic";
122-
123121
- (instancetype)initWithFrame:(CGRect)frame
124122
{
125123
self = [super init];
@@ -197,7 +195,11 @@ - (void)pluginInitialize
197195
if(bind == nil){
198196
bind = @"localhost";
199197
}
200-
self.CDV_LOCAL_SERVER = [NSString stringWithFormat:@"%@://%@",IONIC_SCHEME, bind];
198+
NSString *scheme = [settings cordovaSettingForKey:@"iosScheme"];
199+
if(scheme == nil || [scheme isEqualToString:@"http"] || [scheme isEqualToString:@"https"] || [scheme isEqualToString:@"file"]){
200+
scheme = @"ionic";
201+
}
202+
self.CDV_LOCAL_SERVER = [NSString stringWithFormat:@"%@://%@", scheme, bind];
201203

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

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

241-
self.handler = [[IONAssetHandler alloc] init];
242-
[self.handler setAssetPath:[self getStartPath]];
243-
[configuration setURLSchemeHandler:self.handler forURLScheme:IONIC_SCHEME];
243+
self.handler = [[IONAssetHandler alloc] initWithBasePath:[self getStartPath] andScheme:scheme];
244+
[configuration setURLSchemeHandler:self.handler forURLScheme:scheme];
244245

245246
// re-create WKWebView, since we need to update configuration
246247
// remove from keyWindow before recreating

src/ios/IONAssetHandler.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@
44
@interface IONAssetHandler : NSObject <WKURLSchemeHandler>
55

66
@property (nonatomic, strong) NSString * basePath;
7+
@property (nonatomic, strong) NSString * scheme;
78

89
-(void)setAssetPath:(NSString *)assetPath;
10+
- (instancetype)initWithBasePath:(NSString *)basePath andScheme:(NSString *)scheme;
11+
912

1013
@end

src/ios/IONAssetHandler.m

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,23 @@ -(void)setAssetPath:(NSString *)assetPath {
88
self.basePath = assetPath;
99
}
1010

11+
- (instancetype)initWithBasePath:(NSString *)basePath andScheme:(NSString *)scheme {
12+
self = [super init];
13+
if (self) {
14+
_basePath = basePath;
15+
_scheme = scheme;
16+
}
17+
return self;
18+
}
19+
1120
- (void)webView:(WKWebView *)webView startURLSchemeTask:(id <WKURLSchemeTask>)urlSchemeTask
1221
{
1322
NSString * startPath = @"";
1423
NSURL * url = urlSchemeTask.request.URL;
1524
NSString * stringToLoad = url.path;
1625
NSString * scheme = url.scheme;
1726

18-
if ([scheme isEqualToString:IONIC_SCHEME]) {
27+
if ([scheme isEqualToString:self.scheme]) {
1928
if ([stringToLoad hasPrefix:@"/_app_file_"]) {
2029
startPath = [stringToLoad stringByReplacingOccurrencesOfString:@"/_app_file_" withString:@""];
2130
} else {

0 commit comments

Comments
 (0)