GeoFencing on iOS - iphone

I am using GeoFencing to set a region and perform some task when user Enters or Leaves the region. Bt it is not working for me, i am using gollowing code
CLLocationCoordinate2D coord = CLLocationCoordinate2DMake(some values,some values);
CLRegion *region = [[CLRegion alloc]initCircularRegionWithCenter:coord radius:15.0 identifier:#"SF"];
[locationManager startMonitoringForRegion:region desiredAccuracy:kCLLocationAccuracyBest];
But None of the delegates didEnterRegion and didExitRegion being called when i close the application and the app is monitoring and i move away from that region or enter.

You define a radius of 15m. That is too small. First, try with 50m or more. Look at the map app, if you really have entered that region.
Furthermore, I hope you meant "send to background", instead of closing.
make sure you have set your app to be allowed to run in background? (via plist)
Update:
Finally it turned out that you terminated the application. Then of course you will not get any loctaion events anymore.
Solution:
Don't terminate it via double click on home button follwed by selecting the white cross, simply close it with one click on home button, then it has chance to run in background.
Update2:
Even when the app was terminated, ios relaunches it when it has registered to the location monitoring service:
from CllLocationManager Class Reference
If you start this service and your application is subsequently
terminated, the system automatically relaunches the application into
the background if a new event arrive...

Add CLLocationManagerDelegate in header file.
make sure u added
locationManager.delegate = self;
try increasing the radius and check.

First of all increase the radius you are giving for that particular location and check again if it does not work then by following some easy steps provided in this tutorial you can easily implement Geo-Fencing in your app.
Reference: Apple Documentation

You define a radius of 15m. That is too small. First, try with 50m or more. Look at the map app, if you really have entered that region.
Furthermore, I hope you meant "send to background", instead of closing.
make sure you have set your app to be allowed to run in background? (via plist)

Related

CLLocationManager singleton iphone example

I am new to iPhone development, i am creating a location based app. i have searched lot related to location and came to knew that use one object ( singleton pattern ) of CLLocationManager through out app, in my app i have to update user's location to server using web service,
UPDATE
as discussion with Umer i came to knew that i can use one object in appdelegate of CLLocationManager and implement delegate methods in that, and update user location to server in app delegate ,
So, is it good to do in appDelegate ??
please help.
Your locationservices will run both in foreground and background, but to allow it to run in background (when the app is open and minimized), you need to declare a special background mode in your projects "Plist" file, if I'm not wrong.
The method didUpdateToLocation will give you updates every time there is a significant movement, which you can define and adjust through various settings, such as CLLocationAccuracy (locationManager.desiredAccuracy and locationManager.distanceFilter).
Depending on what desired accuracy you ask, battery main drain quicker or not.
I don't understand what you mean with "web service". These are not web-related, they're native functionalities of iOS.
You can just call the delegate method of locationManager StartUpdatingLocation when user moves to significant distance & also call the web-service for updating User's Location.
Both Task are done in Background mode.
UPDATED ANSWER
SOURCE CODE

how to catch the power button and home button on iPhone?

Today I went to job interview, I encountered three questions now.they hope i provide some Solutions or
Thinking.
1.when user press Home button,app don't go back the main interface or delay 5 seconds to go back the
main interface.
2. when the screen is black ,how to auto activation screen(how to catch power button press event) .
3.how direct launch my app when iPhone is start-up
My english is very bad, i hope everyone can understand what i said above.Thank you very much
None of these are possible on an iOS device, unless it's jailbroken.
Q1 and Q2: not possible
Q3: The closest you can get to desired behaviour is kiosk (store demo) mode of operation.
See more here:
Lock-down iPhone/iPod/iPad so it can only run one app
It's not exactly what you're looking for - it limits the device to use only certain app - but to my knowledge the only way to auto-start an app without jailbraking the device.
I know that this question is old, but there are easy work arounds for every question on here that work for at least iOS 7+, although there is no way to do question 2 without using private APIs. You can successfully answer questions 1 and 3 with public API answers (although they are admittedly hacky)!!
1. When user presses home button, how do you delay 5 seconds before returning to the main screen?
Oscar Gomez's answer was spot on. While you cannot delay the UI from returning to the home screen without blocking the core run loop (which will get you rejected from the app store), you can use some background process techniques if needed to get your extra 5 seconds, just not with UI.
2. How do you catch the Power Button presses?
This is for sure using a private API, and while you can PROBABLY get into the App Store, you probably won't last long once Apple gets wind of you doing this. It is also a hack. You cannot listen directly to the power button, but you CAN listen to the screen going on or off, or both at once. Here is a small code snippet that I have in an Enterprise App:
Inside UIAppDelegate
static int const DisplayOnOffObserver = 54321876;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
...
CFNotificationCenterAddObserver(CFNotificationCenterGetDarwinNotifyCenter(), NULL, &PLDisplayOnOff, CFSTR("com.apple.springboard.hasBlankedScreen"), NULL, 0);
...
return YES;
}
static void PLDisplayOnOff(CFNotificationCenterRef center, void* observer, CFStringRef name, const void* object, CFDictionaryRef userInfo) {
...
DO SOME MAGIC
...
}
- (void)applicationWillTerminate:(UIApplication *)application {
...
CFNotificationCenterRemoveEveryObserver(CFNotificationCenterGetDarwinNotifyCenter(), &DisplayOnOffObserver);
...
}
Obviously you have to have the application ALWAYS running in the background (which can get tricky) or you have to only care to do it within the time that you ask for to run in the background (which isn't hard)
First step is listening to the darwin notification for the screen being toggled on/off.
Second step is implementing the callback.
Third step is stopping listening for the call back.
NOTE: DisplayOnOffObserver is a random number (not very random in my case)
3. How can you directly launch your app when the iPhone starts up?
Assuming that by directly launch, you mean launch in the background, there is actually an Apple supported way of doing this. You first have to enable Background Services for location. Second, you have to start listening for significant location changes. I don't know how well this is documented, but as soon as the iPhone boots, it tries to get it's location. When it does this, it goes from not having a location to having one (which is a significant change). Your app will launch in the background which includes calling the application:didFinishLaunchingWithOptions: method, where you can ask for more time and start other processes to permanently run in the background until the user kills your application manually. If you do permanently run in the background using this method, you do stand a chance of being rejected by Apple.
Hope any of this helps someone! If anyone needs more information, just leave a comment and I will update my answer.
When user press Home button,app don't go back the main interface or delay 5 seconds to go back the
You can't, unless you don't want your app in the appstore... The only thing you CAN do is ask for more time to save your data before your application is terminated, but the UI experience will still be the same - the iphone will go back to the main interface.
when the screen is black ,how to auto activation screen(how to catch power button press event) .
Not possible.
3. How direct launch my app when iPhone is start-up
I don't think you can do this even, with private APIs, and of course with your app not accepted in the appstore.

Background location updates for iOS display GPS icon the entire time

My app runs significant location change updates in the background. However the GPS display icon never turns off..even when app is in the background. Is there a way to use location manager with Significant location change in the background and have the GPS icon NOT display continuously? My users don't understand that it is only periodically obtaining location coordinates and instead think its constantly running in background and thus deleting app thinking its too power intensive. Please help.
I believe that any use of CoreLocation will prompt the location arrow. That includes any of the geofencing CLRegion use, -startMonitoringForSignificantLocationChanges, and -startMonitoringForLocation. I think that is Apple's safeguard that something is using your GPS, even in limited use.
That arrow will be visible till you unregister your application from significant change. But I faced problem, what I can't fine point where I can do this. In my case will be best to unregister on application kill, but with multitasking there is no such ability to handle this moment to unregister.

Executing code in background

I have a application that periodically (via an NSTimer) asks for the users location using locationManager:startUpdatingLocation I want the locationManager to run in the background so have entered "UIBackgroundModes = location" in the info.plist file.
My question is I am not seeing the results I expected, am I right in thinking that when I press the home button the application delegate calls "applicationDidEnterBackground" but although locationManager is allowed to continue my NSTimer is getting suspended (and as a consequence its not calling startUpdatingLocation to periodically query the devices position). Any ideas / solutions would be much appreciated ...
Gary.
All NSTimers are invalidated when entering the background. You need check via the CLLocationManager for any changes.
Just keep the startUpdateLocation running, you delegate will receive any major changes of the location.
When running in the background, you will only receive location changes based on cell towers.
If you read the documentation for -applicationDidEnterBackground: you'll find that yes, your timers are invalidated. Furthermore, the iOS Application Programming Guide tells you exactly how to track location from the background.

Turning off the GPS updates when user hits the home button on iPhone

I have an application with four tabs. Two of the tabs use location updates and other two don't require GPS. I use following code to turn off the location updates when the view is about to disappear
(void)viewWillDisappear:(BOOL)animated {
[self.locationManager stopUpdatingLocation];
}
This seems to work when I change tabs. The puzzle is that when user hits the home button while in the tabs which do not require location information then GPS is turned off. However if the user hits the home button while in one of the tabs that is using location updates then the GPS is left turn on. I think it is because "viewWillDisappear" is never called when the user hits the home button.. Am I right in that assumption, any suggestion on how to fix this problem.
Soofi
Add the "UIBackgroundModes" key to your -Info.plist but don't specify any values, that should ensure location services are not used in the background.
well, unless you've specifically set up the app to have background services for gps, it won't continue running, and I'm guessing you have not done so, otherwise, you wouldn't be asking how to stop it...however, for completeness, just in case you really do want that, I think you want the applicationDidEnterBackground method