I am attempting to write a bit of code that checks the URL of a datasource, then populates an array with objects from that URL. It actually works well, but if there is a problem with the web connection or the address I want to populate the array with data from a bundled file. The issue I am having is that the connection didFailWithError
method is never called. I tried passing a simple string but it does not call. I want the app to still function for people who are using ipod touch or are in airplane mode.
<编码>接线的确是被动反应。
这是我所干的。
- (void)loadListData{
NSLog(@"Loading data from sources");
NSURLRequest *listURLRequest = [NSURLRequest requestWithURL:integerPhoneListURL cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:1.0];
[[NSURLConnection alloc] initWithRequest:listURLRequest delegate:self];
if (!listConnectFail){
phoneListJSON =[NSData dataWithContentsOfURL:integerPhoneListURL];
[self performSelectorOnMainThread:@selector(fetchedData:) withObject:phoneListJSON waitUntilDone:YES];
} else {
//This will tell us if there is an error loading the file
NSLog(@"File not found on web init from file");
phoneListJSON =[NSData dataWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"contactlist" ofType:@"json"]];
[self performSelectorOnMainThread:@selector(fetchedData:) withObject:phoneListJSON waitUntilDone:YES];
}
//Initialize the filtered list with array of customer objects. Based on original data
filteredList = [[NSMutableArray alloc] init];
for (NSDictionary *dict in phoneListOriginal) {
contact *single = [[contact alloc] init];
single.fName = [dict objectForKey:@"fName"];
single.lName = [dict objectForKey:@"lName"];
single.extension = [dict objectForKey:@"extension"];
single.title = [dict objectForKey:@"title"];
single.department = [dict objectForKey:@"department"];
single.cellNumber = [dict objectForKey:@"cellNumber"];
//NSLog(@"%@", single.lName);
[filteredList addObject:single];
}
NSLog(@"Array filteredLIst contains %d records",[filteredList count]); }
-(void) connection:(NSURLConnection *)connection didFailWithError:(NSError *)error{
listConnectFail = YES;
NSLog(@"Connection Failed, pulling from file"); }
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response { listConnectFail = NO; NSLog(@"Connection Succeeded, populating from API"); }
我知道,我可能不看到,这是一件 st事,但我可以利用帮助看看我看不到什么。
提前感谢!