iOS - Open browser before going to background - objective-c

I'm writing following code that opens browser when app will be going to background:
- (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.
*/
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:#"http://www.google.com"]];
}
but its not opening. Any suggestions?
Can I open browser in "applicationWillResignActive" or not?

You code looks fine so I guess that iOS won't let you do it. Your app has been told to go into the background - you don't get to open new apps!
PS :
Why would you ever want to do this - surely this will just really really annoy the user?

Related

How to detect and implement touch events for single tap on home button?

Not sure if it is possible but is there any way to detect a single touch on the home button. To start with, I would simply like to add an NSLog if the user touches down once on the home button (without actually pressing), but I don't know where I would add this functionality. Does Apple allow you to interact with the home button?
I looked at the app delegate methods, but I can't see how any would work in a single tap (touch) context. Would really appreciate your help.
Does Apple allow you to interact with the home button?
No, not yet. There are no APIs available to explicitly detect home button interactions.
You can rely on the traditional app delegate lifecycle function invocations to perform any logic you wanted to.
- (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)applicationWillTerminate:(UIApplication *)application
{
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

App will be locked if the app is kept in background for 2 min

i am trying to do like Users will be asked for an App Passcode while using the app for first time.
App will be locked if the app is kept in background for 2 min.
User need to provide passcode to unlock the app like banking app.. i don't know how to implement this. As i am new to Ios.
please help me. Thnx in advnce.
In your AppDelegate.m file you'll find these boilerplate 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 active 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.
}
You'll want to bring up the password dialog in either WillEnterForeground or DidBecomeActive. Read up on them to see which suits your specific needs.
I did something similar for a client a long time ago. We didn't like the visuals of the dialog appearing when the app became active so we actually brought up the dialog in DidEnterBackground. In our app it just looked better. But these are where you need to start.

Cancel all local notifications when app is closed by the user

There is a small bug in my App.
My App displays notifications at specific times when the App is running and cancel all of them whenever a button is switched.
My problem is that whenever a user closes the App using the multitasking feature of iOS the notifications are still showing up.
I tried to add the following code which doesn't work:
- (void)applicationWillTerminate:(UIApplication *)application
{
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
[[UIApplication sharedApplication] cancelAllLocalNotifications];
}
The problem is that my App should show notifications when the App is running but shouldn't show notifications when the App is terminated.
Why does the above code not work?
The correct answer is that this cannot currently be done by a multitasking app. One solution is to set a flag in our info.plist declaring your app wants to be killed when the user switches to another app - then you will get the willTerminate message (but get killed then).
There are huge numbers of threads on this topic, one which quotes an Apple doc that tells you backgrounded apps that are terminated do NOT get the willTerminate message is here.
For me, this just means I can now close an open bugreport out with a 'cannot fix' resolution :-)
Just because your app is visible in the app-changer, it doesn't mean it is still running.. it can get closed at any point. You cannot differentiate between the OS closing your app or the user closing your app.
Perhaps a button would be the solution? A button that cancels all notifications?
Or you run a real background task (which can last for about 5 minutes) and stop all notifications afterwards. Or you just schedule the notifications for the next 5-10 minutes and that's it.
For what are you using them?

Alternatives to applicationDidEnterBackground and applicationWillResignActive?

I've got an app that changes the screen brightness with [UIScreen mainScreen].brightness = newBrightness, and I want to restore the brightness to it's previous state when the user finishes using it.
I've tried these two delegate methods:
- (void)applicationDidEnterBackground:(UIApplication *)application
- (void)applicationWillResignActive:(UIApplication *)application
But without much success. I suspect my app must be in the foreground to change the brightness? When I change the brightness in didEnterBackgroundMethod, it has no effect at all. When I use willResignActive it does restore the brightness if I switch to another app, but it has no effect when I press the home button.
Are there any notifications or delegate methods that are executed before the app leaves the foreground?
It seems this happens to others as well: see this S.O. post.
Only way around it seems to be forgetting about setBrightness and simulating it by overlaying a black-semi-transparent on your view...
OLD ANSWER:
willResignActive should also be called when you press the home button before the application enters the background state.
This method is called to let your application know that it is about to move from the 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. An application in the inactive state continues to run but does not dispatch incoming events to responders.
This is also the behavior I witness. So, my guess (but it's just a guess) is that your app is not set to support background, so that when pressing the home button it is terminated. In this case applicationDidEnterBackground is not called.
I would suggest to check the info.plist file in your project for the UIApplicationExitsOnSuspend or "Select Application does not run in background" key.
Furthermore, you could try and put some breakpoints (or NSLog traces) in those functions and check whether they are effectively called as expected.
According to Apple´s DevForum it seems to be a bug that Apple don´t want to fix soon.

UIApplication kind of quits while in background

When you open up an app and press home screen again, the app is obviously in te background. When you open other apps and wait a time, the views of my app have been unloaded (like UITableView reloads data).
Is there some sort of notification or how do I know whether my app is about to release their views? Is it just viewDidUnload?
This link should help: iPhone Development - Simulate Memory Warning
Basically you received a memory warning and parts of the view got unloaded.
unfortunately, when you app is put in the background it is frozen and it will not receive events. Unless you have requested some background processing time and have provided the system with a background processing task Expiration Handler:
backgroundTask_ = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{
// Peform clean up work
// Mark the task now as invalid
[[UIApplication sharedApplication] endBackgroundTask:backgroundTask_];
backgroundTask_ = UIBackgroundTaskInvalid;
}];
in which case, after the extra, undetermined amount of processing time is over, the expiration handler will be called.
Some good background docs can be found here App States and Multitasking. But even then you won't be able to do much in the way of cleanup.
Good luck