当App是后台时,我们可以在locationManager didEnterRegion方法中调用WebService吗?如何赚取额外的时间来完成WS电话会议?

时间:2015-12-02 11:40:35

标签: ios objective-c web-services core-location geofencing

我已经建立了一个Geo Fence&使用startMonitoringForRegion方法进行监控。当输入地理围栏区域时,我能够获得事件。退出成功。我想要做的是当我进入某个地理围栏时,我想根据该位置数据进行Web服务调用。显示Web服务结果的本地通知[当应用程序处于后台/被杀时]。

beginBackgroundTaskWithName方法会有帮助吗?

简而言之:

- (void)locationManager:(CLLocationManager *)manager didEnterRegion:(CLRegion *)region
{
     // Make a Web Service Call to fetch data even 
     // **when App is killed/ in background mode.**
}
  1. 根据我的知识,当地理围栏被破坏时,应用程序进入了 背景模式。不确定多久。
  2. 我可以发送本地通知甚至应用程序被杀/在后台 当地理围栏被破坏时。现在如果我想制作网络服务怎么办? 电话&当应用不在时,将其结果显示为通知 前景。
  3. 实现这一目标的最佳方法是什么?任何帮助赞赏。提前谢谢。

2 个答案:

答案 0 :(得分:1)

发现非常有用的东西,每个应用程序都有一些额外的时间,iOS提供解决某些未完成的业务,然后应用程序进入暂停模式。对于iOS 7,时间已减少到大约180秒(3分钟);早些时候它大概是600秒(10分钟)。

为了赚取额外的时间,我们需要初始化UIBackgroundTaskIdentifier

-(void) beginBackgroundUploadTask
{
    if(self.backgroundTask != UIBackgroundTaskInvalid)
    {
        [self endBackgroundUploadTask];
    }

    self.backgroundTask = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{

        [self endBackgroundUploadTask];

    }];
}

一旦后台任务从规定的时间内用完,我们就应该使后台任务失效并结束。

-(void) endBackgroundUploadTask
{
    [[UIApplication sharedApplication] endBackgroundTask:self.backgroundTask ];
    self. backgroundTask = UIBackgroundTaskInvalid;
}

答案 1 :(得分:0)

你可以调用这样的方法

- (void)locationManager:(CLLocationManager *)manager didEnterRegion:(CLRegion *)region
{
    NSLog(@"Region Datas %@", region);

    NSUserDefaults *userid = [NSUserDefaults standardUserDefaults];
    NSString *get_userid=[userid stringForKey:@"USERID"];
    [userid synchronize];
    NSLog(@"User Id %@",get_userid);

    NSString* urlStr1 =[NSString stringWithFormat:@"http://www.test.net/test.php"];
    NSLog(@"Enter Url %@",urlStr1);
    NSURLRequest *request1 = [NSURLRequest requestWithURL:[NSURL URLWithString:urlStr1]];
    [NSURLConnection sendAsynchronousRequest:request1
                                       queue:[NSOperationQueue mainQueue]
                           completionHandler:^(NSURLResponse *response,
                                               NSData *data, NSError *connectionError)
     {
         if (data.length > 0 && connectionError == nil)
         {
             NSDictionary *result = [NSJSONSerialization JSONObjectWithData:data options:0 error:NULL];
             NSLog(@"Success Result %@",result);
         }
         else
         {
             NSLog(@"Error Result %@",connectionError);
         }
     }];

}