2012-02-15 56 views
0

嘗試從URL加載plist並覆蓋當前plist的能力。下面的代碼在模擬器上工作,但在檢查iPhone 4設備時,它不會下拉更新的內容? Xcode的新人有什麼智慧的話?從網絡服務器加載一個PLIST

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]; 
    } 
} 
+0

當你說「它不下拉更新的內容」時,你究竟是什麼意思?設置一個斷點並逐步完成代碼 - 它是否會進入您的條件分支?檢查變量的值。是替換零,任何錯誤拋出? – 2012-02-15 16:24:58

+0

在這種情況下檢查可達性狀態沒有多大意義。只需繼續並下載plist _in background_。如果下載失敗,你會知道。你有沒有嘗試評論'if'? – Costique 2012-02-15 16:26:16

+0

是的,我沒有想到如果在那裏陳述,直到後來。我已經拿出可達性了。 – 2012-02-16 00:54:10

回答

3

您不能在iOS設備上寫入應用程序的包,因爲這不是您允許編輯的沙箱的一部分。您必須將文件保存到文檔目錄,如下所示:

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

然後再從那裏加載plist,而不是您的應用程序包。

+0

啊......這很有道理。我會試一試。 – 2012-02-16 00:52:55

+0

只是不要忘記接受答案,如果它適合你! – 2012-02-16 01:01:20