【问题标题】:How to update App files (html) in IOS?如何在 IOS 中更新 App 文件(html)?
【发布时间】:2012-06-23 06:27:55
【问题描述】:

我的应用程序非常简单。我有一些带有 html 文件名的 plist。当用户选择一行时,webview 会加载该 html 文件及其内容。我很好奇如何处理应用程序更新?例如:我有一个新的更新的 html 文件,其中包含更正,我希望用户通过单击应用程序中的按钮来下载该文件。我希望将旧文件(html)替换为新文件。有没有办法做到这一点?不太确定从哪里开始,希望有人能指出我正确的方向。我假设,没有办法更新 plist,我必须从 plist 切换到数据库,但问题仍然存在,如何让应用程序下载新文件并替换旧文件?

谢谢

只是为了详细说明。

我想我想找出在我的应用程序中完成更新的正确方法。我的 html 文件在应用程序中,我假设我可能让应用程序最初连接到互联网并下载最新的内容文件(html、plist),将它们放入文档文件夹,然后更新我的主屏幕 plist 文件?这是正确的思路吗?

我不希望应用程序只能在互联网上使用,需要那些 html 文件可以离线使用。

【问题讨论】:

  • 只需发布带有更新的 plist 和 HTML 文件的新版本的应用程序?
  • HTML 文件存储在哪里?在应用程序本身内(在这种情况下@Devraj 的评论是有效的)或者它们是由应用程序下载并存储在其他地方(例如在“文档”目录中),还是它们是从远程服务器托管和查看的?还是?
  • 如果它们存储在远程服务器上,只需更新远程服务器上的文件。
  • 我知道我可以发布新版本的应用程序,但审批过程需要 7 天左右。如果我必须纠正一个小错字,我希望能避免这种情况。另一方面,由于我并没有真正更改程序,因此将所有新的 html 文件推送到应用程序并更新 plist 引用会很有用。将文件托管在远程服务器上并让用户访问它们很容易,但是当用户没有互联网连接和应用程序的整个目的时就会出现问题,即使有没有互联网连接。
  • HTML 文件存储在应用程序中,但现在我想一想,将它们存储在文档文件夹中是否有意义?只需将应用程序连接到互联网即可下载最新内容。感谢所有回复。

标签: javascript objective-c ios html


【解决方案1】:

您可以将 HTML 文件存储在外部服务器上。并更改您的应用程序以从您的外部服务器加载 HTML 文件,而不是从您的应用程序包。因此,如果您需要更新任何 HTML 文件的内容,您可以在您的服务器上进行。

【讨论】:

  • 在远程服务器上托管文件并让用户访问它们很容易,但是当用户没有互联网连接和应用程序的全部目的时,问题就出现了即使没有互联网连接,这些数据也可用。
  • 当然。您对原始问题的编辑使其更加清晰,这似乎是正确的道路。
【解决方案2】:

如果你能得到新的html文件名,你可以直接写到你的plist文件中。

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
    NSUserDomainMask, YES);
NSString *docPath = [[paths objectAtIndex:0] stringByAppendingPathComponent:@"settings.plist"];
NSMutableDictionary* newValueDict=[[NSMutableDictionary alloc]initWithContentsOfFile:docPath];
[newValueDict setObject:newHtmlPageValue forKey:@"myHtmlPage"];
[newValueDict writeToFile:docPath atomically:NO];
[newValueDict release];

【讨论】:

    【解决方案3】:

    正如许多人建议的那样,我想我的计划是从网上下载内容,将其保存在库文件夹中,然后从那里加载内容。在应用程序中,我将有一个更新按钮,只要用户选择,它就会连接到服务器并更新内容。在 apple.com (https://developer.apple.com/library/ios/#documentation/Cocoa/Conceptual/URLLoadingSystem/Tasks/UsingNSURLConnection.html) 上找到示例代码

    稍作调整,它应该可以正常工作,如果尝试下载多个文件,这也可能会有所帮助。 Downloading multiple files in iphone app(Objective c)

    - (IBAction)down
    {
        // Create the request.
        NSURLRequest *theRequest=[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://abc.com/missing.html"]
                                                  cachePolicy:NSURLRequestUseProtocolCachePolicy
                                              timeoutInterval:60.0];
        // create the connection with the request
        // and start loading the data
        NSURLConnection *theConnection=[[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
        NSString *msg;
        if (theConnection) {
            // Create the NSMutableData to hold the received data.
            // receivedData is an instance variable declared elsewhere.
            receivedData = [[NSMutableData data] retain];
    
    
    
        } else {
            // Inform the user that the connection failed.
    
    
        }
    
    }
    
    
    - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
    {
        // This method is called when the server has determined that it
        // has enough information to create the NSURLResponse.
    
        // It can be called multiple times, for example in the case of a
        // redirect, so each time we reset the data.
    
    
        // receivedData is an instance variable declared elsewhere.
        [receivedData setLength:0];
    }
    
    - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
    {
        // Append the new data to receivedData.
        //You can also use the connection:didReceiveData: method to provide an indication of the connection’s progress to the user.
        // receivedData is an instance variable declared elsewhere.
    
        NSString *msg = [NSString stringWithFormat:@"data %@",data,nil ];
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"-download-" 
                                                        message:msg
                                                       delegate:nil 
                                              cancelButtonTitle:@"OK" 
                                              otherButtonTitles: nil];
        [alert show];
        [alert release];
       //NSLog ([[NSString alloc] initWithFormat:@"The value: %d  object: %@\n", alphArray.count, [alphArray objectAtIndex:1.1],nil]);
    
        NSString *string = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding];
        NSLog(@"the data %@",string);
    
        NSString *libraryPath = [NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES) objectAtIndex:0];
        NSString *filePath = [libraryPath stringByAppendingPathComponent:@"Missing.html"];
    
        NSLog(@"Full path: %@", filePath);
    
        //NSArray *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    
    
        //write data to files
        [data writeToFile:filePath atomically:YES];
    
        [receivedData appendData:data];
    }
    
    - (void)connection:(NSURLConnection *)connection
      didFailWithError:(NSError *)error
    {
        // release the connection, and the data object
        [connection release];
        // receivedData is declared as a method instance elsewhere
        [receivedData release];
    
        // inform the user
        NSLog(@"Connection failed! Error - %@ %@",
              [error localizedDescription],
              [[error userInfo] objectForKey:NSURLErrorFailingURLStringErrorKey]);
    }
    
    - (void)connectionDidFinishLoading:(NSURLConnection *)connection
    {
        // do something with the data
        // receivedData is declared as a method instance elsewhere
        NSLog(@"Succeeded! Received %d bytes of data",[receivedData length]);
    
        // release the connection, and the data object
        [connection release];
        [receivedData release];
    }
    
    -(NSCachedURLResponse *)connection:(NSURLConnection *)connection
                     willCacheResponse:(NSCachedURLResponse *)cachedResponse
    {
        NSCachedURLResponse *newCachedResponse = cachedResponse;
    
        if ([[[[cachedResponse response] URL] scheme] isEqual:@"https"]) {
            newCachedResponse = nil;
        } else {
            NSDictionary *newUserInfo;
            newUserInfo = [NSDictionary dictionaryWithObject:[NSDate date]
                                                      forKey:@"Cached Date"];
            newCachedResponse = [[[NSCachedURLResponse alloc]
                                  initWithResponse:[cachedResponse response]
                                  data:[cachedResponse data]
                                  userInfo:newUserInfo
                                  storagePolicy:[cachedResponse storagePolicy]]
                                 autorelease];
             NSLog(@"Succeeded! Receive use:%@",newUserInfo);
        }
        NSLog(@"Succeeded! Received %@ - use:%@",newCachedResponse);
        return newCachedResponse;
    }
    

    【讨论】:

      【解决方案4】:

      答案正是你所描述的。

      我最常用的方法是检查文档中是否存在,如果不存在则从包中添加它们。

      使用它们,并在后台下载新的。 (如果互联网可用)。 如果成功下载,请用新的替换旧的。

      并设置某种类型的超时,这样您就不会每秒下载一个新的。

      我喜欢暂停一天。

      并实现一种与服务器检查版本的方法。 (MD5 Hash 和文件大小是很好的,更不用说小比较了)

      祝你好运

      【讨论】:

      • 谢谢,您将文件保存在库中还是文档文件夹中?
      • 我将文件放在字典文件夹中的一个文件夹中。只需确保您有办法使这些文件过期并覆盖旧文件。如果您每次都添加新的,那么您最终会填满设备,这是一种不好的做法。
      • 是的,我明白,我的计划是在下载文件时立即覆盖库文件夹中的旧文件
      • 我建议将它们下载到一个地方。然后在下载成功后替换它们。从我的错误中学习。我必须重新编写我的代码,因为我这样做了。我的项目是图像,但结果是一样的。下载失败并覆盖文件,您将一无所有。-
      猜你喜欢
      • 1970-01-01
      • 2013-02-04
      • 1970-01-01
      • 1970-01-01
      • 2012-06-30
      • 1970-01-01
      • 2014-03-10
      • 2014-09-16
      • 1970-01-01
      相关资源
      最近更新 更多