Ios7 如何使用NSURL会话iOS 7使用Rest Api
当我的应用程序处于后台模式时,我需要调用restapipost并获取调用。我试着阅读苹果公司的文件,这些文件建议我们不能在后台使用DATATASK。我们只能在BG中下载和上传文件。 请帮帮我。Ios7 如何使用NSURL会话iOS 7使用Rest Api,ios7,nsurlsession,Ios7,Nsurlsession,当我的应用程序处于后台模式时,我需要调用restapipost并获取调用。我试着阅读苹果公司的文件,这些文件建议我们不能在后台使用DATATASK。我们只能在BG中下载和上传文件。 请帮帮我。 提前感谢。我创建了一个使用GET方法并将JSON数据发送到API的方法。您也可以根据需要配置NSURLSession,我在这里使用默认会话 + (void)callGETAPIUsingSessionWithParams:(NSDictionary *)params andAPIName:(NSStrin
提前感谢。我创建了一个使用GET方法并将JSON数据发送到API的方法。您也可以根据需要配置NSURLSession,我在这里使用默认会话
+ (void)callGETAPIUsingSessionWithParams:(NSDictionary *)params andAPIName:(NSString *)apiName andCompletionHandler:(void(^)(NSDictionary *result))completionHandler
{
NSLog(@"[GET] Request %@ : %@",apiName, params);
NSString *getURL = [self addQueryStringToUrlString:[NSString stringWithFormat:@"%@/%@",<Your Domain>,apiName] withDictionary:params];
NSURL *URL = [NSURL URLWithString:getURL];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:URL];
NSURLSession *session = [NSURLSession sharedSession];
[request setHTTPMethod:@"GET"];
[request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
NSURLSessionDataTask *task = [session dataTaskWithRequest:request
completionHandler:
^(NSData *data, NSURLResponse *response, NSError *error)
{
if([data length] > 0)
{
NSError *err = nil;
NSDictionary *dictResponse = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:&err];
NSLog(@"[GET] Response for %@",apiName);
// NSLog(@"[%@] => %@",apiName, dictResponse);
completionHandler(dictResponse);
}
else{
NSLog(@"Failed To Get Response for : %@",params);
completionHandler(@{@"result":@"Failed"});
}
}];
[task resume];
}
您可以修改它以拨打POST电话。我希望在后台使用此功能,而应用程序位于后台。此功能不适用于后台任务。后台显式不支持数据任务。