Phonegap - 更新JavaScript和CSS而无需将应用程序提交到appStore

Ard*_*ori 14 app-store cordova

每次更改appStore时,是否可以在不提交应用程序的情况下更新phonegap中的WWW文件夹内容?它有任何法律问题吗?

Mic*_*ael 21

简短回答 - 是的,你可以 - 但你需要重新定位www才能写入它.

答案很长 - 收紧你的短裤朝圣者,这可能会变得坎坷......

因此,当您打包应用程序时,应用程序包中包含您的www目录以及PG自包含Web应用程序的所有各种各样的部分.作为应用程序包的一部分的所有内容都是静态的 - 因此无法写入.但是,您可以为您的应用程序写入NSDocumentDirectory - 它是私有沙盒存储区域.

第一步是(在启动时)将您的www文件夹复制到NSDocumentDirectory中.

其次,您需要覆盖CDVCommandDelegateImpl pathForResource方法,以便将PG指向新的www位置.

此时,您的应用应该像应用包中包含的那样运行.一个值得注意的例外是现在可以修改每个文件.

我的应用程序是一个基本产品,可以有附加内容.如果我包含所有附加内容,该应用程序的大小将为几百MB.因此,我将基本应用程序包含在一个(样本)数据包中,并且当用户需要扩展产品时,他们会选择其他数据包,这些数据包会被下载,提取并复制到www文件夹的相应子目录中,因为它存在于NSDocumentDirectory.

作为奖励,如果我发现HTML中的错误 - 或想要添加功能,我可以这样做而无需重新提交应用程序.我需要重新提交应用程序的唯一原因是,如果有一个Objective-C错误修复程序.

  • 你能把完成这个的代码放在github或其他地方吗?我真的很感激能够看到一份工作副本. (5认同)

小智 19

这只是加入迈克尔的答案.

1)将您的www文件夹复制到NSDocumentDirectory: - 对于phonegap ios,将其添加到/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)要覆盖默认路径(orig.设置为www文件夹),在CordovaLib/Classes/CDVCommandDelegateImpl.m中,替换函数体:

- (NSString*)pathForResource:(NSString*)resourcepath


有:


- (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];
}


其他参考文献: