iphone - Objective C,下载、检查并保持 plist 文件可用

标签 iphone objective-c plist asihttprequest

好吧,我今天/本周有点精神障碍,所以我可能很愚蠢,但我无法让它发挥作用。因此,任何建议/帮助一如既往,我们将不胜感激......

我需要我的 iPhone 应用程序,根据请求从服务器下载一次 plist 文件,并在应用程序加载时随时使用它(IE 实际上只需要下载一次)。但我真的不认为我做得对......我正在使用 ASIHTTPRequest 来获取文件,并且它在单独的线程中运行以允许进度条更新。下载在首次运行时或根据请求(目前)开始。

我没有收到任何错误,并且正在联系服务器,但我不知道文件是否/保存到何处。我的测试似乎表明该文件没有被下载。

所以我想我的问题是:

  • 我应该将文件保存在哪里?对此有什么规则吗?或者它会保留在手机上的应用程序本身中吗?
  • 下载后,此文件在应用运行时是否可用?
  • 有更好的方法吗?

下面的一些代码:

-(void)getPlist
{
    NSArray *docpaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [docpaths objectAtIndex:0];

    NSURL *url = [NSURL URLWithString:plistURL];
    ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
    [request setDownloadDestinationPath:documentsDirectory];
    [request setDownloadProgressDelegate:progbar];
    [request startSynchronous];
}

提前致谢/抱歉,如果这是愚蠢的n00bish,仍然非常了解这个 Objective-C 业务。

最佳答案

如果您使用 ASIHTTPRequest,只需使用它的内置缓存系统。不要推出自己的。它非常简单且易于管理

ASIHTTPRequest can automatically store downloaded data in a cache for use later. This can be helpful in many situations:

You want to have access to the data when there is no internet connection and you can’t download it again You want to download something only if it has changed since you last downloaded it The content you are working with will never change, so you only want to download it once

http://allseeing-i.com/ASIHTTPRequest/How-to-use#using_a_download_cache

回答您原来的问题

我应该将文件保存在哪里?对此有什么规则吗?或者它会保留在手机上的应用程序本身中吗?

只需将其保存到应用程序的文档目录中即可。它是沙盒的。

下载后,该文件在运行应用程序时是否可用?

是的。但是,如果用户删除了您的应用程序并重新安装了它,您的所有缓存文件都将被删除。处理好情况。

有更好的方法吗?

我认为您当前的实现没有任何问题。如果遇到问题,请检查请求对象是否有错误和/或检查返回的 HTTP StatusCode int statusCode = [request responseStatusCode];

获取您的文档路径(设备内部)路径:

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];

关于iphone - Objective C,下载、检查并保持 plist 文件可用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6613329/

相关文章:

iphone - 对 CALayer 子类的自定义属性进行动画处理

ios - 如何更改 UIImageView 中单个像素的图像

iphone - 我怎样才能让我的 UITableViewCell 包含两个标签?

macos - 发货时如何在 Retina 显示屏上为应用程序启用 "use low resolution"?

javascript - 在表单中捕获 iPhone 键盘下一个按钮按下事件

iphone - 当 View Controller 加载时强制我的 View Controller 横向显示

iphone - UIAlertView 后台问题?

iOS 代码 : How to load a value from a user-defined setting from build settings at run time

macos - Mac 上的应用程序首选项文件位置在哪里

ios - 在 IOS 中从 plist 中获取字符串值