1

私はログイン認証にASIHttprequestを使用していますこれがdemeコードです

私が試しているコードは次のとおりです。

[self setRequest:[ASIHTTPRequest requestWithURL:url1]];
[_request setUseKeychainPersistence:[useKeychain isOn]];
[_request setDelegate:self];
[_request setShouldPresentAuthenticationDialog:[useBuiltInDialog isOn]];
[_request setDidFinishSelector:@selector(topSecretFetchComplete:)];
[_request setDidFailSelector:@selector(topSecretFetchFailed:)];
[_request startAsynchronous];
[request setValidatesSecureCertificate:NO];
[_request setValidatesSecureCertificate:NO];

Request failed および Request complete メソッド

- (IBAction)topSecretFetchFailed:(ASIHTTPRequest *)theRequest{
   [responseField setText:[[request error] localizedDescription]];
   [responseField setFont:[UIFont boldSystemFontOfSize:12]];
}

- (IBAction)topSecretFetchComplete:(ASIHTTPRequest *)theRequest{
   [responseField setText:[request responseString]];
   [responseField setFont:[UIFont boldSystemFontOfSize:12]];
   NSLog(@"Response :%@",[request responseString]);

}

- (void)authenticationNeededForRequest:(ASIHTTPRequest *)theRequest{
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Please Login" message:[request authenticationRealm] delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"OK",nil];

[alertView addTextFieldWithValue:@"" label:@"Username"];
[alertView addTextFieldWithValue:@"" label:@"Password"];
[alertView show];

}

私はiosが初めてなのでお願い
します......このデモでは、クロックを上げたときに1つのアラートがポップアップしますが、Webサービスを使用すると、次のような応答が返されます

応答:ASIHTTPRequest: 0xa846400

ただし、ユーザー名とパスワードを入力するためのアラートビューをポップアップしません

ログインを認証するための他のより良い方法があれば、私に提案してください

ログインをルートビューとして配置したいのですが、ログインに成功した後、アプリに入ることができます

ありがとう...

4

2 に答える 2

1

空白、特殊文字、および改行文字がその応答に含まれている場合があるため、最初にその文字列をトリミングしてから、次のように応答を出力します..

NSString *strResponse = [[theRequest responseString] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
NSLog(@"Response :%@",strResponse);

アップデート:

- (NSString *)flattenHTML:(NSString *)html {

    NSScanner *theScanner;
    NSString *text = nil;
    theScanner = [NSScanner scannerWithString:html];

    while ([theScanner isAtEnd] == NO) {

        [theScanner scanUpToString:@"<" intoString:NULL] ; 

        [theScanner scanUpToString:@">" intoString:&text] ;

        html = [html stringByReplacingOccurrencesOfString:[NSString stringWithFormat:@"%@>", text] withString:@""];
    }
    //
    html = [html stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];

    return html;
}

このメソッドを次のように使用します..

 NSString *strResponse = [[theRequest responseString] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
 NSString *strFinal = [self flattenHTML:strResponse];
 NSLog(@"Response :%@",strFinal);
于 2013-08-01T09:19:50.603 に答える
0

ログイン認証の場合、ASIFormDataRequest を使用します。次の手順に従います。

1)サーバーからのユーザー名とパスワードに一致するWebサービスを1つ作成し、ユーザー名とパスワードが正しい場合は応答を返します応答「成功」 ELSE「失敗」を返します。

例: https://www.yoursite.com/login1.php?username=%@&password=%@

2) このように UsernameTextfield.text と PasswordTextfield.text を使用して 1 つの URL を作成します。

NSURL *url1 = [NSURL URLWithString:[NSString stringWithFormat:@"https://www.yoursite.com/login1.php?username=%@&password=%@",Ustr,Pstr]];

Ustr と Pstr は両方のテキスト フィールドの NSString です。

3) ASIFormDataRequest を作成する

- (IBAction)fetchTopSecretInformation:(id)sender{

    Ustr = User.text;
    Pstr  = Pass.text;

    NSURL *url1 = [NSURL URLWithString:[NSString stringWithFormat:@"https://www.yoursite.com/login1.php?username=%@&password=%@",Ustr,Pstr]];
    ASIFormDataRequest *request1 = [ASIFormDataRequest requestWithURL:url1];
    [request1 setUseKeychainPersistence:YES];
    request1.delegate = self;
    request1.shouldPresentCredentialsBeforeChallenge = YES;

    [request1 setRequestMethod:@"POST"];
    [request1 setPostValue:User.text forKey:@"User Name"];
    [request1 setPostValue:Pass.text forKey:@"Password"];
    [request1 setTimeOutSeconds:30];

    [request1 setDidFailSelector:@selector(topSecretFetchFailed:)];
    [request1 setDidFinishSelector:@selector(topSecretFetchComplete:)];
    [request1 startAsynchronous];

}

このメソッドを xib の Sign In Button に接続します。

- (IBAction)topSecretFetchFailed:(ASIHTTPRequest *)theRequest{
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" message:@"Error sending request to the server" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
    [alert show];
}

- (IBAction)topSecretFetchComplete:(ASIHTTPRequest *)theRequest{
    NSString *strResponse = [[theRequest responseString] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
    strFinal = [theRequest responseString];
    NSLog(@"Sattus Code : %@",[theRequest responseString]);

    NSString *failStr = @"failed";
    NSString *successStr = @"successfully";

    if ([strFinal isEqualToString:successStr]) {
    ViewController *view = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil];
      [self.navigationController pushViewController:view animated:YES];

   }else
        if ([strFinal isEqualToString:failStr]) {
            UIAlertView *AlertFail = [[UIAlertView alloc] initWithTitle:@"LogIn Failed !" message:@"Wrong ID or Password" delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil];
         [AlertFail show];
        }
}

成功した場合は次のページに進み、そうでない場合はアラート警告.....

于 2013-08-07T06:31:01.957 に答える