0
我在IOS SDK下面的代碼我建:HTTP請求API調用
+ (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?
出了什麼問題'SDKCompletionBlock'? – Wain