2013-04-24 123 views
0

我從json url寫入plist數組。 我想,當不存在互聯網的tableview從plist中獲取數據,並在存在的tableView得到JSON的URL數據如何在沒有互聯網的情況下從tableView中讀取數據

這是我的代碼(我創建的文檔文件夾中的應用plist文件):

#import "ViewController.h" 
#define DOC_DIR [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0] 


@implementation ViewController 
{ 
    NSMutableArray *name; 
    NSMutableData *data; 
    NSString *listPath; 
    NSMutableArray *array; 
    NSArray *n; 
    NSMutableArray *add; 
} 
@synthesize table; 
-(NSString*)Dir 
{ 
    return [NSSearchPathForDirectoriesInDomains(NSDocumentationDirectory, NSUserDomainMask, YES)objectAtIndex:0]; 
} 
- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    [UIApplication sharedApplication].networkActivityIndicatorVisible = YES; 
    NSURL *url = [NSURL URLWithString:@"http://myDomain.com/test.php"]; 
    NSURLRequest *request = [NSURLRequest requestWithURL:url]; 
    NSURLConnection *con = [[NSURLConnection alloc] initWithRequest:request delegate:self]; 
    [con start]; 


} 

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response 
{ 
    data = [[NSMutableData alloc]init]; 
} 

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)theData 
{ 
    [data appendData:theData]; 
} 

- (void)connectionDidFinishLoading:(NSURLConnection *)connection 
{ 
    [UIApplication sharedApplication].networkActivityIndicatorVisible = NO; 
    name = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil]; 
    for (int i = 0; i < [name count]; i++) { 

    NSIndexPath *indexPath = [self.table indexPathForSelectedRow]; 
    n = [[name objectAtIndex:(indexPath.row)+i]objectForKey:@"title"]; 
    if(!add){ 
     add = [NSMutableArray array]; 
    } 
    [add addObject:n]; 
    } 
    NSLog(@"add = %@",add); 
    [table reloadData]; 
    [self WriteToPlist:add]; 
} 
- (void)WriteToPlist:(NSArray*)dataArray 
{ 
    NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:dataArray,@"Name", nil]; 
    NSString *Path = [DOC_DIR stringByAppendingPathComponent:@"Plist.plist"]; 
    [dic writeToFile:Path atomically:YES]; 
    NSLog(@"Path : %@",Path); 
} 

和驗證碼從JSON讀取數據:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 
    // Return the number of sections. 
    return 1; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    // Return the number of rows in the section. 
    return [name count]; 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"cell"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

    if (cell == nil) { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
    } 
    cell.textLabel.text = [[name objectAtIndex:indexPath.row] objectForKey:@"title"]; 
//top code read data from json and I want when no internet read data from plist 
    return cell; 
} 

回答

0
// Path to the plist (in the application bundle) 
NSString *path = [[NSBundle mainBundle] pathForResource: 
    @"Plist" ofType:@"plist"]; 

// Build the array from the plist 
NSMutableArray *array2 = [[NSMutableArray alloc] initWithContentsOfFile:path]; 

// Show the string values 
for (NSString *str in array2) 
    NSLog(@"--%@", str); 

你有數組現在只值設置爲數據源陣列

[tableview reloadData]; 

爲了檢查互聯網連接,你可以使用Reachability class provided by apple

+0

我的朋友如何的tableView顯示,當沒有互聯網(如何在plist文件訪問?) – john 2013-04-24 06:29:17

+0

爲什麼你需要互聯網從plist文件訪問內容?這是一個本地文件。您可以隨時訪問它。 – Anil 2013-04-24 06:37:12

+0

@john Tableview與連接無關,用於檢查連接使用可用性類的可用性。編輯答案與鏈接 – 2013-04-24 06:39:53

0

第1步:檢查互聯網連接或者是不使用下面的方法,實現此方法

- (BOOL) connectedToInternet 
{ 
    NSURL *requestURL = [NSURL URLWithString:@"http://www.google.com"]; 
    NSData *data = [NSData dataWithContentsOfURL:requestURL]; 

    return ([data bytes]>0) ? YES : NO; 
} 

OR

步驟1:如果您知道的話,您也可以實施Reachability Class來檢查Apple的互聯網連接性。

第2步:現在請檢查連接在你的方法Befor調用web服務請求

if([self connectedToInternet]) 
{ 
    // Make a reqeust to server 
    [UIApplication sharedApplication].networkActivityIndicatorVisible = YES; 
    NSURL *url = [NSURL URLWithString:@"http://yourDomain.com/test.php"]; 
    NSURLRequest *request = [NSURLRequest requestWithURL:url]; 
    NSURLConnection *con = [[NSURLConnection alloc] initWithRequest:request delegate:self]; 
    [con start]; 
} 
else 
{ 
    // Write a Handy code to load Data from Your Local Plist 
    // Path to plist From Documents Folder 
    NSString *docFilePath = [DOC_DIR stringByAppendingPathComponent:@"Data.plist"]; 

    NSDictionary *dictData = [NSDictionary dictionaryWithContentsOfFile:docFilePath]; 


    NSLog(@"%@",dictData); 
    // Reload your TableView 
} 
+0

謝謝我的朋友你能幫助我,告訴我如何從文檔文件夾中調用.plist文件並存儲在NSDictionary中? – john 2013-04-24 07:24:27

+0

我編輯了我的代碼,檢查它 – 2013-04-24 07:28:19

相關問題