Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/google-app-engine/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios7 如何使用NSURL会话iOS 7使用Rest Api_Ios7_Nsurlsession - Fatal编程技术网

Ios7 如何使用NSURL会话iOS 7使用Rest Api

Ios7 如何使用NSURL会话iOS 7使用Rest Api,ios7,nsurlsession,Ios7,Nsurlsession,当我的应用程序处于后台模式时,我需要调用restapipost并获取调用。我试着阅读苹果公司的文件,这些文件建议我们不能在后台使用DATATASK。我们只能在BG中下载和上传文件。 请帮帮我。 提前感谢。我创建了一个使用GET方法并将JSON数据发送到API的方法。您也可以根据需要配置NSURLSession,我在这里使用默认会话 + (void)callGETAPIUsingSessionWithParams:(NSDictionary *)params andAPIName:(NSStrin

当我的应用程序处于后台模式时,我需要调用restapipost并获取调用。我试着阅读苹果公司的文件,这些文件建议我们不能在后台使用DATATASK。我们只能在BG中下载和上传文件。 请帮帮我。
提前感谢。

我创建了一个使用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电话。

我希望在后台使用此功能,而应用程序位于后台。此功能不适用于后台任务。后台显式不支持数据任务。