Skip to content

Commit 20a5e21

Browse files
committed
[IOS] Fix instantiateViewController twice + better return exceptions to JS
1 parent 66f588e commit 20a5e21

File tree

3 files changed

+59
-28
lines changed

3 files changed

+59
-28
lines changed

cordova-plugin-nativeview.podspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ Pod::Spec.new do |s|
1616
#
1717

1818
s.name = "cordova-plugin-nativeview"
19-
s.version = "1.0.6"
19+
s.version = "1.0.7"
2020
s.summary = "Start or Back to a native screen/page from app or other app"
2121

2222
# This description is used to generate tags and improve search results.

plugin.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
<plugin xmlns="http://apache.org/cordova/ns/plugins/1.0"
44
id="cordova-plugin-nativeview"
5-
version="1.0.6">
5+
version="1.0.7">
66
<name>Cordova NativeView Plugin</name>
77
<description>
88
Start or Back to a UIViewController(ios)/Activity(Android)

src/ios/CDVNativeView.m

Lines changed: 57 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -22,22 +22,22 @@ - (instancetype)init
2222
self = [super init];
2323
if (self) {
2424
self.resultExceptions = @{
25-
@"IO_EXCEPTION": ^{
25+
@"IoException": ^{
2626
return CDVCommandStatus_IO_EXCEPTION;
2727
},
28-
@"CLASS_NOT_FOUND_EXCEPTION": ^{
28+
@"NotFoundException": ^{
2929
return CDVCommandStatus_CLASS_NOT_FOUND_EXCEPTION;
3030
},
31-
@"PARAM_INVALID_EXCEPTION": ^{
31+
@"ParamInvalidException": ^{
3232
return CDVCommandStatus_ERROR;
3333
},
34-
@"INSTANTIATION_EXCEPTION": ^{
34+
@"InstantiationException": ^{
3535
return CDVCommandStatus_INSTANTIATION_EXCEPTION;
3636
},
37-
@"NOT_FOUND_EXCEPTION": ^{
37+
@"NoResultException": ^{
3838
return CDVCommandStatus_NO_RESULT;
3939
},
40-
@"PARAMS_TYPE_EXCEPTION": ^{
40+
@"ParamsTypeException": ^{
4141
return CDVCommandStatus_INVALID_ACTION;
4242
}
4343
};
@@ -84,7 +84,7 @@ - (void)show:(CDVInvokedUrlCommand*)command {
8484

8585
} else {
8686
message = [[NSString alloc] initWithFormat:@"%@ invalid. Must contain a Storyboard / Controller / URI valid in name", firstParam];
87-
@throw [[NSException alloc] initWithName:@"IO_EXCEPTION" reason:message userInfo:nil];
87+
@throw [[NSException alloc] initWithName:@"IoException" reason:message userInfo:nil];
8888
}
8989

9090
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK];
@@ -100,16 +100,11 @@ - (void)show:(CDVInvokedUrlCommand*)command {
100100

101101
}else{
102102
message = [[NSString alloc] initWithFormat:@"An UIViewController name or Storyboard name or URI valid name is required at least. Please, pass in the first param in JS, like this: 'NativeView.show('MyViewController') or NativeView.show('MyStoryboard') or NativeView.show('MyStoryboard', 'MyViewController') or NativeView.show('instagram://')"];
103-
@throw [[NSException alloc] initWithName:@"CLASS_NOT_FOUND_EXCEPTION" reason:message userInfo:nil];
103+
@throw [[NSException alloc] initWithName:@"NotFoundException" reason:message userInfo:nil];
104104
}
105-
106-
// Init viewController from Storyboard with initial view Controlleror or user defined viewControllerName
107-
[self instantiateViewController:viewControllerName fromStoryboard:storyboardName];
108-
109-
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK];
110105

111106
}else{
112-
@throw [[NSException alloc] initWithName:@"PARAMS_TYPE_EXCEPTION" reason:@"The params of show() method needs be a string or a json" userInfo:nil];
107+
@throw [[NSException alloc] initWithName:@"ParamsTypeException" reason:@"The params of show() method needs be a string or a json" userInfo:nil];
113108
}
114109

115110
if ([self isValidURI: uri]) {
@@ -132,7 +127,13 @@ - (void)show:(CDVInvokedUrlCommand*)command {
132127
CaseBlock c = self.resultExceptions[e.name];
133128

134129
CDVCommandStatus exceptionType = c ? c() : CDVCommandStatus_ERROR;
135-
pluginResult = [CDVPluginResult resultWithStatus:exceptionType messageAsString:e.reason];
130+
NSDictionary* error = @{
131+
@"success": @NO,
132+
@"name": e.name,
133+
@"message": e.reason
134+
};
135+
136+
pluginResult = [CDVPluginResult resultWithStatus:exceptionType messageAsDictionary:error];
136137
}
137138

138139
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
@@ -160,7 +161,13 @@ - (void)showMarket:(CDVInvokedUrlCommand*)command {
160161

161162
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK];
162163
} else {
163-
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_INVALID_ACTION messageAsString:@"Invalid application id: the parameter 'marketId' is invalid"];
164+
NSDictionary* error = @{
165+
@"success": @NO,
166+
@"name": @"ParamInvalidException",
167+
@"message": @"Invalid application id: the parameter 'marketId' is invalid"
168+
};
169+
170+
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_INVALID_ACTION messageAsDictionary:error];
164171
}
165172

166173
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
@@ -178,23 +185,36 @@ - (void)checkIfAppInstalled:(CDVInvokedUrlCommand*)command {
178185
uri = [config objectForKey:@"uri"];
179186

180187
if (uri == nil) {
181-
@throw [[NSException alloc] initWithName:@"PARAMS_TYPE_EXCEPTION" reason:@"The 'uri' key is required" userInfo:nil];
188+
@throw [[NSException alloc] initWithName:@"ParamsTypeException" reason:@"The 'uri' key is required" userInfo:nil];
182189
}
183190
}else if ([config isKindOfClass:[NSString class]]) {
184191
uri = (NSString *) config;
185192
}else{
186-
@throw [[NSException alloc] initWithName:@"PARAMS_TYPE_EXCEPTION" reason:@"The params of checkIfAppInstalled() method needs be a string or a json" userInfo:nil];
193+
@throw [[NSException alloc] initWithName:@"ParamsTypeException" reason:@"The params of checkIfAppInstalled() method needs be a string or a json" userInfo:nil];
187194
}
188195

189196
if (![self isValidURI: uri]) {
190197
NSString *message = [[NSString alloc] initWithFormat:@"uri param invalid: %@", uri];
191-
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:message];
198+
NSDictionary* error = @{
199+
@"success": @NO,
200+
@"name": @"ParamInvalidException",
201+
@"message": message
202+
};
203+
204+
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsDictionary:error];
192205
} else {
193206
if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:uri]]) {
194207
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsBool:(true)];
195208
}
196209
else {
197-
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsBool:(false)];
210+
NSString *message = [[NSString alloc] initWithFormat:@"The app that responds to URI: %@ was not found", uri];
211+
NSDictionary* error = @{
212+
@"success": @NO,
213+
@"name": @"NotFoundException",
214+
@"message": message
215+
};
216+
217+
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_CLASS_NOT_FOUND_EXCEPTION messageAsDictionary:error];
198218
}
199219
}
200220
} @catch (NSException *e) {
@@ -205,7 +225,12 @@ - (void)checkIfAppInstalled:(CDVInvokedUrlCommand*)command {
205225
CaseBlock c = self.resultExceptions[e.name];
206226

207227
CDVCommandStatus exceptionType = c ? c() : CDVCommandStatus_ERROR;
208-
pluginResult = [CDVPluginResult resultWithStatus:exceptionType messageAsString:e.reason];
228+
NSDictionary* error = @{
229+
@"success": @NO,
230+
@"name": e.name,
231+
@"message": e.reason
232+
};
233+
pluginResult = [CDVPluginResult resultWithStatus:exceptionType messageAsDictionary:error];
209234
} @finally {
210235
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
211236
}
@@ -243,7 +268,7 @@ - (void) instantiateViewController:(NSString *)viewControllerName {
243268
}
244269
} @catch(NSException *e) {
245270
message = [[NSString alloc] initWithFormat:@"%@ and/or its own xib does not exist. \nDetail: %@", viewControllerName, e.reason];
246-
@throw [[NSException alloc] initWithName:@"CLASS_NOT_FOUND_EXCEPTION" reason:message userInfo:nil];
271+
@throw [[NSException alloc] initWithName:@"NotFoundException" reason:message userInfo:nil];
247272
}
248273
}
249274

@@ -252,7 +277,7 @@ - (void) instantiateViewController:(NSString *)viewControllerName {
252277

253278
} else {
254279
message = [[NSString alloc] initWithFormat:@"UIViewController with name %@ was not found", viewControllerName];
255-
@throw [[NSException alloc] initWithName:@"PARAM_INVALID_EXCEPTION" reason:message userInfo:nil];
280+
@throw [[NSException alloc] initWithName:@"ParamInvalidException" reason:message userInfo:nil];
256281
}
257282
}
258283

@@ -291,7 +316,7 @@ - (void) instantiateViewController:(NSString *)viewControllerName fromStoryboard
291316
}
292317
} @catch (NSException *e) {
293318
NSString *detailMessage = [[NSString alloc] initWithFormat:@"%@ \nDetail: %@", message, e.reason];
294-
@throw [[NSException alloc] initWithName:@"INSTANTIATION_EXCEPTION" reason:detailMessage userInfo:nil];
319+
@throw [[NSException alloc] initWithName:@"NotFoundException" reason:detailMessage userInfo:nil];
295320
}
296321
}
297322

@@ -300,7 +325,7 @@ - (void) instantiateViewController:(NSString *)viewControllerName fromStoryboard
300325

301326
} else {
302327
message = [[NSString alloc] initWithFormat:@"Storyboard %@ was not found", storyboardName];
303-
@throw [[NSException alloc] initWithName:@"PARAM_INVALID_EXCEPTION" reason:message userInfo:nil];
328+
@throw [[NSException alloc] initWithName:@"ParamInvalidException" reason:message userInfo:nil];
304329
}
305330
}
306331

@@ -322,7 +347,13 @@ - (void) openAPP:(NSString *)uriValue withCommand:(CDVInvokedUrlCommand*) comman
322347
NSString* message = @"APP with uri %@ not found.";
323348
NSLog(message, uriValue);
324349

325-
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_INSTANTIATION_EXCEPTION messageAsString:[[NSString alloc] initWithFormat:message, uriValue]];
350+
NSDictionary* error = @{
351+
@"success": @NO,
352+
@"name": @"InstantiationException",
353+
@"message": [[NSString alloc] initWithFormat:message, uriValue]
354+
};
355+
356+
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_INSTANTIATION_EXCEPTION messageAsDictionary:error];
326357
} else {
327358
NSString* message = @"APP with uri %@ opened.";
328359
NSLog(message, uriValue);

0 commit comments

Comments
 (0)