2015-10-05 51 views
0

我正在从NSURLConnection移动到NSURLSession,并且在保存URL请求值的NSMutableURLRequest时遇到问题。从NSURLConnection不兼容的指针类型迁移到NSURLSession

如何保留在NSURLSession这个信息没有收到此错误的任何想法:

incompatible pointer types sending nsmutableurlrequest to parameter of type nesting * _nonull nsurl urlwithstring

我看到这条线[NSURL URLWithString:myRequest]想要一个的NSString,但如何我仍然可以通过其他信息是我的NSMutableURLRequest

NSMutableURLRequest *myRequest = [NSMutableURLRequest requestWithURL: myURL]; 
[acquisitionRequest setValue:userAgent forHTTPHeaderField:@"User-Agent"]; 

NSURLSession *session = [NSURLSession sharedSession]; 
[[session dataTaskWithURL:[NSURL URLWithString:myRequest] 
      completionHandler:^(NSData *data, 
           NSURLResponse *response, 
           NSError *error) {     
      }] resume]; 

回答

0

您应该使用dataTaskWithRequest:completionHandler:代替dataTaskWithURL:completionHandler:。这样你就可以传递整个请求。如果它抱怨可变,你可以复制它以摆脱可变状态。

NSMutableURLRequest *myRequest = [NSMutableURLRequest requestWithURL: myURL]; 
[acquisitionRequest setValue:userAgent forHTTPHeaderField:@"User-Agent"]; 

NSURLSession *session = [NSURLSession sharedSession]; 

[[session dataTaskWithRequest:[myRequest copy] 
      completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) { 
       // completion stuff 
}] resume]; 
+0

谢谢BIG TIME Keith。这看起来像一个快乐的露营者没有副本,但我会测试并在测试后批准它。再次感谢。 – Jazzmine

0

你的问题是要传递NSMutableURLRequest对象在使用URLWithString:其期待您通过NSString创建URL。通过myURL而不是[NSURL URLWithString:myRequest],你应该很好。

但是,我也建议使用dataTaskWithRequest:completionHandler:来代替。

0

我收到类似的问题。我无法在iOS 9上登录,因为NSData已被弃用。我需要更新到NSURLSession。我试图用下面的代码,但是 - 看起来有两行仍然从NSData中绘制,其中我的指针是一个NSURLSession。

它加剧了系统。我如何设置接收线路与NSURLSession连接?

NSURLSession *session=[NSURLSession sharedSession]; 
    NSLog(@"response%@",response); 
    NSLog(@"Response code: %ld", (long)[response statusCode]); 

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

     NSError *error = nil; 
     /* NSDictionary *jsonData = [NSJSONSerialization 
            JSONObjectWithData:urlData 
            options:NSJSONReadingMutableContainers 
            error:&error];*/ 
     // NSLog(@"dict%@",jsonData); 
     self.topLevelArray3 = [NSJSONSerialization JSONObjectWithData:session options:NSJSONReadingMutableContainers error:&error]; 
     for (int i=0; i<[self.topLevelArray3 count]; i++) {