如何更改 sendSynchronousRequest:urlRequest 因为已弃用
How to change sendSynchronousRequest:urlRequest because is Deprecated
我是 Objective - C 的初学者,我有一个方法
- (void)getAltitudeFromElevationFromAlt:(float)latitude Long:(float)longitude{
dispatch_async( dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSString *apiKey = @"IzaSyA5CDPUYC7GY5PzJdu_K4ouRy55gm3R5BO4";
NSString *address = [NSString stringWithFormat:@"https://maps.googleapis.com/maps/api/elevation/json?locations=%f,%f&key=%@", latitude, longitude, apiKey];
// Send a synchronous request
NSURLRequest * urlRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:address]];
NSURLResponse * response = nil;
NSError * error = nil;
NSData * data = [NSURLConnection sendSynchronousRequest:urlRequest
returningResponse:&response
error:&error];
NSString *str = @"No Data";
if (error == nil)
{
NSDictionary *dictionary = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
str = [NSString stringWithFormat:@"%@", dictionary[@"results"][0][@"elevation"]];
// NSLog(@"text = %@", dictionary[@"results"][0][@"elevation"]);
NSLog(@"str = %@", str);
dispatch_async( dispatch_get_main_queue(), ^{
_altitudeMeterLabel.text = str;
});
}
});
}
请帮助更改此顺序
NSData * data = [NSURLConnection sendSynchronousRequest:urlRequest
returningResponse:&response
error:&error];
使用NSURLSession
和异步请求:
- (void)getAltitudeFromElevationFromAlt:(float)latitude Long:(float)longitude {
NSString *apiKey = @"IzaSyA5CDPUYC7GY5PzJdu_K4ouRy55gm3R5BO4";
NSString *address = [NSString stringWithFormat:@"https://maps.googleapis.com/maps/api/elevation/json?locations=%f,%f&key=%@", latitude, longitude, apiKey];
// Send an ASYNCHRONOUS request
NSURLRequest * urlRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:address]];
[[[NSURLSession sharedSession] dataTaskWithRequest:urlRequest completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
NSString *str = @"No Data";
if (error) {
NSLog(@"%@", error);
} else {
NSError * jsonError = nil;
NSDictionary *dictionary = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&jsonError];
if (jsonError) {
NSLog(@"%@", jsonError);
} else {
str = [NSString stringWithFormat:@"%@", dictionary[@"results"][0][@"elevation"]];
// NSLog(@"text = %@", dictionary[@"results"][0][@"elevation"]);
NSLog(@"str = %@", str);
}
dispatch_async( dispatch_get_main_queue(), ^{
_altitudeMeterLabel.text = str;
});
}
}] resume];
}
注:
不需要全局 dispatch_async
块,因为数据任务无论如何都会分派到后台线程。
将 NSURLSession 与 dataTaskWithRequest
结合使用
- (void)getAltitudeFromElevationFromAlt:(float)latitude Long:(float)longitude
{
NSString *apiKey = @"IzaSyA5CDPUYC7GY5PzJdu_K4ouRy55gm3R5BO4";
NSString *address = [NSString stringWithFormat:@"https://maps.googleapis.com/maps/api/elevation/json?locations=%f,%f&key=%@", latitude, longitude, apiKey];
NSURLRequest * request = [NSURLRequest requestWithURL:[NSURL URLWithString:address]];
NSURLResponse * response = nil;
NSString *str = @"No Data";
NSURLSession *session = [NSURLSession sharedSession];
NSURLSessionDataTask *task = [session dataTaskWithRequest:request
completionHandler:
^(NSData *data, NSURLResponse *response, NSError *error) {
if (error == nil)
{
NSDictionary *dictionary = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
str = [NSString stringWithFormat:@"%@", dictionary[@"results"][0][@"elevation"]];
// NSLog(@"text = %@", dictionary[@"results"][0][@"elevation"]);
NSLog(@"str = %@", str);
dispatch_async( dispatch_get_main_queue(), ^{
_altitudeMeterLabel.text = str;
});
}
}];
[task resume];
}
请尝试以下代码
- (void)getAltitudeFromElevationFromAlt:(float)latitude Long:(float)longitude{
dispatch_async( dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSString *apiKey = @"IzaSyA5CDPUYC7GY5PzJdu_K4ouRy55gm3R5BO4";
NSString *address = [NSString stringWithFormat:@"https://maps.googleapis.com/maps/api/elevation/json?locations=%f,%f&key=%@", latitude, longitude, apiKey];
// Send a synchronous request
NSURLSession *session = [NSURLSession sharedSession];
[[session dataTaskWithURL:[NSURL URLWithString:address]
completionHandler:^(NSData *data,
NSURLResponse *response,
NSError *error) {
NSString *str = @"No Data";
if (error == nil)
{
NSDictionary *dictionary = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
str = [NSString stringWithFormat:@"%@", dictionary[@"results"][0][@"elevation"]];
// NSLog(@"text = %@", dictionary[@"results"][0][@"elevation"]);
NSLog(@"str = %@", str);
dispatch_async( dispatch_get_main_queue(), ^{
_altitudeMeterLabel.text = str;
});
}
}] resume];
});
}
从 iOS9 开始不推荐使用同步方法,因为它会阻塞主线程。所以不建议同步执行这个操作。
如果确实需要,您可以创建 NSUrlSession 类别来同步执行您的请求。
#import "NSURLSession+Sync.h"
@implementation NSURLSession (Sync)
+ (NSData *)sendSynchronousRequest:(NSURLRequest *)request
returningResponse:(__autoreleasing NSURLResponse **)responsePtr
error:(__autoreleasing NSError **)errorPtr {
dispatch_semaphore_t sem;
__block NSData * result;
result = nil;
sem = dispatch_semaphore_create(0);
[[[NSURLSession sharedSession] dataTaskWithRequest:request
completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
if (errorPtr != NULL) {
*errorPtr = error;
}
if (responsePtr != NULL) {
*responsePtr = response;
}
if (error == nil) {
result = data;
}
dispatch_semaphore_signal(sem);
}] resume];
dispatch_semaphore_wait(sem, DISPATCH_TIME_FOREVER);
return result;
}
@end
并在如下代码中使用:
NSData * data = [NSURLSession sendSynchronousRequest:urlRequest returningResponse:&response error:&error];
我是 Objective - C 的初学者,我有一个方法
- (void)getAltitudeFromElevationFromAlt:(float)latitude Long:(float)longitude{
dispatch_async( dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSString *apiKey = @"IzaSyA5CDPUYC7GY5PzJdu_K4ouRy55gm3R5BO4";
NSString *address = [NSString stringWithFormat:@"https://maps.googleapis.com/maps/api/elevation/json?locations=%f,%f&key=%@", latitude, longitude, apiKey];
// Send a synchronous request
NSURLRequest * urlRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:address]];
NSURLResponse * response = nil;
NSError * error = nil;
NSData * data = [NSURLConnection sendSynchronousRequest:urlRequest
returningResponse:&response
error:&error];
NSString *str = @"No Data";
if (error == nil)
{
NSDictionary *dictionary = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
str = [NSString stringWithFormat:@"%@", dictionary[@"results"][0][@"elevation"]];
// NSLog(@"text = %@", dictionary[@"results"][0][@"elevation"]);
NSLog(@"str = %@", str);
dispatch_async( dispatch_get_main_queue(), ^{
_altitudeMeterLabel.text = str;
});
}
});
}
请帮助更改此顺序
NSData * data = [NSURLConnection sendSynchronousRequest:urlRequest
returningResponse:&response
error:&error];
使用NSURLSession
和异步请求:
- (void)getAltitudeFromElevationFromAlt:(float)latitude Long:(float)longitude {
NSString *apiKey = @"IzaSyA5CDPUYC7GY5PzJdu_K4ouRy55gm3R5BO4";
NSString *address = [NSString stringWithFormat:@"https://maps.googleapis.com/maps/api/elevation/json?locations=%f,%f&key=%@", latitude, longitude, apiKey];
// Send an ASYNCHRONOUS request
NSURLRequest * urlRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:address]];
[[[NSURLSession sharedSession] dataTaskWithRequest:urlRequest completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
NSString *str = @"No Data";
if (error) {
NSLog(@"%@", error);
} else {
NSError * jsonError = nil;
NSDictionary *dictionary = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&jsonError];
if (jsonError) {
NSLog(@"%@", jsonError);
} else {
str = [NSString stringWithFormat:@"%@", dictionary[@"results"][0][@"elevation"]];
// NSLog(@"text = %@", dictionary[@"results"][0][@"elevation"]);
NSLog(@"str = %@", str);
}
dispatch_async( dispatch_get_main_queue(), ^{
_altitudeMeterLabel.text = str;
});
}
}] resume];
}
注:
不需要全局 dispatch_async
块,因为数据任务无论如何都会分派到后台线程。
将 NSURLSession 与 dataTaskWithRequest
结合使用- (void)getAltitudeFromElevationFromAlt:(float)latitude Long:(float)longitude
{
NSString *apiKey = @"IzaSyA5CDPUYC7GY5PzJdu_K4ouRy55gm3R5BO4";
NSString *address = [NSString stringWithFormat:@"https://maps.googleapis.com/maps/api/elevation/json?locations=%f,%f&key=%@", latitude, longitude, apiKey];
NSURLRequest * request = [NSURLRequest requestWithURL:[NSURL URLWithString:address]];
NSURLResponse * response = nil;
NSString *str = @"No Data";
NSURLSession *session = [NSURLSession sharedSession];
NSURLSessionDataTask *task = [session dataTaskWithRequest:request
completionHandler:
^(NSData *data, NSURLResponse *response, NSError *error) {
if (error == nil)
{
NSDictionary *dictionary = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
str = [NSString stringWithFormat:@"%@", dictionary[@"results"][0][@"elevation"]];
// NSLog(@"text = %@", dictionary[@"results"][0][@"elevation"]);
NSLog(@"str = %@", str);
dispatch_async( dispatch_get_main_queue(), ^{
_altitudeMeterLabel.text = str;
});
}
}];
[task resume];
}
请尝试以下代码
- (void)getAltitudeFromElevationFromAlt:(float)latitude Long:(float)longitude{
dispatch_async( dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSString *apiKey = @"IzaSyA5CDPUYC7GY5PzJdu_K4ouRy55gm3R5BO4";
NSString *address = [NSString stringWithFormat:@"https://maps.googleapis.com/maps/api/elevation/json?locations=%f,%f&key=%@", latitude, longitude, apiKey];
// Send a synchronous request
NSURLSession *session = [NSURLSession sharedSession];
[[session dataTaskWithURL:[NSURL URLWithString:address]
completionHandler:^(NSData *data,
NSURLResponse *response,
NSError *error) {
NSString *str = @"No Data";
if (error == nil)
{
NSDictionary *dictionary = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
str = [NSString stringWithFormat:@"%@", dictionary[@"results"][0][@"elevation"]];
// NSLog(@"text = %@", dictionary[@"results"][0][@"elevation"]);
NSLog(@"str = %@", str);
dispatch_async( dispatch_get_main_queue(), ^{
_altitudeMeterLabel.text = str;
});
}
}] resume];
});
}
从 iOS9 开始不推荐使用同步方法,因为它会阻塞主线程。所以不建议同步执行这个操作。
如果确实需要,您可以创建 NSUrlSession 类别来同步执行您的请求。
#import "NSURLSession+Sync.h"
@implementation NSURLSession (Sync)
+ (NSData *)sendSynchronousRequest:(NSURLRequest *)request
returningResponse:(__autoreleasing NSURLResponse **)responsePtr
error:(__autoreleasing NSError **)errorPtr {
dispatch_semaphore_t sem;
__block NSData * result;
result = nil;
sem = dispatch_semaphore_create(0);
[[[NSURLSession sharedSession] dataTaskWithRequest:request
completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
if (errorPtr != NULL) {
*errorPtr = error;
}
if (responsePtr != NULL) {
*responsePtr = response;
}
if (error == nil) {
result = data;
}
dispatch_semaphore_signal(sem);
}] resume];
dispatch_semaphore_wait(sem, DISPATCH_TIME_FOREVER);
return result;
}
@end
并在如下代码中使用:
NSData * data = [NSURLSession sendSynchronousRequest:urlRequest returningResponse:&response error:&error];