如何在Objective C中将NSURLConnection更改为NSURLSession?

问题描述 投票:0回答:2

我是iOS的新手。我在向Apple Store提交应用程序时遇到了IPv6的错误。我的代码是这样的

-(void)serverconnection{


    NSString *soapMessage = [NSString stringWithFormat:@"<?xml version=\"1.0\" encoding=\"utf-8\"?>"
                             "<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">"
                             "<soap:Body>"
                             "<Method Name xmlns=\"http://tempuri.org/\">"
                             "<UserId>%@</UserId>"
                             "</Method Name>"
                             "</soap:Body>"
                             "</soap:Envelope>",userid];




    NSURL *myNSUObj=[NSURL URLWithString:@"URL/webservice.asmx?op=MethodName"];
    // NSURLRequest *myNSURequestObj=[NSURLRequest requestWithURL:myNSUObj];

    NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:myNSUObj];
    NSString *msgLength = [NSString stringWithFormat:@"%lu", (unsigned long)[soapMessage length]];

    [theRequest addValue: @"text/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
    [theRequest addValue: @"http://tempuri.org/MethodName" forHTTPHeaderField:@"SOAPAction"];
    [theRequest addValue: msgLength forHTTPHeaderField:@"Content-Length"];
    [theRequest setHTTPMethod:@"POST"];
    [theRequest setHTTPBody: [soapMessage dataUsingEncoding:NSUTF8StringEncoding]];

    myNSUConnectionObj=[[NSURLConnection alloc]initWithRequest:theRequest delegate:self];
    NSLog(@"Data =%@",myNSUConnectionObj);
    if(myNSUConnectionObj)
    {

        NSLog(@"successful connection");
        myNSMDataFromServer=[[NSMutableData alloc]init];
    }
}

#pragma mark - NSURLConnection Delegate

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{

    [myNSMDataFromServer setLength:0];
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{

    [myNSMDataFromServer appendData:data];
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection{

    NSXMLParser *myNSXMLParserObj=[[NSXMLParser alloc]initWithData:myNSMDataFromServer];
    myNSXMLParserObj.delegate=self;
    [myNSXMLParserObj parse];
    NSLog(@"%@",myNSXMLParserObj.parserError);
    NSLog(@"DataArray: %@",myDataNSMArray);

    Keyarry=[responsedict valueForKey:@"Key"];
    NSLog(@"Assign Count =%@",Keyarry);
    NSString *string = [NSString stringWithFormat:@"%@", [Keyarry objectAtIndex:0]];
    NSLog(@"String value =%@",string);
    ibtassigncountlbl.text =string;

  }

#pragma mark - NSXMLParsing Delegate

-(void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict
{
    if([elementName isEqualToString:@"FillBlocksNew"])
    {
        myDataClassObj=[[mydata alloc]init];
    }
}

-(void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{

    myMutableStringObj=[[NSMutableString alloc]initWithString:string];
    NSLog(@"Array String: %@",myMutableStringObj);
    NSData *data = [myMutableStringObj dataUsingEncoding:NSUTF8StringEncoding];
    responsedict = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
    NSLog(@"JSON DATA = %@",responsedict);
}

-(void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{
    NSLog(@"DataArray: %@",myDataNSMArray);
}

我的问题是代表在didFinishLoading中使用NSURLSessionDataDelegate的内容。

当我在下面使用这个代表时:

- (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task connectionDidFinishLoading:(NSURLConnection *)conn { }

它告诉我错误。我需要按照Apple的建议从NSURLConnectionDataDelegate改为NSURLSessionDataDelegate

ios objective-c xcode nsxmlparser
2个回答
1
投票

NSURLConnection方法与IPv6兼容。无需更改它以支持IPv6。我认为问题出在你的网址上。在应用程序的某处,您可能使用过硬编码的IPv4地址,如(w.x.y.z)。删除具有适当域名的地址,申请将获得通过。

您还应该使用最新的Rachability类。


0
投票

基本上delegate方法用于创建两个类之间的连接,所以当你请求url使用NSURLRequest时它会连接到delegate类的NSURLConnection方法,它一次处理特定的请求url并明智地做出响应。

NSURLConnection有两种方法表示请求已完成(NSURLConnectionDataDelegate -connectionDidFinishLoading:和NSURLConnectionDelegate -connection:didFailWithError :)。两种方法都表示特定请求的完成。

现在使用NSURLConnection没有什么不对,你需要确保你的URL并以正确的方式使用delegate方法,见下文。

#pragma mark -
#pragma mark - NSURLConnection Delegate Methods
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
    [webData setLength:0];
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{
    if(!webData){
        webData = [[NSMutableData alloc] init];
    }
    [webData appendData:data];
}

-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    NSString *returnString = [[NSString alloc] initWithData:webData encoding:NSUTF8StringEncoding];
    NSLog(@"response = %@",returnString);
}
© www.soinside.com 2019 - 2024. All rights reserved.