문제

i am using ASIHttprequest for login authentication Here is the deme code

The code i am trying is :

[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 and Request complete methods

- (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];

}

i am new to ios so please......
in this demo when i clock go one alert is popup but when i use my webservice its give the response like this

Response :ASIHTTPRequest: 0xa846400

but do not popup the alertview for enter username and password

If any other Better Way to authenticate login please suggest me

i want to place login as rootview and after successfully login can enter in to app

thanks...

도움이 되었습니까?

해결책 2

For Login Authentication ,using ASIFormDataRequest Follow the following STEPS

1) Create one webservice which can match User Name and Password from server and give the response if Username and Password is correct Give response "Success" ELSE "Fail".

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

2) Create one url using UsernameTextfield.text and PasswordTextfield.text like this

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

Where Ustr and Pstr are NSString from Both text fields.

3) Create 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];

}

Connect this method with Sign In Button in xib.

- (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];
        }
}

So if success go to next page otherwise Alert warning .....

다른 팁

it some times happen when whitespace, special characters and new line characters are in that response so first trim that string and then print the response like bellow..

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

UPDATE:

- (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;
}

use this method like bellow..

 NSString *strResponse = [[theRequest responseString] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
 NSString *strFinal = [self flattenHTML:strResponse];
 NSLog(@"Response :%@",strFinal);
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top