我有这个代码消费它在iOS8和iOS10中工作但在iOS9中不起作用
BOOL blnAnswer = NO;
NSString *strAnswer = @"";
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://10.16.2.42/api/IniciarSesionMobile/GetConnectTest"]
cachePolicy:NSURLRequestReturnCacheDataElseLoad
timeoutInterval:35];
// Fetch the JSON response
NSData *urlData;
NSURLResponse *response;
NSError *error;
// Make synchronous request
urlData = [NSURLConnection sendSynchronousRequest:urlRequest
returningResponse:&response
error:&error];
// Construct a String around the Data from the response
strAnswer = [[NSString alloc] initWithData:urlData encoding:NSUTF8StringEncoding];
答案 0 :(得分:1)
如果要从服务器获取数据,请按照以下步骤进行操作
NSMutableURLRequest *urlRequest = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:@"http://10.16.2.42/api/IniciarSesionMobile/GetConnectTest"]];
//If you have parameters,pass it to URL
//create the Method "GET"
[urlRequest setHTTPMethod:@"GET"];
NSURLSession *session = [NSURLSession sharedSession];
NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:urlRequest completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
NSString *strResponse = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding];
NSLog(@"response is: %@", strResponse );
}];
[dataTask resume];