NSURLConnection のような iOS API クラスを使用しないのはなぜですか? (iOS5以上必須だと思います)
たとえば、次のように REST GET 操作を呼び出すことができます。
NSMutableURLRequest *req = [NSMutableURLRequest requestWithURL:url];
[req setHTTPMethod:GET];
[NSURLRequest setAllowsAnyHTTPSCertificate:YES forHost:[url host]];//for https
connection = [[NSURLConnection alloc] initWithRequest:req delegate:self startImmediately:YES];
url は、残りのサービス操作の URL を指す NSURL オブジェクトにする必要があります。そして、対応するデリゲート メソッドを宣言します。
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
NSHTTPURLResponse* httpResponse = (NSHTTPURLResponse*)response;
code = [httpResponse statusCode];
NSLog(@"%@ %i",@"Response Status Code: ",code);
[data setLength:0];
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)d {
[self.data appendData:d];
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
[[[UIAlertView alloc] initWithTitle:@"Error"
message:nil
delegate:nil
cancelButtonTitle:@"ok"
otherButtonTitles:nil] show];
self.connection = nil;
self.data = nil;
}
接続、データ、およびコードは、実装クラスのローカル変数である可能性があります。これらの変数には、確立された接続、受信した JSON データ (または何でも)、および 200、404 などの応答 http コードを格納します。
最後に、安全な REST サービスを呼び出す予定がある場合は、authenticationchallenge デリゲートを含めることを忘れないでください。
- (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge {
//set the user and password loged in
NSString *username = @"username";
NSString *password = @"password";
NSURLCredential *credential = [NSURLCredential credentialWithUser:username
password:password
persistence:NSURLCredentialPersistenceForSession];
[[challenge sender] useCredential:credential forAuthenticationChallenge:challenge];
}
お役に立てれば!