我仅在 iOS 6 中面临解析失败的问题。该代码适用于 iOS 6 的早期版本,但不适用于 iOS 6。我正在尝试将登录请求发送到服务器 url。我收到来自服务器的 xml 格式的响应。我正在使用 NSXMLParser 来解析响应。我能够在 iOS 3.x、iOS 4.x 和 iOS 5.x 中解析 xml,但在 iOS 6 中解析失败。
我正在使用这些行
NSXMLParser *m_parser = [[NSXMLParser alloc] initWithData:urlData];
[m_parser setDelegate:self];
BOOL parseFlag = [m_parser parse];//parsing failed in iOS 6
在 iOS 6 中只- (void)parser:(NSXMLParser *)parser parseErrorOccurred:(NSError *)parseError
被调用。
我得到的错误是 NSXMLParserErrorDomain 错误 68。
详细的源代码如下。
-(IBAction)loginButtonPressed:(id)sender
{
NSMutableString *urlString = [[@"someURL" mutableCopy] autorelease];
[urlString appendString:@"loginAuthenticate"];
[urlString appendFormat:@"?username=%@",m_username.text];
[urlString appendFormat:@"&password=%@",pwd];
NSURL *url = [NSURL URLWithString:[urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
NSString *postLength = [NSString stringWithFormat:@"%d", [urlString length]];
NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
[request setURL:url];
[request setHTTPMethod:@"POST"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"text/xml; charset=utf-16" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:[urlString dataUsingEncoding:NSUTF16StringEncoding]];
NSError *error;
NSURLResponse *response;
NSData *urlData;
urlData=[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
NSString *responseXml = [[NSString alloc] initWithData:urlData encoding:NSUTF16StringEncoding];
if (responseXml &&[responseXml length]>0)
{
NSXMLParser *m_parser = [[NSXMLParser alloc] initWithData:urlData];
[m_parser setDelegate:self];
NSLog(@"urlData:%@",m_parser);
BOOL parseFlag = [m_parser parse];//parsing failed in iOS 6
if (parseFlag == 1)
NSLog(@"parseFlag = YES");//called if iOS version is less than iOS 6
else
{
NSLog(@"parseFlag = NO");//called if iOS version is iOS 6
}
.
.
}
// This delegate method is called in iOS 6 only
- (void)parser:(NSXMLParser *)parser parseErrorOccurred:(NSError *)parseError {
NSLog(@"%@",@"parseErrorOccured");
NSLog(@"parseError:%@",parseError);
//error is NSXMLParserErrorDomain error 68
}