2013-08-01 26 views
1

我使用ASIHttprequest進行登錄認證Here is the deme codeASIHttprequest日誌中authenticaion

我努力的代碼是:

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

請求失敗,請求完成方法

- (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所以請......
在本演示中,當我去鍾一個警報是彈出但是當我使用我的web它給這樣

響應

響應:ASIHTTPRequest:0xa846400

但不彈出alertview用於輸入用戶名和密碼

如果有任何其他更好的方式來驗證登錄請發uggest我

我希望將登錄爲rootview併成功登錄後,可以進入到App

感謝......當空格,特殊字符和換行字符是在

+0

的NSLog(@ 「的迴應:%@」[TheReqest responseString])NOT的NSLog(@ 「迴應:%@」 ,theRequest); – Gabrail

+0

ASIhttp是已停用的庫。使用[AFNetworking](https://github.com/AFNetworking/AFNetworking) –

回答

0

進行登錄認證,使用ASIFormDataRequest按照以下步驟

1)創建一個Web服務,可以從服務器相匹配用戶名和密碼,並給予回覆if用戶名和密碼是否正確給予回覆「成功」否則「失敗」。

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

2)創建使用UsernameTextfield.text和PasswordTextfield.text這樣

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。

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

所以,如果成功進入下一個頁面,否則警報警告.....

1

它有時發生響應,從而第一修剪該字符串,然後打印像波紋管響應..

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

使用這種方法就像波紋管..

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

它給出了整個站點 – Denny

+0

,您只需在此粘貼 –

+0

響應:<!DOCTYPE html> <腳本類型= 「文本/ JavaScript的」> var _ttref = []; _ttref.push(['_ setCookie']); 登錄聘請自由職業者,工作外包,找工作| Freelancer.com 。 – Denny