我正在嘗試在Objective-C中編寫iPhone應用程序。我需要使用NSURLConnection POST數據。我可以找到的每個例子都與JSON有關;我不需要使用JSON。我所需要做的就是POST數據,並從PHP腳本中獲取簡單的1或0(成功或失敗)。而已。使用NSURLConnection POST - NO JSON
我遇到了這個代碼,但我不知道如何使用它或修改它不使用JSON:
- (void)performRequest {
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:@"http://someplace.com/"]];
[request setValue:@"Some Value" forHTTPHeaderField:@"Some-Header"];
[request setHTTPBody:@"{\"add_json\":\"here\"}"];
[request setHTTPMethod:@"POST"];
[NSURLConnection connectionWithRequest:[request autorelease] delegate:self];
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
// Fail..
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
// Request performed.
}
事實上,它不會被棄用。它已從非正式協議轉換爲正式的[NSURLConnectionDataDelegate協議](http://developer.apple.com/library/ios/documentation/Foundation/Reference/NSURLConnectionDataDelegate_protocol/Reference/Reference.html#//apple_ref/occ/ intfm/NSURLConnectionDataDelegate /連接:didReceiveData :)。我希望鏈接到這個其他參考的[NSURLConnectionDelegate協議](https://developer.apple.com/library/ios/#documentation/Foundation/Reference/NSURLConnectionDelegate_Protocol/Reference/Reference.html)文檔可以避免這種常見的混淆。 – Rob