How to test UILocalNotification in didFinishLaunchingWithOptions - objective-c

I would like to test how my app behaves when the app is quit and the user taps a notification in notification center so that my app is launched. In - didFinishLaunchingWithOptions you can check for the key UILocalNotification *notification = [launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey]; but how can I debug this?
If i start running the app in Xcode then I quit the app it will lose the connection to Xcode.

If you set your Xcode Scheme to launched manually (Edit Scheme > Run > Info), then you can stop running your app after you've scheduled your UILocalNotification.
Then if you run your application again, it won't launch until you launch it from the simulator/device.
Then you could wait for the notification to fire and click the notification which then launches your app (manually!). That way you can debug that UILocalNotification from the launchOptions of application:didFinishLaunchingWithOptions:
Just note that for debugging, you'll probably want to schedule your UILocalNotification for a date about 10 seconds or so in the future, it's always a pain to debug time related stuff.
Good luck.

the didFinishLaunchingWithOptions: method will not be called until you launch the app. But closing your app does NOT make your app loses its connection with xcode, as your app goes in background.
Maybe i missed something, but you actually can run your app in your device and then proceed as you wish.
Also you can try this code in didFinishLaunchingWithOptions: if the log are still not working.
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:#"options" message:[launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey] description] delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alertView show];
return YES;

Keep your application running in background connected to xcode in debug mode and let one local notification arrive, then go to notification center and click on that notification and didReceiveLocalNotification: function will be called.
The only difference between local notification arriving when application is completely closed is that you have to write code in didFinishLaunchingWithOptions (you can copy paste same code you have written for didReceiveLocalNotification: function) and didReceiveLocalNotification: is not called.
Hope this info helps you..

May be this is a stupid answer. :)
I usually put a UIAlertView there. And set the text in alertview depending on the local notification.
Another is quit the app. Put some NSLog's in applicationDidFinishLaunching. Quit the App. Go to Xcode->Organiser. Then select your device under Devices tab. Select the options Console. You can see the logs there.
Hope this may help.

Related

background location iOS 8

Hello Devs: I am working on an app where I would like to fetch users location in background and send push notifications to him as soon as the user arrives at that particular location. Here is what I have done so far with my locatioManager in my app delegate
locationManager = [[CLLocationManager alloc] init];
[locationManager setDelegate:self];
[locationManager setDesiredAccuracy:kCLLocationAccuracyBest];
if (IS_OS_8_OR_LATER) [locationManager requestAlwaysAuthorization];
[locationManager startUpdatingLocation];
I have set up my info.plist to always request location. I also get the message that my app will be using location in the background when I install the app on my device. However when I close the app and arrive at the specific location I don't get any push notifications or alerts until I launch the app. I turned on Background mode --> location updates under capabilities section and then everything works absolutely fine. I receive notification seamlessly without launching the app. This is all good but when I close the app I see a blue bar on my status bar saying that my app is tracking the location in background. How do I hide that blue bar on the top? I am pretty sure this is going to scare away my users and they will remove my app instantly. To make long story short how do I accomplish this? I know this question has been asked and answered several times in past but all those answers are 2-3 years old and don't seem to work with new iOS 8. I need to get the user location in background in order for my app to work or else its useless. I will really appreciate any help or suggestions to this.
Thanks!
What you want to do is called (background) Geofencing. Your app doesn't need to calculate it by itself since CoreLocation already offers this feature.
Please have a look at this answer from Daniel.
The Geofencing feature will wake up your app when the users gets into the target zone, and will not display the blue bar.

ios 7 : didreceiveremotenotification fetchcompletionhandler not getting called when app kept in background for over night testing

I have used VoIP and remote notification as background modes.
I kept my application idle in background for overnight testing.
I have written following code in applicationDidEnterBackground
- (void)applicationDidEnterBackground:(UIApplication *)application
{
[[UIApplication sharedApplication] setKeepAliveTimeout:600 handler:^{
NSString *message = #"voip keep alive timeout executed....";
NSLog(#"%#",message);
[Logger addEntry:CAT_ML_CORE andSubCategory:SUBCAT_DEBUG andMessage:message];
}];
}
after 1 day keeping application idle in background I found "voip keep alive timeout executed...." got printed in my logs.
This indicated that my app is alive.
But when I send push notification to my app "didreceiveremotenotification fetchcompletionhandler" method doesn't get called.
Which is contradictory to the statement made by Apple "Unlike the application:didReceiveRemoteNotification: method, which is called only when your app is running, the system calls didreceiveremotenotification fetchcompletionhandler method regardless of the state of your app. If your app is suspended or not running, the system wakes up or launches your app and puts it into the background running state before calling the method."
Can someone please tell me why this is happening?
Make sure your APNS payload has "content-available"
content-available - number - Provide this key with a value of 1 to indicate that new content is available.This is used to support Newsstand apps and background content downloads.
https://developer.apple.com/library/ios/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/Chapters/ApplePushService.html

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?

iOS - Open browser before going to background

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?

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