gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 00:41:10 42 4
gpt4 key购买 nike

我是 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);
}

我的问题是 NSURLSessionDataDelegate 中的 didFinishLoading 委托(delegate)使用什么。

当我在下面使用这个委托(delegate)时:

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

它显示错误。我需要按照 Apple 的建议将 NSURLConnectionDataDelegate 更改为 NSURLSessionDataDelegate

最佳答案

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

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

关于ios - 如何在 Objective C 中将 NSURLConnection 更改为 NSURLSession?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39486378/

42 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com