2013-12-12 100 views
1

我想每次從服務器保存json文件,當用戶有互聯網連接時使用它,當iPhone沒有互聯網連接。但它不起作用。這裏是我的代碼:保存JSON文件到iPhone

- (void)writeJsonToFile 
{ 
    //applications Documents dirctory path 
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString *documentsDirectory = [paths objectAtIndex:0]; 

    //live json data url 
    NSString *stringURL = @"http://volodko.info/ic/json.php"; 
    NSURL *url = [NSURL URLWithString:stringURL]; 
    NSData *urlData = [NSData dataWithContentsOfURL:url]; 

    //attempt to download live data 
    if (urlData) 
    { 
     NSString *filePath = [NSString stringWithFormat:@"%@/%@", documentsDirectory,@"data.json"]; 
     [urlData writeToFile:filePath atomically:YES]; 
    } 
    //copy data from initial package into the applications Documents folder 
    else 
    { 
     //file to write to 
     NSString *filePath = [NSString stringWithFormat:@"%@/%@", documentsDirectory,@"data.json"]; 

     //file to copy from 
     NSString *json = [ [NSBundle mainBundle] pathForResource:@"data" ofType:@"json" inDirectory:@"html/data" ]; 
     NSData *jsonData = [NSData dataWithContentsOfFile:json options:kNilOptions error:nil]; 

     //write file to device 
     [jsonData writeToFile:filePath atomically:YES]; 
    } 
} 
+0

你的意思是不工作? –

+0

您是否嘗試過使用Core Data來滿足您的需求..? – mAc

+0

應用程序崩潰沒有互聯網,但必須使用本地json文件 – Anton

回答

0

你應該在適當的方式創建路徑

替換此

NSString *filePath = [NSString stringWithFormat:@"%@/%@", documentsDirectory,@"data.json"]; 

NSString *filePath = [documentsDirectory stringByAppendingPathComponent:@"data.json"]; 
+0

不工作:(以下是控制檯數據:2013-12-12 14:30:54.420 iPhone中心[301:1503]網絡連接失敗,在睡眠6.849714秒後嘗試4 。 2013-12-12 14:31:13.962 iPhoneCenter [301:60b] ***由於未捕獲的異常'NSInvalidArgumentException'而終止應用程序,原因:'數據參數爲零' – Anton

+0

然後檢查數據是否存在?給出正確路徑的解決方案,首先檢查是否從WerbService下載數據,如果(data.length> 0)則只寫入路徑。 –

0

嘗試使用NSUserDefaults的,而不是寫這篇小JSON文本到文件

- (void)writeJsonToFile 
{ 
    NSString *jsonString; 

    NSString *stringURL = @"http://volodko.info/ic/json.php"; 
    NSURL *url = [NSURL URLWithString:stringURL]; 
    NSData *urlData = [NSData dataWithContentsOfURL:url]; 

    if (urlData) 
    { 
     // JSON successfully downloaded, store it to user defaults 
     jsonString = [[NSString alloc] initWithData:urlData encoding:NSUTF8StringEncoding]; 
     [[NSUserDefaults standardUserDefaults] setValue:jsonString forKey:@"jsonString"]; 
    } 
    else 
    { 
     // no urlData, using stored JSON 
     jsonString = [[NSUserDefaults standardUserDefaults] valueForKey:@"jsonString"]; 
    } 
} 

甚至可能更好:

- (NSString *)getJSON 
{ 
    <the code above> 
    return jsonString; 
} 

,然後用它在其它功能:

NSString *jsonString = [self getJSON]; 
0

安東,在第一時間獲取在nsmutable arrayjson數據和存儲您陣列保持數據時,你再次運行UR陣列不是一個零,但重新播放數據第二次...和另一種方式來存儲數據在local database ...如果你需要存儲的數據..

但我們的問題是解決第一個..幸運..

2

試試這個。 。 。

- (void)writeJsonToFile 
{ 
    //applications Documents dirctory path 
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString *documentsDirectory = [paths objectAtIndex:0]; 

    //live json data url 
    NSString *stringURL = @"http://volodko.info/ic/json.php"; 
    NSURL *url = [NSURL URLWithString:stringURL]; 
    NSData *urlData = [NSData dataWithContentsOfURL:url]; 

    //attempt to download live data 
    if (urlData) 
    { 
     NSString *filePath = [documentsDirectory stringByAppendingPathComponent:@"data.json"]; 
     [urlData writeToFile:filePath atomically:YES]; 
    } 
    //copy data from initial package into the applications Documents folder 
    else 
    { 
     //file to write to 
     NSString *filePath = [documentsDirectory stringByAppendingPathComponent:@"data.json"];; 

     //file to copy from 
     NSString *json = [ [NSBundle mainBundle] pathForResource:@"data" ofType:@"json" inDirectory:@"html/data" ]; 
     NSData *jsonData = [NSData dataWithContentsOfFile:json options:kNilOptions error:nil]; 

     //write file to device 
     [jsonData writeToFile:filePath atomically:YES]; 
    } 
} 
2

這對我有用。閱讀AFJSONRequestOperation指南。代碼還會檢查json文件是否已被緩存。

NSString *path = @"http://volodko.info/ic/json.php"; 
    NSURL *url = [[NSURL alloc] initWithString:path]; 
    NSURLRequest *request = [[NSURLRequest alloc] initWithURL:url]; 

    id cachedJson = [[NSUserDefaults standardUserDefaults] valueForKey:path]; 
    if (cachedJson) { 
     [self didUpdateJSON:cachedJson]; 
    } else { 
     AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) { 
      [self didUpdateJSON:JSON]; 
      dispatch_async(dispatch_get_main_queue(), ^{ 
       [[NSUserDefaults standardUserDefaults] setObject:JSON forKey:path]; 
       [[NSUserDefaults standardUserDefaults] synchronize]; 
      }); 
     } failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) { 
      NSLog(@"Request Failed with Error: %@, %@", error, error.userInfo); 
     }]; 
     [operation start]; 
    } 
2

從JSON解析器獲取數據並將其存儲在數組中。之後,您可以將其添加到SQLite數據庫。