我正在尝试编写一些代码来检查数据源的 URL,然后用来自该 URL 的对象填充一个数组。它实际上运行良好,但如果 Web 连接或地址出现问题,我想用捆绑文件中的数据填充数组。我遇到的问题是该connection didFailWithError
方法永远不会被调用。我尝试传递一个简单的字符串,但它没有调用。我希望该应用程序仍然适用于使用 ipod touch 或处于飞行模式的人。
connection didReceiveResponse
工作没有问题。
这就是我正在使用的。
- (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]); }
-(无效)连接:(NSURLConnection *)连接didFailWithError:(NSError *)错误{
listConnectFail = YES;
NSLog(@"Connection Failed, pulling from file"); }
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response { listConnectFail = NO; NSLog(@"连接成功,从 API 填充"); }
我知道这可能是我没有看到的愚蠢的东西,但我可以使用帮助来查看我没有看到的东西
提前致谢!