我正在使用PHP編寫的Web服務檢查我的iPhone應用程序發送的HTTP請求,然後PHP腳本返回yes或no取決於憑據是否正確,目前我是硬編碼他們,但他們似乎並沒有按預期運行,即使PHP腳本返回yes,條件失敗,我試圖將結果更改爲硬編碼的字符串,並且這工作...Objective-C Rest Response
Here是代碼
用PHP編寫的NSString *varyingString1 = @"username=";
NSString *varyingString2 = @"username";
NSString *varyingString3 = @"&password=";
NSString *varyingString4 = @"password";
NSString *post = [NSString stringWithFormat: @"%@%@%@%@", varyingString1, varyingString2, varyingString3, varyingString4];
NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:@"http://url.com/api/login.php"]];
[request setHTTPMethod:@"POST"];
[request setHTTPBody:postData];
NSData *returnData = [NSURLConnection sendSynchronousRequest: request returningResponse: nil error: nil];
NSString *strResult = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];
NSString *result = strResult;
result = [post stringByReplacingOccurrencesOfString:@" " withString:@""];
if(result == @"yes"){
NSLog(@"%@",strResult);
[self performSegueWithIdentifier:@"picker" sender:self];
}else{
NSLog(@"No");
}
我的Web服務,然後看起來像這樣
<?php if($_POST['password'] == 'password' && $_POST['username'] == 'username') echo "yes";
else echo "no";
任何想法?
運行您的代碼後,「結果」的值是什麼? – 2013-02-12 23:42:06
使用您的調試器,檢查保存的結果值。你可能有其他的空白字符,你不知道你的變量。 – Jeremy 2013-02-12 23:44:36