次のコードはほぼ完全に機能します。途中でphpを使用して、Xcodeからローカルホストのmysqlサーバーに接続しています。これは最終的にログインシステムになります。
NSString *strURL = [NSString stringWithFormat:@"http://localhost:8888/Check.php?user=%@&pass=%@",txtName.text,passName.text];
// to execute php code
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:strURL]];
NSError *e;
NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:&e];
// to receive the returned value
NSString *strResult = [[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]autorelease];
NSLog(@"%@",strResult);
NSString *success = @"Success";
if ([strResult isEqualToString:success]) {
NSLog(@"I realize that the two strings are the same");
}
else {
NSLog(@"The two strings are not the same");
}
strResult は、デバッガーに次の項目を出力します。これは、php ファイルにさまざまな条件でエコーバックするように指示している (ユーザー名とパスワードが正しいか間違っているか)。
ただし、何らかの理由で、コードの if ステートメントの部分は、出力では文字列 strResult に "Success" という単語が含まれていることが明確に示されているにもかかわらず、常に else メソッドに移動します。
両方の文字列 (strResult と success) が互いに等しいことがわかりますが、何らかの理由でXcodeではそれができないため、これは非常にイライラします。