[英]iOS: How can I add credentials to my code for my NSURL?
I'm trying to access a page that has xml I'm trying to parse. 我正在尝试访问一个包含要解析的xml的页面。 I'm using the following code which works as long as I do not need credentials.
我正在使用以下代码,只要我不需要凭据即可。 How can I add credentials to this code?
如何向此代码添加凭据?
NSURL *url = [[NSURL alloc] initWithString:URLPath];
NSXMLParser *xmlParser = [[NSXMLParser alloc] initWithContentsOfURL:url];
[xmlParser setDelegate: self];
success = [xmlParser parse];
Above my "URLPath" is my url string. 我的“ URLPath”上方是我的网址字符串。 I've tried using the following url path format but it doesn't work for me.
我尝试使用以下网址路径格式,但对我而言不起作用。
http://username:password@mypage.com/path/to/file.aspx?Function=Update
Thanks! 谢谢!
Use the NSURLConnectionDelegates
使用
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");
}
}
See NSURLConnection and Basic HTTP Authentication in iOS for more information 有关更多信息,请参见iOS中的NSURLConnection和基本HTTP身份验证。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.