我正在尝试访问包含xml的页面,我正在尝试解析。我正在使用以下代码,只要我不需要凭据就行。如何向此代码添加凭据?
NSURL *url = [[NSURL alloc] initWithString:URLPath];
NSXMLParser *xmlParser = [[NSXMLParser alloc] initWithContentsOfURL:url];
[xmlParser setDelegate: self];
success = [xmlParser parse];
在我的“URLPath”之上是我的网址字符串。我尝试使用以下url路径格式,但它对我不起作用。 http://username:password@mypage.com/path/to/file.aspx?Function=Update
谢谢!
答案 0 :(得分:1)
使用NSURLConnectionDelegates
- (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge {
if ([challenge previousFailureCount] == 0) {
NSLog(@"received authentication challenge");
NSURLCredential *newCredential = [NSURLCredential credentialWithUser:@"USER"
password:@"PASSWORD"
persistence:NSURLCredentialPersistenceForSession];
NSLog(@"credential created");
[[challenge sender] useCredential:newCredential forAuthenticationChallenge:challenge];
NSLog(@"responded to authentication challenge");
}
else {
NSLog(@"previous authentication failure");
}
}
有关详细信息,请参阅NSURLConnection and Basic HTTP Authentication in iOS