我已经建立了一个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.**
}
实现这一目标的最佳方法是什么?任何帮助赞赏。提前谢谢。
答案 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);
}
}];
}