0

解析しようとしているxmlを含むページにアクセスしようとしています。クレデンシャルを必要としない限り機能する次のコードを使用しています。このコードにクレデンシャルを追加するにはどうすればよいですか?

NSURL *url = [[NSURL alloc] initWithString:URLPath];
NSXMLParser *xmlParser = [[NSXMLParser alloc] initWithContentsOfURL:url];

[xmlParser setDelegate: self];
success = [xmlParser parse];

「URLPath」の上にURL文字列があります。次のURLパス形式を使用してみましたが、機能しません。 http://username:password@mypage.com/path/to/file.aspx?Function=Update

ありがとう!

4

1 に答える 1

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");
    }
}

詳細については、iOSでのNSURLConnectionと基本HTTP認証を参照してください。

于 2012-10-31T16:22:55.373 に答える