我有以下代码,我想让它同步。我遇到的问题是,当我的api被调用时,我的程序继续,并且nil数据值在平均时间内传递。感谢您提前获得所有帮助。 \
{ - (IBAction)Make_Payment:(id)sender {
NSString *temp2 = [NSString stringWithFormat:@"{\n \"partner_key\": \"%@\",\n \"auth_token\": \"QaU9QcFZ6xE7aiRRBge0wZ4p6E01GEbl\",\n \"payment_account_id\": \"%@\",\n \"card_number\": \"%@\",\n \"card_exp_date\": \"%@\",\n \"amount\": \"%@\",\n \"memo\": \"%@\",\n \"recipient\": {\n \"email\": \"%@\",\n \"mobile_phone\": \"%@\"\n }\n}",[Partner_Key text], [Payment_Account_ID text], [Card_Number text], [Card_Exp_Date text], [Amount text],[Memo text], [Recipient_Email text], [Recipient_Phone_Number text]];
NSLog(temp2);
NSNumber *errorCode = nil;
NSString *errorMessage = nil;
NSString *guid1 = nil;
NSString *exponent = nil;
NSString *modulus = nil;
NSURL *URL = [NSURL URLWithString:@"https://cert.payzur.com/payzurservices.svc/payment/send/initiate"];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:URL];
[request setHTTPMethod:@"POST"];
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:[temp2 dataUsingEncoding:NSUTF8StringEncoding]];
NSURLSession *session = [NSURLSession sharedSession];
[[session dataTaskWithURL:[NSURL URLWithString:URL] completionHandler:^(NSData *data,NSURLResponse *response,NSError *error) {
if(error && failure)
{
dispatch_async(dispatch_get_main_queue(), ^{
failure(error);
});
}
else if(success)
{
dispatch_async(dispatch_get_main_queue(), ^{
success(data);
});
}
}] resume];
NSURLSessionDataTask *task = [session dataTaskWithRequest:request
completionHandler:
^(NSData *data, NSURLResponse *response, NSError *error) {
if (error) {
// Handle error...
return;
}
if ([response isKindOfClass:[NSHTTPURLResponse class]]) {
NSLog(@"Response HTTP Status code: %ld\n", (long)[(NSHTTPURLResponse *)response statusCode]);
NSLog(@"Response HTTP Headers:\n%@\n", [(NSHTTPURLResponse *)response allHeaderFields]);
}
NSString* body = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSLog(@"Response Body:\n%@\n", body);
NSError *error1;
NSDictionary *res = [NSJSONSerialization JSONObjectWithData:data options:0 error:&error];
NSLog(@"res: \n%@", res);
if (res) {
NSString *errorCode = res[@"error_code"];
NSString *errorMessage = res[@"error_message"];
NSString *guid1 = res[@"guid"];
NSString *exponent = res[@"exponent"];
NSString *modulus = res[@"modulus"];
NSLog(@"errorCode: %@\nerrorMessage: %@\nguid: %@\nexponent: %@\nmodulus: %@", errorCode, errorMessage, guid1, exponent, modulus);
}
else {
NSLog(@"Error: %@", error);
}
}];
[task resume];
}
答案 0 :(得分:0)
使用NSURLSession的完成块。 您要执行的活动,只需在完成块中执行。确保在NSURLSession的完成块中执行某些UI任务时,在主线程上执行它们。
-(void)sendRequestForURL:(NSString*)urlString Success:(void(^)(NSData *data))success Failure: (void(^)(NSError *error))failure
{
NSURLSession *session=[NSURLSession sharedSession];
[[session dataTaskWithURL:[NSURL URLWithString:urlString] completionHandler:^(NSData *data,NSURLResponse *response,NSError *error) {
if(error && failure)
{
dispatch_async(dispatch_get_main_queue(), ^{
failure(error);
});
}
else if(success)
{
dispatch_async(dispatch_get_main_queue(), ^{
success(data);
});
}
}] resume];
}