2

私はiOSアプリケーションの開発の初心者です。リモートXMLファイルからロードされた別のURLに到達する必要があります。これらのURLの一部にはhttp基本認証が必要です。

NSURLCredentialによる認証、正しく機能します。ここで、ユーザーに資格情報の入力を要求したいと思います。接続が失敗した場合、setAlertViewStyle:UIAlertViewStyleLoginAndPasswordInputを使用してインターフェイスログインUIAlertViewを視覚化します。

この時点で、textFieldAtIndex:0および1を使用してユーザーが入力した通信ユーザー名とパスワードを確認しますが、NSURLConnectionを使用した新しい接続試行に使用できます。

誰かが私を助けることができますか?

これは私のコードです:

- (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge {
    if ([challenge previousFailureCount] == 0)
    {
        NSLog(@"received authentication challenge");

        NSURLCredential *newCredential;
        newCredential=[NSURLCredential credentialWithUser:@""
                                                 password:@""
                                              persistence:NSURLCredentialPersistenceForSession];


        NSLog(@"credential created");

        [[challenge sender] useCredential:newCredential forAuthenticationChallenge:challenge];

        NSLog(@"responded to authentication challenge");

    }
    else
    {
        NSLog(@"previous authentication failure");

        UIAlertView *message = [[UIAlertView alloc] initWithTitle:@"Login"
                                                          message:@"Enter your username & password"
                                                         delegate:self
                                                cancelButtonTitle:@"Cancel"
                                                otherButtonTitles:@"Login", nil];

        [message setAlertViewStyle:UIAlertViewStyleLoginAndPasswordInput];

        [message show];
    }
}

- (BOOL)alertViewShouldEnableFirstOtherButton:(UIAlertView *)alertView
{
    NSString *inputText = [[alertView textFieldAtIndex:0] text];
    if( [inputText length] >= 1 )
    {
        return YES;
    }
    else
    {
        return NO;
    }
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    NSString *title = [alertView buttonTitleAtIndex:buttonIndex];
    if([title isEqualToString:@"Login"])
    {
        UITextField *username = [alertView textFieldAtIndex:0];
        UITextField *password = [alertView textFieldAtIndex:1];
        NSLog(@"Username: %@\npassword: %@", username.text, password.text);
        if ([username.text isEqualToString:@"myname"] && [password.text isEqualToString:@"1234"])
        NSLog(@"Success");
    }
    [xmlDataBuffer setLength:0];
}
4

0 に答える 0