从 Web 服务器加载 PLIST

发布于 2025-01-06 11:07:12 字数 764 浏览 2 评论 0原文

尝试能够从 URL 加载 plist 并使其覆盖当前的 plist。下面的代码可以在模拟器上运行,但是在 iPhone 4 设备上检查时它不会拉取更新的内容?对于 Xcode 新手,您有什么宝贵的建议吗?

Reachability *r = [Reachability reachabilityWithHostName:@"www.google.com"];
NetworkStatus internetStatus = [r currentReachabilityStatus];

if ((internetStatus == ReachableViaWiFi) || (internetStatus == ReachableViaWWAN)){

    NSString *path = [[NSBundle mainBundle] bundlePath];
    NSString *finalPath = [path stringByAppendingPathComponent:@"SpecialCases.plist"];
    NSURL *theFileURL = [NSURL URLWithString:@"http://www.awebsite.com"];
    NSDictionary *replace = [[NSDictionary alloc] initWithContentsOfURL:theFileURL];
    if (replace != nil){
    [replace writeToFile:finalPath
                 atomically:YES];
    }
}

Trying to have the ability to load a plist from a URL and have it overwrite the current plist that is there. The code below works on the simulator but when checking on an iPhone 4 device it doesn't pull down the updated content? Any words of wisdom for an new guy to Xcode?

Reachability *r = [Reachability reachabilityWithHostName:@"www.google.com"];
NetworkStatus internetStatus = [r currentReachabilityStatus];

if ((internetStatus == ReachableViaWiFi) || (internetStatus == ReachableViaWWAN)){

    NSString *path = [[NSBundle mainBundle] bundlePath];
    NSString *finalPath = [path stringByAppendingPathComponent:@"SpecialCases.plist"];
    NSURL *theFileURL = [NSURL URLWithString:@"http://www.awebsite.com"];
    NSDictionary *replace = [[NSDictionary alloc] initWithContentsOfURL:theFileURL];
    if (replace != nil){
    [replace writeToFile:finalPath
                 atomically:YES];
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

习ぎ惯性依靠 2025-01-13 11:07:12

您无法在 iOS 设备上写入应用程序包,因为它不属于您可以编辑的沙箱的一部分。您必须将文件保存到文档目录,如下所示:

NSString *path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *finalPath = [path stringByAppendingPathComponent:@"SpecialCases.plist"];

然后从那里加载 plist,而不是您的应用程序包。

You cannot write to your application's bundle on an iOS device, as that isn't part of the sandbox you are allowed to edit. You must save your file to the documents directory, like this:

NSString *path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *finalPath = [path stringByAppendingPathComponent:@"SpecialCases.plist"];

And then later load the plist from there, not your app bundle.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文