From 92e0b27105443cb1c0f28d13ffd47b352b7b1d89 Mon Sep 17 00:00:00 2001 From: Fernando Tubio Date: Thu, 9 Jun 2016 13:01:20 -0300 Subject: [PATCH 1/2] Reformatted iOS source code for consistency --- src/ios/CDVHostedWebApp.h | 16 +++---- src/ios/CDVHostedWebApp.m | 90 ++++++++++++++++++++------------------- 2 files changed, 55 insertions(+), 51 deletions(-) diff --git a/src/ios/CDVHostedWebApp.h b/src/ios/CDVHostedWebApp.h index d0e7274..b385b10 100644 --- a/src/ios/CDVHostedWebApp.h +++ b/src/ios/CDVHostedWebApp.h @@ -13,20 +13,20 @@ @interface CDVHostedWebApp : CDVPlugin { - CVDWebViewNotificationDelegate *notificationDelegate; - NSDictionary *manifest; + CVDWebViewNotificationDelegate* notificationDelegate; + NSDictionary* manifest; } -@property (nonatomic, strong, readonly) NSDictionary *manifest; +@property (nonatomic, strong, readonly) NSDictionary* manifest; --(void) loadManifest:(CDVInvokedUrlCommand*)command; +- (void)loadManifest:(CDVInvokedUrlCommand*)command; --(void) getManifest:(CDVInvokedUrlCommand*)command; +- (void)getManifest:(CDVInvokedUrlCommand*)command; --(void) enableOfflinePage:(CDVInvokedUrlCommand*)command; +- (void)enableOfflinePage:(CDVInvokedUrlCommand*)command; --(void) disableOfflinePage:(CDVInvokedUrlCommand*)command; +- (void)disableOfflinePage:(CDVInvokedUrlCommand*)command; --(void) injectPluginScript:(CDVInvokedUrlCommand *)command; +- (void)injectPluginScript:(CDVInvokedUrlCommand*)command; @end diff --git a/src/ios/CDVHostedWebApp.m b/src/ios/CDVHostedWebApp.m index c68327a..2f99d6f 100644 --- a/src/ios/CDVHostedWebApp.m +++ b/src/ios/CDVHostedWebApp.m @@ -19,25 +19,29 @@ @interface CDVHostedWebApp () @implementation CVDWebViewNotificationDelegate -- (void)webViewDidStartLoad:(UIWebView*)theWebView { - [self.wrappedDelegate webViewDidStartLoad: theWebView]; +- (void)webViewDidStartLoad:(UIWebView*)theWebView +{ + [self.wrappedDelegate webViewDidStartLoad:theWebView]; [[NSNotificationCenter defaultCenter] postNotification:[NSNotification notificationWithName:kCDVHostedWebAppWebViewDidStartLoad object:theWebView]]; } -- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType { +- (BOOL)webView:(UIWebView*)webView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType +{ [[NSNotificationCenter defaultCenter] postNotification:[NSNotification notificationWithName:kCDVHostedWebAppWebViewShouldStartLoadWithRequest object:request]]; return [self.wrappedDelegate webView:webView shouldStartLoadWithRequest:request navigationType:navigationType]; } -- (void)webViewDidFinishLoad:(UIWebView *)webView { +- (void)webViewDidFinishLoad:(UIWebView*)webView +{ [self.wrappedDelegate webViewDidFinishLoad:webView]; [[NSNotificationCenter defaultCenter] postNotification:[NSNotification notificationWithName:kCDVHostedWebAppWebViewDidFinishLoad object:webView]]; } -- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error { +- (void)webView:(UIWebView*)webView didFailLoadWithError:(NSError*)error +{ [self.wrappedDelegate webView:webView didFailLoadWithError:error]; [[NSNotificationCenter defaultCenter] postNotification:[NSNotification notificationWithName:kCDVHostedWebAppWebViewDidFailLoadWithError object:error]]; @@ -49,7 +53,7 @@ @implementation CDVHostedWebApp @synthesize manifest; -static NSString * const defaultManifestFileName = @"manifest.json"; +static NSString* const defaultManifestFileName = @"manifest.json"; - (void)pluginInitialize { @@ -115,8 +119,8 @@ - (void)pluginInitialize } // loads the specified W3C manifest --(void) loadManifest:(CDVInvokedUrlCommand *)command { - +- (void)loadManifest:(CDVInvokedUrlCommand*)command +{ CDVPluginResult* pluginResult = nil; NSString* manifestFileName = [command.arguments objectAtIndex:0]; @@ -131,8 +135,8 @@ -(void) loadManifest:(CDVInvokedUrlCommand *)command { } // returns the currently loaded manifest --(void) getManifest:(CDVInvokedUrlCommand *)command { - +- (void)getManifest:(CDVInvokedUrlCommand*)command +{ CDVPluginResult* pluginResult = nil; if (self.manifest != nil) { pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDictionary:self.manifest]; @@ -144,32 +148,32 @@ -(void) getManifest:(CDVInvokedUrlCommand *)command { } // enables offline page support --(void) enableOfflinePage:(CDVInvokedUrlCommand *)command { - +- (void)enableOfflinePage:(CDVInvokedUrlCommand*)command +{ self.enableOfflineSupport = YES; - CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsBool:true]; + CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsBool:true]; [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId]; } // disables offline page support --(void) disableOfflinePage:(CDVInvokedUrlCommand *)command { - +- (void)disableOfflinePage:(CDVInvokedUrlCommand*)command +{ self.enableOfflineSupport = NO; CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsBool:true]; [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId]; } --(void) injectPluginScript:(CDVInvokedUrlCommand *)command { - +- (void)injectPluginScript:(CDVInvokedUrlCommand*)command +{ NSArray* scriptList = @[[command.arguments objectAtIndex:0]]; - BOOL result = [self injectScripts: scriptList]; + BOOL result = [self injectScripts:scriptList]; CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsBool:result]; [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId]; } // loads a manifest file and parses it --(NSDictionary *) loadManifestFile:(NSString *)manifestFileName { - +- (NSDictionary*)loadManifestFile:(NSString*)manifestFileName +{ self.manifestError = nil; if (manifestFileName == nil) { @@ -182,13 +186,13 @@ -(NSDictionary *) loadManifestFile:(NSString *)manifestFileName { return nil; } - NSData *manifestData = [NSData dataWithContentsOfFile:filePath]; + NSData* manifestData = [NSData dataWithContentsOfFile:filePath]; if (manifestData == nil) { self.manifestError = [NSString stringWithFormat:@"Error reading manifest file: %@", manifestFileName]; return nil; } - NSError *error = nil; + NSError* error = nil; id parsedManifest = [NSJSONSerialization JSONObjectWithData:manifestData options:0 error:&error]; if (error) { @@ -207,18 +211,18 @@ -(NSDictionary *) loadManifestFile:(NSString *)manifestFileName { return nil; } --(BOOL) injectScripts:(NSArray *)scriptList { - +- (BOOL)injectScripts:(NSArray*)scriptList +{ NSString* content = @""; for (NSString* scriptName in scriptList) { NSURL* scriptUrl = [NSURL URLWithString:scriptName relativeToURL:[NSURL URLWithString:@"www/"]]; NSString* scriptPath = scriptUrl.absoluteString; - NSError *error = nil; + NSError* error = nil; NSString* fileContents = nil; if (scriptUrl.scheme == nil) { - fileContents = [NSString stringWithContentsOfFile: [[NSBundle mainBundle] pathForResource: scriptPath ofType:nil] encoding:NSUTF8StringEncoding error:&error]; + fileContents = [NSString stringWithContentsOfFile:[[NSBundle mainBundle] pathForResource:scriptPath ofType:nil] encoding:NSUTF8StringEncoding error:&error]; } else { @@ -237,7 +241,7 @@ -(BOOL) injectScripts:(NSArray *)scriptList { return[(UIWebView*)self.webView stringByEvaluatingJavaScriptFromString:content] != nil; } -- (BOOL) isCordovaEnabled +- (BOOL)isCordovaEnabled { BOOL enableCordova = NO; NSObject* setting = [self.manifest objectForKey:@"mjs_api_access"]; @@ -274,7 +278,7 @@ - (BOOL) isCordovaEnabled return enableCordova; } --(BOOL) isMatchingRuleForPage:(NSDictionary*) rule withPlatformCheck: (BOOL) checkPlatform +- (BOOL)isMatchingRuleForPage:(NSDictionary*)rule withPlatformCheck:(BOOL)checkPlatform { // ensure rule applies to current platform if (checkPlatform) @@ -307,7 +311,7 @@ -(BOOL) isMatchingRuleForPage:(NSDictionary*) rule withPlatformCheck: (BOOL) che NSArray* match = nil; if ([setting isKindOfClass:[NSArray class]]) { - match = (NSArray*) setting; + match = (NSArray*)setting; } else if ([setting isKindOfClass:[NSString class]]) { @@ -316,7 +320,7 @@ -(BOOL) isMatchingRuleForPage:(NSDictionary*) rule withPlatformCheck: (BOOL) che if (match != nil) { - CDVWhitelist *whitelist = [[CDVWhitelist alloc] initWithArray:match]; + CDVWhitelist* whitelist = [[CDVWhitelist alloc] initWithArray:match]; NSURL* url = ((UIWebView*)self.webView).request.URL; isURLMatch = [whitelist URLIsAllowed:url]; } @@ -350,7 +354,7 @@ - (void)createOfflineView [self.offlineView loadRequest:[NSURLRequest requestWithURL:offlinePageURL]]; } else { - NSString *offlinePageTemplate = @"
%@
"; + NSString* offlinePageTemplate = @"
%@
"; [self.offlineView loadHTMLString:[NSString stringWithFormat:offlinePageTemplate, @"It looks like you are offline. Please reconnect to use this application."] baseURL:nil]; @@ -380,7 +384,7 @@ - (void)updateConnectivityStatus:(CDVReachability*)reachability } else { if (self.failedURL) { - [(UIWebView*)self.webView loadRequest: [NSURLRequest requestWithURL: self.failedURL]]; + [(UIWebView*)self.webView loadRequest:[NSURLRequest requestWithURL:self.failedURL]]; } else { [self.offlineView setHidden:YES]; @@ -440,18 +444,18 @@ - (void)webViewDidFinishLoad:(NSNotification*)notification NSMutableArray* scripts = [[NSMutableArray alloc] init]; if ([pluginMode isEqualToString:@"client"]) { - [scripts addObject: @"cordova.js"]; + [scripts addObject:@"cordova.js"]; } - [scripts addObject: @"hostedapp-bridge.js"]; - [self injectScripts: scripts]; + [scripts addObject:@"hostedapp-bridge.js"]; + [self injectScripts:scripts]; } // inject custom scripts NSObject* setting = [self.manifest objectForKey:@"mjs_import_scripts"]; if (setting != nil && [setting isKindOfClass:[NSArray class]]) { - NSArray* customScripts = (NSArray*) setting; + NSArray* customScripts = (NSArray*)setting; if (customScripts != nil && customScripts.count > 0) { for (NSDictionary* item in customScripts) @@ -459,7 +463,7 @@ - (void)webViewDidFinishLoad:(NSNotification*)notification if ([self isMatchingRuleForPage:item withPlatformCheck:NO]) { NSString* source = [item valueForKey:@"src"]; - [self injectScripts: @[source]]; + [self injectScripts:@[source]]; } } } @@ -492,7 +496,7 @@ - (void)didWebViewFailLoadWithError:(NSNotification*)notification } #ifndef __CORDOVA_4_0_0 -- (BOOL) shouldOverrideLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType +- (BOOL)shouldOverrideLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType { NSURL* url = [request URL]; @@ -509,7 +513,7 @@ - (BOOL) shouldOverrideLoadWithRequest:(NSURLRequest*)request navigationType:(UI } #endif --(BOOL) shouldAllowNavigation:(NSURL*) url +- (BOOL)shouldAllowNavigation:(NSURL*)url { NSMutableArray* scopeList = [[NSMutableArray alloc] initWithCapacity:0]; @@ -540,12 +544,12 @@ -(BOOL) shouldAllowNavigation:(NSURL*) url NSObject* setting = [self.manifest objectForKey:@"mjs_access_whitelist"]; if (setting != nil && [setting isKindOfClass:[NSArray class]]) { - NSArray* accessRules = (NSArray*) setting; + NSArray* accessRules = (NSArray*)setting; if (accessRules != nil) { for (NSDictionary* rule in accessRules) { - NSString *accessUrl = [rule objectForKey:@"url"]; + NSString* accessUrl = [rule objectForKey:@"url"]; if (accessUrl != nil) { [scopeList addObject:accessUrl]; @@ -558,7 +562,7 @@ -(BOOL) shouldAllowNavigation:(NSURL*) url setting = [self.manifest objectForKey:@"mjs_extended_scope"]; if (setting != nil && [setting isKindOfClass:[NSArray class]]) { - NSArray* scopeRules = (NSArray*) setting; + NSArray* scopeRules = (NSArray*)setting; if (scopeRules != nil) { for (NSString* rule in scopeRules) @@ -573,7 +577,7 @@ -(BOOL) shouldAllowNavigation:(NSURL*) url // Updates the network connectivity status when the app is paused or resumes // NOTE: for onPause and onResume, calls into JavaScript must not call or trigger any blocking UI, like alerts -- (void) appStateChange +- (void)appStateChange { CDVConnection* connection = [self.commandDelegate getCommandInstance:@"NetworkStatus"]; [self updateConnectivityStatus:connection.internetReach]; From 30d0570189dc962bb717d99d2a263bba4395882b Mon Sep 17 00:00:00 2001 From: Fernando Tubio Date: Thu, 9 Jun 2016 15:03:33 -0300 Subject: [PATCH 2/2] Removed unused method when target is cordova-ios-4.x or newer --- src/ios/CDVHostedWebApp.m | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ios/CDVHostedWebApp.m b/src/ios/CDVHostedWebApp.m index 2f99d6f..35f2757 100644 --- a/src/ios/CDVHostedWebApp.m +++ b/src/ios/CDVHostedWebApp.m @@ -511,7 +511,6 @@ - (BOOL)shouldOverrideLoadWithRequest:(NSURLRequest*)request navigationType:(UIW return NO; } -#endif - (BOOL)shouldAllowNavigation:(NSURL*)url { @@ -574,6 +573,7 @@ - (BOOL)shouldAllowNavigation:(NSURL*)url return [[[CDVWhitelist alloc] initWithArray:scopeList] URLIsAllowed:url]; } +#endif // Updates the network connectivity status when the app is paused or resumes // NOTE: for onPause and onResume, calls into JavaScript must not call or trigger any blocking UI, like alerts