Objective-C的問題,我最近從sendSynchronousRequest切換到dataTaskWithRequest與dataTaskWithRequest
與sendSynchronousRequest我的方法是可以正常使用,但是當我切換到dataTaskWithRequest我得到以下錯誤:
error NSURLError * domain: @"NSURLErrorDomain" - code: 4294966096 0x15ee96c0
和
myError NSError * domain: nil - code: 1684370017 0x26cce125
我不明白爲什麼。
這裏是舊代碼(註釋)和新代碼:
/*-(NSDictionary *)GetProductionScheduleData:(NSString *)areaDescription
{
NSString *areaDescriptionWSpaceCharacters = [areaDescription stringByReplacingOccurrencesOfString:@" " withString:@"%20"];
NSString *requestString = [NSString stringWithFormat:@"%@?areaDescription=%@",kIP,areaDescriptionWSpaceCharacters];
NSURL *JSONURL = [NSURL URLWithString:requestString];
NSURLResponse* response = nil;
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:JSONURL];
NSData* data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:nil];
if(data == nil)
return nil;
NSError *myError;
NSDictionary *productionSchedule = [[NSDictionary alloc]initWithDictionary:[NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&myError]];
return productionSchedule;
}*/
-(void)GetProductionScheduleData:(NSString *)areaDescription Completetion:(void (^) (NSMutableDictionary * result,NSError * error))completion{
NSString *areaDescriptionWSpaceCharacters = [areaDescription stringByReplacingOccurrencesOfString:@" " withString:@"%20"];
NSString *requestString = [NSString stringWithFormat:@"%@?areaDescription=%@",kIP,areaDescriptionWSpaceCharacters];
NSURL *JSONURL = [NSURL URLWithString:requestString];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:JSONURL];
NSURLSession *session = [NSURLSession sharedSession];
NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:request
completionHandler:^(NSData *data, NSURLResponse *response, NSError *error)
{
NSError *myError;
NSMutableDictionary *productionSchedule = [[NSMutableDictionary alloc]initWithDictionary:[NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&myError]];
completion(productionSchedule,myError);
}];
[dataTask resume];
}
請幫助!這與sendSynchronousRequest一起工作我開始不喜歡dataTaskWithRequest。
您使用的是ios9嗎?我得到了同樣的錯誤,當我改變到ios9 swift2,也許ATS是問題 – user2258959