2011-12-05 151 views
0

我正在使用以下代碼與HTTP服務器建立連接。在這裏,我將如何修改我的代碼,以便通過UITextField的輸入提供用戶名和密碼,並且下次將使用將保存在NSUserDefaults中的相同數據進行連接。從UITextField獲取用戶名密碼iPhone

的NSString * urlAsString = @ 「http://abc.com/services/user/[email protected] &密碼= 123456」;

//urlAsString = [urlAsString stringByAppendingString:@"[email protected]"]; 
//urlAsString = [urlAsString stringByAppendingString:@"&pass=123456"]; 

NSURL *url = [NSURL URLWithString:urlAsString]; 

NSMutableURLRequest *urlRequest = [NSMutableURLRequest requestWithURL:url]; 

[urlRequest setTimeoutInterval:30.0f]; 

[urlRequest setHTTPMethod:@"POST"]; 

NSOperationQueue *queue = [[NSOperationQueue alloc] init]; 

[NSURLConnection sendAsynchronousRequest:urlRequest queue:queue completionHandler:^(NSURLResponse *response,NSData *data, NSError *error) { 

    if ([data length] >0 && error == nil){ 

     NSString *html = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]; 

     NSLog(@"HTML = %@", html); 

     receivedData = [[NSMutableData data] retain]; 

    } 
    else if ([data length] == 0 && error == nil){ 
     NSLog(@"Nothing was downloaded."); 

    } 

    else if (error != nil){ 
     NSLog(@"Error happened = %@", error); 
    } }]; 

回答

2
NSString *urlAsString = [NSString stringWithFormat: 
     @"http://abc.com/services/user/validateemail=%@&password=%@", 
      myUsernameTextField.text,  // [email protected] 
      myPasswordTextField.text ];  // 1234565 
+0

感謝名單,但現在當我嘗試這個代碼,它被賦予的錯誤,沒有得到連接 – AppDeveloper

+0

這已經工作過?如果是這樣,urlAsString的值與硬編碼時相同嗎? – Rayfleck

+0

不,它不工作,甚至一次...而與以前的代碼,它工作正常,當我們的價值觀直接給予,而不是從文本字段 – AppDeveloper

相關問題