正在检查从Google获取当前时间和日期。第一个选择虽然不是最好的方法,但它使用了贬值的方法,并等待所有的事情都用同步方法完成,这不是一个好的用户体验。
-(NSDate*)timeAndDateFromWeb{
NSMutableURLRequest *request = [[NSMutableURLRequest alloc]
initWithURL:[NSURL URLWithString:@"https://google.co.uk"]];
[request setHTTPMethod:@"GET"];
NSHTTPURLResponse *httpResponse = nil;
[NSURLConnection sendSynchronousRequest:request returningResponse:&httpResponse error:nil];
NSString *dateString = [[httpResponse allHeaderFields] objectForKey:@"Date"];
DebugLog(@" *** GOOGLE DATE: %@ ****",dateString);
if (httpResponse){
hasDataConnection = YES;
}
else{
hasDataConnection = NO;
}
// Convert string to date object
NSDateFormatter *dateformatted = [NSDateFormatter new];
[dateformatted setDateFormat:@"E, d MMM yyyy HH:mm:ss zzz"];
NSLocale *locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en"];
[dateformatted setLocale:locale];
return [dateformatted dateFromString:dateString];
}
试图适应它几乎在那里,虽然我返回nil为我的日期字符串:[dateformatted dateFromString:dateString];
NSURL *url = [NSURL URLWithString:@"https://google.co.uk"];
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url];
NSOperationQueue *queue = [[NSOperationQueue alloc] init];
NSHTTPURLResponse *httpResponse = nil;
NSString *dateString = [[httpResponse allHeaderFields] objectForKey:@"Date"];
[NSURLConnection sendAsynchronousRequest:urlRequest queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
if (error) {
hasDataConnection = NO;
//NSLog(@"nn ----> Not connected Error,%@", [error localizedDescription]);
}
else {
//NSLog(@"nn -----> connected: %@", [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding]);
hasDataConnection = YES;
}
}];
// Convert string to date object
NSDateFormatter *dateformatted = [NSDateFormatter new];
[dateformatted setDateFormat:@"E, d MMM yyyy HH:mm:ss zzz"];
NSLocale *locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en"];
[dateformatted setLocale:locale];
DebugLog(@" *** GOOGLE DATE: %@ ****",[dateformatted dateFromString:dateString]);
return [dateformatted dateFromString:dateString];
当你从同步切换到异步时,不能像以前那样返回值
当你调用sendAsynchronousRequest
方法时,它启动一个后台任务,但是你的线程立即继续工作。这就是为什么httpResponse
和dateString
都是null
。
因此,您应该将返回类型更改为void
,因为您不能立即返回结果,并添加回调,该回调将在作业完成时运行。并在完成任务时处理您的格式:
- (void)timeAndDateFromWeb:(void (^)(NSDate *))completion {
NSURL *url = [NSURL URLWithString:@"https://google.co.uk"];
NSURLSessionTask *task = [NSURLSession.sharedSession
dataTaskWithURL:url
completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
if (error) {
// hasDataConnection = NO;
//NSLog(@"nn ----> Not connected Error,%@", [error localizedDescription]);
}
else if ([response isKindOfClass:NSHTTPURLResponse.class] ) {
NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response;
NSString *dateString = [[httpResponse allHeaderFields] objectForKey:@"Date"];
// Convert string to date object
NSDateFormatter *dateformatted = [NSDateFormatter new];
[dateformatted setDateFormat:@"E, d MMM yyyy HH:mm:ss zzz"];
NSLocale *locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en"];
[dateformatted setLocale:locale];
// DebugLog(@" *** GOOGLE DATE: %@ ****",[dateformatted dateFromString:dateString]);
completion([dateformatted dateFromString:dateString]);
//NSLog(@"nn -----> connected: %@", [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding]);
// hasDataConnection = YES;
}
}];
[task resume];
}
因此,这个方法的结果将返回到块中,与后台任务的结果相同。
不要忘记,它将在后台线程中被调用,就像下载任务完成块一样。所以如果你想改变一些UI你需要移回主线程:
[self timeAndDateFromWeb:^(NSDate *date) {
dispatch_async(dispatch_get_main_queue(), ^{
NSLog(@"%@", date);
// UI changes
});
}];
或者你可以在返回结果之前将它移到函数的主线程中:
NSDate *date = [dateformatted dateFromString:dateString];
dispatch_async(dispatch_get_main_queue(), ^{
completion(date);
});