0

我正在构建的 IOS SDK 中有以下代码:

+ (void) makeRequestToEndPoint:(NSString *) endpoint values:(NSMutableDictionary *) params onCompletion:(SDKCompletionBlock) responseHandler
{
    [params setObject: key forKey: @"key"];

    NSString * urlString = [self createApiUrlFromEndpoint: endpoint];
    NSURL * url = [NSURL URLWithString: urlString];

    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL: url];
    request.HTTPMethod = @"POST";
    [request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
    [request setValue:@"charset" forHTTPHeaderField:@"utf-8"];
    [request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
    request.HTTPBody = [[params urlEncodedString] dataUsingEncoding:NSUTF8StringEncoding];

    [NSURLConnection sendAsynchronousRequest:request
                                       queue:[NSOperationQueue mainQueue]
                           completionHandler:^(NSURLResponse *response, NSData *data, NSError *error)
    {

        NSError * dicError = nil;
        NSDictionary * dictionary = nil;

        if([data length] >= 1) {
            dictionary = [NSJSONSerialization JSONObjectWithData: data options:kNilOptions error: &dicError];
        }

        responseHandler(dictionary, error);
    }];

}

因此,使用 SDK 的人可以通过执行以下操作进行 API 调用:

[SDK makeRequestToEndpoint:@]

构建上述代码以方便人们使用 SDK 的最佳方式(处理错误处理、响应处理等的最佳方式)是什么?

4

1 回答 1

0

有许多开源框架,您可以从中学习异步网络的良好设计实践。我建议你看看

于 2013-10-07T16:50:27.940 回答