Giter Site home page Giter Site logo

voyage11 / location Goto Github PK

View Code? Open in Web Editor NEW
606.0 58.0 153.0 77 KB

Background Location Update Programming for iOS 7 and 8 over an extended period of time. Continue to get location on both foreground and background.

Home Page: https://mobileoop.com/

License: MIT License

Objective-C 100.00%

location's Introduction

Background Location Update Programming for iOS 7 and iOS 8

Most of the solutions before iOS 7 work well because during that time the system does not have multitasking and the system does not automatically move the app from background mode to suspended mode.

So, if your app needs consistent location update from the device, you will have to implement a solution that constantly refresh the app when it is in background mode so that it has a short period of time in executing the code and send the location to your server.

I have personally used a few weeks of time in testing various solutions that I found from StackOverFlow and also Apple Developer Forum but I didn't have much luck.

I only managed to get the consistent location update on the background in iOS 7 by combining a few solutions together with my own tweaks.

The solution that I am provided might not be elegant but it is able to do what I need to do. I am constantly looking for a better solution when I have time.

I am glad that this solution helps some other iOS developers who are developing location based application. So far, this is the most popular post on my blog. If you have any question, you may join us for a discussion here: Background Location Update Programming for iOS 7 and 8.

Update on 26 September 2014: I have updated the solution to work on iOS 8.

Update on 14 October 2014: I have Moved the delay10Seconds timer to become a share property.

Personal Note

Currently, I am working as a Senior iOS Developer in Singapore. I might not be free to keep this solution updated. Please check the changes on the latest iOS to make sure that the solutions works well. Thanks.

Thanks.

Ricky

location's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

location's Issues

180 Seconds Limit

Hi:

First of all, thanks for this contribution.

I've been looking as you to reach a proper solution of unlimited update location every X time. Unfortunately I'm not able to do it even with your repository. I have set up everything, but still with this one and other solutions there is no way of break the 180 second limit.

delay10Seconds timer not set/reset when entering background

Hi

I am trying to user your code in my app, and found our few problems:

  1. I changed the main timer from 1 minute to 5 mintes, delay timer stays at 10s. When you start app, it update locations and finish after 10 seconds. Then put app to background, you it will start updating location again - and will update them in background for more than 4 minutes, when main timer reset again. I think you should not call [locationManager startUpdatingLocation] in -applicationEnterBackground. Is this a bug or you wanted it this way for some reason? Maybe it was just not that obvious with 1 minute timer, but with longer timer it is a problem. If I set timer for 1 hour, it would constantly update location for one hour after going to background.
  2. Wouldn't it be better if instead of creating separate timer for updateToServer, just put call to [self updateToServer] in -stopLocationDelayBy10Seconds ? At this point you always has the most current user location.

allowsBackgroundLocationUpdates on iOS 8.4

_locationManager.allowsBackgroundLocationUpdates = YES; will crash on ios8.4.
so I modify to
if([_locationManager respondsToSelector:@selector(setAllowsBackgroundLocationUpdates:)]) {
// We now have iOS9 and the right capabilities to set this:
[_locationManager setAllowsBackgroundLocationUpdates:YES];
}

Battery drain issue

Thank you for your solution but I have battery issue when I used it for background task.

If I didn't move, my iphone consume battery 4-5% per hour.

But if I moved anywhere, it consumed more than 10% per hour.

I already set desiredAccuracy very low and set terms between location update.

How can I reduce battery drain??

Initialisation error when integrating into a bigger app

The initialisation statement in BackgroundTaskManager.m of its property _bgTaskIdList = [NSMutableArray array]; does not allocate any space to this array. Whilst this does not produce any errors in the sample app, when integrated with more xcode modules, it becomes unreliable and can crash with EXC BAD ACCESS.
I have replaced this line in my app with _bgTaskIdList = [[NSMutableArray alloc]init]; which works fine for me. I hope this helps other users

Does this still work after iOS 10?

This is a great code!
But I found that the background timer not always work accurate after iOS 10.
Like if I set it to 10 sec, it would some time wake in more than 10 sec or even longer.

Anyone know how to deal with this situation? Thanks!

can't start new background task if time elapsed is more than 3 minutes.

Hi , Thanks for a great approach
I'm using your code , It works as expected when the app is run through xcode debugger , but when the app runs on it's own it is limited to 3 min only and then it crashes , I monitor successful location updates and updates to server through UILocalNotifications , after 3 minutes all background processes stop and the app crashes , what i found in the crash log is the following
<BKNewProcess: 0x124e37c00; com.eventagrate.JAC; pid: 8442; hostpid: -1> has active assertions beyond permitted time:
{(
<BKProcessAssertion: 0x124e37ae0> id: 8442-700206AD-3E22-4CB1-8551-6A3B5F5E4721 name: Called by JAC, from -[BackgroundTaskManager beginNewBackgroundTask] process: <BKNewProcess: 0x124e37c00; com.MYAPPUDID; pid: 8442; hostpid: -1> permittedBackgroundDuration: 180.000000 reason: finishTask owner pid:8442 preventSuspend preventIdleSleep preventSuspendOnSleep ,
<BKProcessAssertion: 0x124d1a350> id: 8442-33C56A89-43AB-43D0-8F18-A8EAA1BC3C1F name: Called by MYAPP, from -[BackgroundTaskManager beginNewBackgroundTask] process: <BKNewProcess: 0x124e37c00; com.MYAPPUDID; pid: 8442; hostpid: -1> permittedBackgroundDuration: 180.000000 reason: finishTask owner pid:8442 preventSuspend preventIdleSleep preventSuspendOnSleep
)}

I was wondering If this solution doesn't work anymore or if you have updated your code to handle this error ,
Thanks for your help

CocoaPods integration?

Hey! First of all, great project, appreciate the effort of putting together so much code separately listed in answers on StackOverflow, you saved me a lot of trouble.

However, could you maybe add CocoaPods support and a wiki page? It took me a while to figure out your "API".

Thanks a lot!

Crash with the latest version

I wanted to integrate the latest version of your awesome "library", however, I'm now getting a crash just when restarting the location updates. All was fine with the previous one. I'm using iOS 7.1. Can you please take a look on it ? Thank you very much.

Issue when phone lock and app in background more time

Hi

I used your code but it is working maximum 1 to 2 hours after that it is not working.
My App is similar like Uber so I need every 2 to 3 mins update lat long on server.

Please let know if any Query.

My Skype ID - g4ever81

Waiting for your reply.

Thanks

New Request - Update View after location has been retrieved

I am trying to update the view as soon as the location has been retrieved. I added the below code in viewDidLoad method of my UIViewController.

[ [LocationShareModel sharedModel] addObserver:self forKeyPath:@"myLocationArray" options:NSKeyValueObservingOptionNew context:nil];

and added below code in my UIViewController for receiving the update.

-(void) observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {

if ( [keyPath isEqualToString:@"myLocationArray"] ) {
    NSLog(@"changedValues: %@", change);
    NSLog(@"Received value");
}

}

The observerValueForkeyPath is not being called.

Highly appreciate your help in this.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.