2

RESTful Web サービスを使用するために使用されるさまざまなメソッドとライブラリを Google で検索しました。しかし、古いライブラリを試してみると、それらのほとんどは時代遅れであるか、機能しませんでした。これは、一部の製品が廃止され、新しい SDK と互換性がないことが原因である可能性があります。

SBJSON、ASIHTTP、stig、および jsonframework を試しましたが、どれも機能していないようです。

RESTful Web サービスを使用するために iOS で使用されている現在のライブラリにはどのようなものがありますか? 誰かが同じライブラリを使用したサンプル チュートリアルへのリンクを提供してくれると助かります。

4

3 に答える 3

6

RestKit を試す: http://restkit.org/

よく知られており、人気があり、JSON だけでなく XML も使用し、コア データ (NSManagedObjects マッピングへの応答) と連携して、ローカル キャッシュとして優れたバックエンドを作成します。

于 2012-09-20T14:46:59.290 に答える
3

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];

        }

お役に立てれば!

于 2012-09-20T14:42:53.637 に答える
0

組み込みの を使用してNSURLSessionからNSJSONSerialization、Github のMantleフレームワークを使用して、JSON ディクショナリをカスタム ObjC オブジェクトにマップします。

于 2014-05-20T21:48:48.387 に答える