Is it possible to update the content of WWW folder in phonegap without submitting the app every time you make changes to appStore? Is there any legal issue with it?
Short answer - yes, you can - but you need to relocate www before you can write to it.
Long answer - tighten up your shorts pilgrim, this might get bumpy....
So, when you package your app, the app bundle contains your www directory and all of the various and sundry parts of your PG self contained web app. Everything that is part of the app bundle is static - so it cannot be written to. You can, however, write to the NSDocumentDirectory for your application - it's private sandboxed storage area.
The first step is to (upon launch) copy your www folder into the NSDocumentDirectory.
Second, you'll need to override the CDVCommandDelegateImpl pathForResource method in order to point PG at the new www location.
At this point your app should be operating as it did when contained within the app bundle. One notable exception is that each of the files can now be modified.
My app is a base product which can have add-on content. If I included all of the add-on content, the app would be several hundred MB in size. So, I include the basic app with a single (sample) data package, and as users need to extend the product, they select additional data packages which are downloaded, extracted and copied into the appropriate subdirectory of the www folder as it exists in the NSDocumentDirectory.
As a bonus, if I find a bug in HTML - or want to add a feature, I can do so without resubmitting the app. The only reason I need to resubmit the app is if there is an Objective-C bugfix.
This is just adding onto Michael's answer.
1 ) Copy your www folder into the NSDocumentDirectory: - for phonegap ios, add this to /ProjectName/Classes/AppDelegate.m
- (BOOL)application:(UIApplication*)application didFinishLaunchingWithOptions:(NSDictionary*)launchOptions { CGRect screenBounds = [[UIScreen mainScreen] bounds]; self.window = [[[UIWindow alloc] initWithFrame:screenBounds] autorelease]; self.window.autoresizesSubviews = YES; self.viewController = [[[MainViewController alloc] init] autorelease]; //self.viewController.useSplashScreen = YES; // Set your app's start page by setting the tag in config.xml. // If necessary, uncomment the line below to override it. // self.viewController.startPage = @"index.html"; // NOTE: To customize the view's frame size (which defaults to full screen), override // [self.viewController viewWillAppear:] in your view controller. self.window.rootViewController = self.viewController; [self.window makeKeyAndVisible]; if ([[NSUserDefaults standardUserDefaults] boolForKey:@"AppFirstLaunch"]) { NSLog(@"App already launched!"); } else { [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"AppFirstLaunch"]; [[NSUserDefaults standardUserDefaults] synchronize]; NSLog(@"App launched for first time!"); BOOL success1; NSError *error1; NSFileManager *fileManager1 = [NSFileManager defaultManager]; fileManager1.delegate = self; NSArray *paths1 = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *documentsDirectory1 = [paths1 objectAtIndex:0]; NSString *writableDBPath1 = [documentsDirectory1 stringByAppendingPathComponent:@"/www"]; success1 = [fileManager1 fileExistsAtPath:writableDBPath1]; if ( success1 ) { NSString *defaultDBPath1 = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"www"]; NSLog(@"default SUCCESS / AppDelegate path %@",defaultDBPath1); success1 = [fileManager1 copyItemAtPath:defaultDBPath1 toPath:writableDBPath1 error:&error1]; } else { if (![[NSFileManager defaultManager] fileExistsAtPath:writableDBPath1]) [[NSFileManager defaultManager] createDirectoryAtPath:writableDBPath1 withIntermediateDirectories:NO attributes:nil error:&error1]; } } return YES; }
2 ) To override the default path ( orig. set to www folder ), within CordovaLib/Classes/CDVCommandDelegateImpl.m, replace the body of the function:
-(NSString*) pathForResource:(NSString*)resourcepath
with:
- (NSString*)wwwFolderName { NSArray *searchPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); return [NSString stringWithFormat:@"%@/www",[searchPaths objectAtIndex:0]]; } - (NSString*) pathForResource:(NSString*)resourcepath { NSBundle* mainBundle = [NSBundle mainBundle]; NSMutableArray* directoryParts = [NSMutableArray arrayWithArray:[resourcepath componentsSeparatedByString:@"/"]]; NSString* filename = [directoryParts lastObject]; [directoryParts removeLastObject]; NSString* directoryPartsJoined = [directoryParts componentsJoinedByString:@"/"]; NSString* directoryStr = [self wwwFolderName]; if ([directoryPartsJoined length] > 0) { directoryStr = [NSString stringWithFormat:@"%@/%@", [self wwwFolderName], [directoryParts componentsJoinedByString:@"/"]]; } NSLog(@"default path %@",directoryStr); if (![[self wwwFolderName] isEqualToString:@"www"]) { return [NSString stringWithFormat:@"%@/%@",[self wwwFolderName],@"shared/index.html"]; } return [mainBundle pathForResource:filename ofType:@"" inDirectory:directoryStr]; }
Other References:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With