I am making an application in which have to show user current location on map with some time set, i have worked on that and it is working for me, however if i leave the app open and after 5 minutes app goes to inactive state i stop the location update and after 5 minutes if i again relock the slider and check the app, it comes into active state , have called location update and show the current location, however few times it got crashed from inactive to active state
- (void)applicationWillResignActive:(UIApplication *)application {
[locationManger stopUpdatingLocation];
}
- (void)applicationDidBecomeActive:(UIApplication *)application {
locationManger.delegate = self;
[locationManger startUpdatingLocation];
}
If you only have to show the user's location on the map, then you dont need CLLocationManager. Just use
mapView.showsUserLocation = YES;
Related
I've tried this iOS Sprite Kit tutorial and have created a similar app. However, I notice that when I press the home button to go to iOS home screen, I get a bad access exception in xCode. When I go back into the app, it starts from the beginning.
How can I properly close/minimize a Sprite Kit app to avoid that exception?
I tried this within the view controller presenting the scene, but it does not get called:
-(void)viewWillDisappear:(BOOL)animated
{
SKView * skView = (SKView *)self.view;
skView.paused = YES;
[super viewWillDisappear:animated];
}
I found out that there's Kobold Kit sprite engine built on top of Sprite Kit, after porting my project to that, i can minimize the app and restore it with the same stuff on the screen!
I believe that the proper way to handle minimizing an app is in the AppDelegate via these methods :
- (void)applicationWillResignActive:(UIApplication *)application
{
// Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
// Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}
- (void)applicationDidEnterBackground:(UIApplication *)application
{
// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}
- (void)applicationWillEnterForeground:(UIApplication *)application
{
// Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}
- (void)applicationDidBecomeActive:(UIApplication *)application
{
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}
I have an application where there are 3 tabs to calculate distance and all. When I first launch the app, on clicking 3 rd tab some network call is happening. Now I put the application to background.
When the application comes to foreground, it should call viewwillappear to go for the network call again. but it is not happening. it is not calling viewwillappear.
How can I check when application comes to foreground, it should check for 3rd tab and call network method
Please help me
When application comes to foreground,
- (void)applicationWillEnterForeground:(UIApplication *)application;
of the app delegate is called.
You can restart all your paused tasks in:
- (void)applicationDidBecomeActive:(UIApplication *)application;
- (void)applicationDidEnterBackground:(UIApplication *)application
{
//save in NSUserDefaults (or wherever) which tab is currently active
}
- (void)applicationWillEnterForeground:(UIApplication *)application
{
// read from NSUserDefaults which tab was active before,
// and use an IF statement to control the further behavior
}
- (void)applicationDidEnterBackground:(UIApplication *)application {
if(tab3){
[viewController3 netWorkCallFromHere];
}
}
in this approach you will have to declare BOOL tab3 in Appdelegate.
set it true in third viewController and set it false in another viewController .
when it returns from the background then it will check the flag and it will work accordingly.
- (void)applicationDidEnterBackground:(UIApplication *)application
{
// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}
- (void)applicationWillEnterForeground:(UIApplication *)application
{
NSLog(#"%d",tabBar.selectedIndex);
if (tabBar.selectedIndex == 2) {
NSLog(#"Your work");
}
}
I am new to location based applications.
My requirement is, device has to show the location based notifications when the user reaches the selected region.
I implemented perfectly. This app is working on background also.
Now my new requirement is, device has to show the location based notifications even after kill the app. [I saw a couple of iPhone apps working with this functionality. The apps are "Reminder" & "Locationizer" ] .
Can you please check my implementations steps as follows.
Launch the application.
Selected the location alert button.
Called the following CLLocationManager API's to monitor my region.
[locationManager startMonitoringForRegion:#"MyRegion" desiredAccuracy:kCLLocationAccuracyBest];
[locationManager startMonitoringSignificantLocationChanges];
Quit the app [Running in background].
I am getting location based alert notifications properly.
Here the new requirement.
Launch the application.
Selected the location alert Button.
Called the following CLLocationManager API's to monitor my region.
[locationManager startMonitoringForRegion:#"MyRegion" desiredAccuracy:kCLLocationAccuracyBest];
[locationManager startMonitoringSignificantLocationChanges];
Kill the app. [now application is not running in background]
App should show the location based notification when user reach the location.
How do I implement this logic?
Hi Friends I found solution for this issue.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
id locationValue = [launchOptions objectForKey:UIApplicationLaunchOptionsLocationKey];
if (locationValue)
{
[self initLocationMonitoring]; // this will invoke CLLocationManager
}
return YES;
}
An app that is quitted cant perform any functioning...so i will suggest to recheck those apps ..they must be functioning in the background to check the location of the device...they won't be able to alert if they are quitted.
I have a NSTimer that I am stopping when my application resigns active, which I later restart when the application again becomes active. What I did not realise was that applicationWillResignActive would fire when my application first started (pretty obvious really). So what is happening is my NSTimer is incorrectly starting (when the application first starts). My question is, is there a way to check for the application resuming from inactive as apposed to it first starting?
- (void)applicationWillResignActive:(UIApplication *)application {
// STOP CORE TIMER
[[[self mapController] coreTimer] invalidate];
[[self mapController] setCoreTimer:nil];
}
.
- (void)applicationDidBecomeActive:(UIApplication *)application {
// START CORE TIMER
[[self mapController] setCoreTimer:[NSTimer
scheduledTimerWithTimeInterval:ONE_SECOND
target:[self mapController]
selector:#selector(timerDisplay:)
userInfo:nil
repeats:YES]];
}
There is the applicationWillEnterForeground which seems to only fire when the app comes back from the background. Just tested it, won't be called on launch.
Discussion
In iOS 4.0 and later, this method is
called as part of the transition from
the background to the active state.
You can use this method to undo many
of the changes you made to your
application upon entering the
background. The call to this method is
invariably followed by a call to the
applicationDidBecomeActive: method,
which then moves the application from
the inactive to the active state.
In my app I am using CLLocationManager and AdWhirl. I have made no specific development regarding background mode: I don't want my app to be working when it is in background, i.e. when the user press the "home button", GPS location should no be updated.
Yesterday evening I pressed "home button", and this morning the iPhone was out of battery. It's an iPhone 4 with iOS 4.1, not jailbreaked, and there is no background app running.
The battery was about 35% yesterday evening, and 0% this morning (iPhone was shutdown).
I have set breakpoint in my delegate, which is called each time GPS location is updated. When app is in background mode, delegate is not called. So I'm thinking GPS is really disabled in background mode: ok.
This morning, I am following battery drain: it's about 1% drop each 15 min. I think it a bit too much.
Should I do something specific when the app goes to background mode? Do you think this 1% drop is normal?
Yes, internet access and GPS are two big drains on battery. I don't know at all what you mean with normal, since no other apps are running you've concluded that that is in fact what happens :) Assuming you've tested with NO apps running and didn't get 1% per 15 minutes...
For adwhirl, it's unknown whether it already stops accessing the internet when the app goes into the background, but you can add this to your App Delegate:
- (void)applicationDidEnterBackground:(UIApplication *)application {
/*
Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
If your application supports background execution, called instead of applicationWillTerminate: when the user quits.
*/
[lm stopUpdatingLocation];
[adView ignoreAutoRefreshTimer]
}
- (void)applicationWillEnterForeground:(UIApplication *)application {
/*
Called as part of transition from the background to the active state: here you can undo many of the changes made on entering the background.
*/
[adView doNotIgnoreAutoRefreshTimer]
[lm startUpdatingLocation];
}
(lm and adView are the Location Manager object and the adWhirlView, both declared in the App Delegate. I've found it more useful to do all location managing via methods I make in the App Delegate.)