2014-01-16 107 views
0

嗨我正在嘗試爲應用程序創建一個簡單的登錄頁面。登錄將在本地應用程序上完成,而不是在網絡上完成。目前模擬器運行時沒有錯誤,但同時沒有進行認證。我可以知道如何實現身份驗證?應用程序登錄功能問題

我的代碼:

- (IBAction) login: (id) sender 
{ 
NSString *content = [NSString stringWithFormat:@"Agent_code=%@&Password=%@",[Agent_code text], [Password text]]; 

NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:@"http://test/login.php?"]]; 
[request setHTTPMethod:@"GET"]; 
[request setHTTPBody:[content dataUsingEncoding:NSUTF8StringEncoding]]; 


// generates an autoreleased NSURLConnection 
[NSURLConnection connectionWithRequest:request delegate:self]; 

NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil]; 
NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding]; 

NSLog(@"success",returnString); 
} 

PHP

<?php 

# Init the MySQL Connection 



$username = "user";  

$dbname = "db";  

$password = "pass";   

$hostname = "localhost";   

$table = "table"; 


if (!($connectDB = mysql_connect($hostname, $username, $password))) 

die ('Failed to connect to MySQL Database Server - #'.mysql_errno().': '.mysql_error()); 



if (!mysql_select_db ($dbname)) 

die ('Connected to Server, but Failed to Connect to Database - #'.mysql_errno().': '.mysql_error()); 



# GETTING PARAMETERS 

$Agent_code = $_GET['Agent_code']; 

$Password = $_GET['Password']; 



# Prepare the SELECT Query 

$selectSQL = "SELECT Agent_code, Password FROM $table WHERE Agent_code = '$Agent_code' AND Password = '$Password'"; 



# Execute the SELECT Query 

if (!($selectRes = mysql_query ($selectSQL))) { 

echo 'Retrieval of data from Database Failed - #'.mysql_errno().': '.mysql_error(); 

} else { 

    $row = mysql_fetch_assoc ($selectRes); 

    if (mysql_num_rows ($selectRes) == 1) { 

    echo 'User and password found!'; 

    } else { 

    echo 'Invalid Username or Password.'; 

    } 

} 

mysql_close($connectDB); 

?> 
+0

你是說**的NSLog(@ 「成功」,returnString); **得到(空)...? –

回答

0

確定。這些可能是問題:

  1. 您尚未正確將API傳遞到API。
  2. 響應數據尚未被正確捕獲。
  3. 響應尚未正確打印。它應該是:NSLog(@"success:%@",returnString);

請嘗試這種解決方案:

- (IBAction)login:(id)iSender { 
    NSString *aContent = [NSString stringWithFormat:@"Agent_code=%@&Password=%@",[Agent_code text], [Password text]]; 
    NSString *anURLString = [NSString stringWithFormat:@"http://test/login.php?%@", aContent]; 
    NSURL *anURL = [NSURL URLWithString:anURLString]; 

    NSError *anError = nil; 
    NSURLResponse *anURLResponse; 
    NSURLRequest *anURLRequest=[NSURLRequest requestWithURL:anURL cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:30.0]; 
    NSData *aResponseData = [NSURLConnection sendSynchronousRequest:anURLRequest returningResponse:&anURLResponse error:&anError]; 

    NSString *aResponse = [[NSString alloc] initWithData:aResponseData encoding:NSUTF8StringEncoding]; 
    NSLog(@"RESPONSE = %@",aResponse); 
} 
+0

正確的我已經試過了代碼,數據被解析成xcode。從這點開始,我該如何實現身份驗證,以便只有擁有正確登錄信息的用戶才能訪問下一頁?我知道可以使用鑰匙串存儲令牌,以便存儲登錄詳細信息。感謝致敬 – user3044463

相關問題