ios - 发送同步请求 :returningResponse deprecated

标签 ios objective-c

我有一个 Xcode 应用程序,我正在更新到最新的 iOS。我现在注意到在构建时出现以下错误/警告:

/ConfViewController.m:198:46: 'sendSynchronousRequest:returningResponse:error:' 已弃用:首先在 iOS 9.0 中弃用 - 使用 [NSURLSession dataTaskWithRequest:completionHandler:](参见 NSURLSession.h

根据我所阅读的内容,我应该开始使用“NSURLSession”,但我如何在我的代码中使用“NSURLSession”,或者我是否看错了?

我的代码:

NSString *deviceName = [[UIDevice currentDevice]name];
        NSString *post =[[NSString alloc] initWithFormat:@"devicename=%@",deviceName];
        NSLog(@"PostData: %@",post);

        NSURL *url=[NSURL URLWithString:@"http://www.mydomain/sysscripts/conf/devicelookup17.php"];


        NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];

        NSString *postLength = [NSString stringWithFormat:@"%lu", (unsigned long)[postData length]];

        NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
        [request setURL:url];
        [request setHTTPMethod:@"POST"];
        [request setValue:postLength forHTTPHeaderField:@"Content-Length"];
        [request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
        [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
        [request setHTTPBody:postData];

        NSError *error = [[NSError alloc] init];
        NSHTTPURLResponse *response = nil;

//The ERROR point
NSData *urlData=[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];

        //NSLog(@"Response code: %ld", (long)[response statusCode]);
        if ([response statusCode] >=200 && [response statusCode] <300)
        {
            NSString *responseData = [[NSString alloc]initWithData:urlData encoding:NSUTF8StringEncoding];
            NSLog(@"Response ==> %@", responseData);

            SBJsonParser *jsonParser = [SBJsonParser new];
            NSDictionary *jsonData = (NSDictionary *) [jsonParser objectWithString:responseData error:nil];
           // NSLog(@"%@",jsonData);

           // NSInteger success = [(NSNumber *) [jsonData objectForKey:@"success"] integerValue];
            NSInteger roomid = [(NSNumber *) [jsonData objectForKey:@"roomid"] integerValue];
           // NSLog(@"%ld",(long)success);
            //NSLog(@"%ld",(long)roomid);
            NSString *RoomID =  [NSString  stringWithFormat:@"%ld",(long)roomid];
           // NSLog(@"%@", RoomID);

            NSString *firstString = @"http://www.mydomain/apps/conf/lon/dt/devices/ /template17.php";
           // NSLog(@"%@", firstString);

            NSString *roomID = RoomID;
           // NSLog(@"%@", roomID);

            NSString *newString = [firstString stringByReplacingOccurrencesOfString:@" " withString:roomID];
          //  NSLog(@"%@", newString);

            NSURL *url2 = [NSURL URLWithString: newString];
            NSLog(@"%@", url2);

            NSURLRequest *request2 = [NSURLRequest requestWithURL:url2];


           // ConfViewController *navex =[[ConfViewController alloc] initWithNibName:nil bundle:nil];
            //[self presentViewController:navex animated:YES completion:NULL];
            [webView loadRequest:request2];



        }

非常感谢您的宝贵时间。

最佳答案

  1. 替换

    NSData *urlData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
    

    [[[NSURLSession sharedSession] dataTaskWithRequest:request completionHandler:^(NSData * data, NSURLResponse * response, NSError * error) {
    
    
    }] resume];
    
  2. 将整个代码放在完成 block 中的 sendSynchronousRequest 行之后(大括号之间)。

  3. 替换

    if ([response statusCode] >=200 && [response statusCode] <300)
    

    NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response;
    NSInteger statusCode = httpResponse.statusCode;
    if (statusCode >= 200 && statusCode < 300)
    
  4. urlData替换为data

  5. 删除

    NSError *error = [[NSError alloc] init];
    NSHTTPURLResponse *response = nil;
    

关于ios - 发送同步请求 :returningResponse deprecated,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43654098/

相关文章:

ios - iOS 中的无线配件配置 (WAC) 集成以及如何使用

ios - 为什么我不能在 tableview、iOS、objective c 中的 didSelectRowAtIndexPath 中隐藏/显示标签

ios - Objective C 从 URL 获取 html,编码错误

ios - 如何使用UIDocumentPickerViewController获得原始文件名?

objective-c - 将数据从一个 View Controller 传递到另一个 View Controller

ios - 自定义 UItableViewCell 未加载图像? swift 4

ios - 通过倾斜设备移动节点

ios - AIR 2.7 for iOS 在后台播放音频?

iphone - 如何重置或清除 OpenGLES(iPhone) 中的 glVertexAttribPointer 值?

ios - 检查特定应用程序的代码是否已安装