2013-12-20 20 views
0

我想將一個變量傳遞給一個php頁面,但它沒有被傳遞到頁面或被保存在數據庫中。請參閱以下代碼中的註釋:從目標C發送變量到php Cocoa

NSString *[email protected]"fname"; 
NSString *[email protected]"lname"; 
NSString *urlString = [[NSString alloc] initWithFormat:@"http://localhost:8888/Test-objective%20c%20/send.php?first_name=%@&last_name=%@",fname,lname]; 

NSLog(urlString); 

NSURL *url = [ NSURL URLWithString:urlString]; 
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url]; 
NSAlert *dq= [[NSAlert alloc] init]; 
[dq setMessageText:urlString]; 
[dq runModal]; 
[request setHTTPMethod:@"GET"]; 

NSURLConnection* connection = [[NSURLConnection alloc] initWithRequest:request delegate:self]; 
[connection start]; 

NSAlert *d= [[NSAlert alloc] init]; 
[d setMessageText:@"connection"]; 
[d runModal]; 

//This is the part I don't understand. 
//This line has a problem: 
NSString *urlString = [[NSString alloc] initWithFormat:@"http://localhost:8888/Test-objectivec/send.php?first_name=%@&last_name=%@",fname,lname]; 

//But when I replace it with 
NSString *urlString = @"http://localhost:8888/Test-objectivec/send.php?first_name=My&last_name=nme"; 

//Then it works just fine. 

此外,任何人都可以告訴我如何看到服務器的響應?

+0

將日誌更改爲'NSLog(@「URL string:%@」,urlString);'。調試,創建了一切嗎? – Wain

回答

0
NSString *[email protected]"fname"; 
NSString *[email protected]"lname"; 
NSString *urlString = [[NSString alloc] 

NSString *urlString = [[NSString alloc]initWithFormat:@"http://localhost:8888/Test-objectivec/send.php?first_name=%@&last_name=%@",fnamelname]; 
  • 從「測試目標C」,其轉化%20這一情況問題去掉空白。
+0

%20在目標c中作爲特殊對待 –

+0

我已經刪除了空格,但變量未插入到數據庫中。最新的問題,當我通過變量賦值,否則通過靜態first_name和last_name,正如我在我的問題中解釋的,它工作正常之前。 – mano

+0

SO添加%%佔位% –

0

當您使用格式創建字符串時,您需要注意格式中的特殊字符。特別是在你的情況下%。格式應該是:

NSString *urlString = [[NSString alloc] initWithFormat:@"http://localhost:8888/Test-objective%%20c%%20/send.php?first_name=%@&last_name=%@",fname,lname]; 
+0

代碼工作正常,現在當我使用NSString * urlString = @「http:// localhost:8888/Test- ?的ObjectiveC/send.php FIRST_NAME =我和姓氏= NME「;但是當使用NSString * urlString = [[NSString alloc] initWithFormat:@「http:// localhost:8888/Test-objectivec/send.php?first_name =%@&last_name =%@」,fnamelname];它不起作用。 – mano

+0

評論中的鏈接無效。將代碼更新添加到問題底部(並檢查格式)。 – Wain

+0

我在我的問題中添加了「這是我不明白的東西」我不明白的主要問題 – mano