IOS 7 background fetch - objective-c

I am developing a app that use the background fetch.
I would need that the function it execute in background each 5sec, but with the code that I saw in google, the SO decide when the function should to be call.
I would want the this function it execute each 5sec.
The my code is here:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Override point for customization after application launch.
[[UIApplication sharedApplication] setMinimumBackgroundFetchInterval:UIApplicationBackgroundFetchIntervalMinimum];
return YES;
}

There's no way to do this. iOS is responsible to decide when it's best to give your app a chance to fetch content:
At appropriate times, the system gives background execution time to the apps that support this background mode, launching the app directly into the background if needed.
(From the docs)

Related

+[CATransaction synchronize] called within transaction right on app start

This is very odd...
The message appears right in application didFinishLaunchingWithOptions. Even with a totally empty method.
Say like this:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
[self.window makeKeyAndVisible];
return YES;
}
And, yes, I'm aware that this doesn't work since there's no rootController defined for the window.
So the app crashes of course - but I still get this +[CATransaction synchronize] called within transaction.
The app actually works and doesn't crash - but it's still weird as hell and I'd like to get rid of that...
Any ideas?
[Edit]
Here's the stack trace of the app start...

iOS Handle notifications after starting app from not-running state

I've been trying to handle receiving notifications in my app, but its not really working out.
When I use didReceiveLocalNotification:(UILocalNotification *)notification. I can receive and use the notification that is used to enter the app, without any problems
However, this function is only fired when the app is already running (active, inactive, background, and possibly suspended, but I haven't tried that yet).
Now, there is this function didFinishLaunchingWithOptions:(NSDictionary *)launchOptions where you can use [launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey] which would return a UILocalNotification.
However, when you launch the app from not-running state, this event is not fired. The LocalNotification then opens the app, but I can not use it in any way.
Now, my question is: How can I make it work, so I can receive and process notifications when starting the app, from a notification, when the app is in not-running state? Is there perhaps something I'm doing wrong here?
Here is a bit of sample code from my app:
First, the didFinishLaunchingWithOptions function, which, unfortunatly does not work. The function [sharedLocalNotificationsInstance processNotification:notification] is never launched...
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{
LocalNotificationsController *sharedLocalNotificationsInstance = [LocalNotificationsController sharedLocalNotificationsInstance];
[sharedLocalNotificationsInstance checkNotifications];
UILocalNotification *notification = [launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey];
if ( notification != nil ) {
// Process the received notification
[sharedLocalNotificationsInstance processNotification:notification];
application.applicationIconBadgeNumber = 0;
}
return YES;
}
And a second piece of code: The didReceiveLocalNotification function, which works perfectly: I receive the notification, and [sharedLocalNotificationsInstance processNotification:notification] works perfectly.
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification{
// Used when the application launches from a notification
LocalNotificationsController *sharedLocalNotificationsInstance = [LocalNotificationsController sharedLocalNotificationsInstance];
// Process the received notification
[sharedLocalNotificationsInstance processNotification:notification];
}
This is how iOS handles local notification. It depends on which state of your app, e.g. active, running in background, or not started yet. The iOS will invoke either didFinishLaunchingWithOptions or didReceiveLocalNotification, or won't touch your app at all.
Please see this article for clarification - http://www.thekspace.com/home/component/content/article/62-uilocalnotification-demystified.html
<Matrix-Morpheus-Meme title="WHAT IF I TOLD YOU">
When an application is launched from the "not-running" state because a user tapped on a local notification alert, the application has been started by iOS, not by Xcode, thus IT IS NOT RUNNING UNDER THE DEBUGGER. You cannot breakpoint inside it and neither does NSLog() send anything to the Xcode console. Test with a UIAlertController.
</Matrix-Morpheus-Meme>

UILocalNotification when app is closed

Using the UILocalNotification when the app is open, this function in the app delegate is fired :
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
{
but when the app is close and not in the background, it starts the app when i hit the notification massage, but it doesnt fire this method.
i need to fire it because she is the one who take me to another scene-that i need to present when someone get the notification.
it works only when she is on background .
You have to implement application:didFinishLaunchingWithOptions:. The notification will be one of the options.
- (BOOL)application:(UIApplication *)application
didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
UILocalNotification *notification = [launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey];
if (notification) {
// handle your notification here.
}
}
From the specs:
If the action button is tapped (on a device running iOS), the system
launches the application and the application calls its delegate’s
application:didFinishLaunchingWithOptions: method (if implemented); it passes
in the notification payload (for remote notifications) or the local-notification
object (for local notifications).
In other words, application:didReceiveLocalNotification is only for when, as you've found, the app is running.
If the app is LAUNCHED due to a local (or for that matter remote) notification, the goods from the notification are passed into the application:didFinishLaunchingWithOptions: method, and that's where you catch that.

UILocalNotification

I have UILocalNotification the has two buttons a cancel and view button when the application is in the background and the alert come up I click the view button and it opens on application but I have a method call that is ment to run if the launch option has a UILocalNotification object that is not working
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
//notifcation key
UILocalNotification *notifcation = [launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey];
if (notifcation) {
NSLog(#"working");
}
}
when your application is in background then didFinishLaunchingWithOptions will not call in that case use -
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
here userInfo is same as UIApplicationLaunchOptionsLocalNotificationKey.
Thanks for Help I have worked it out I thought that the launch options were passed to the application even if the app was in the background but that is not the case if application is in background and a local notification comes in the application delegates has the didReceiveLocalNotification method called I thought that was call when application was in foreground and when in the background application did launch with options was called but the options only has the local notification key when the app is closed (not in background) and the notification is call to start up the application.
so if ay one else has te same issue make sure you use
-(void) application:(UIApplication *)application
didReceiveLocalNotification:(UILocalNotification *)notification
method when you click your action button on the local notification alert to run and actions when you open your application from that notification alert button.

How do I detect an iPad's interfaceRotation at the start?

I'm having problems getting my iPad app to detect its interfaceOrientation in the first UIViewController I initialize (in code). In fact, if I trace for application.statusBarOrientation, that too returns 1 (UIInterfaceOrientationPortrait) even if I launched in landscape.
If I trace self.interfaceOrientation in my first UIViewController, it remains 1 until it gets to viewWillDisappear... Which is unfortunately too late!
Here's some code (even though there's not much to see):
In my appDelegate I have this:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// show loading screen first
[window addSubview:loadingScreenViewController.view];
[window makeKeyAndVisible];
NSLog(#"applicationDidBecomeActive:statusBarOrientation = %d", application.statusBarOrientation);
return YES;
}
which traces 1 (portrait), even though I clearly see the status bar is landscape... and in the first view controller I have this:
- (void)viewDidLoad
{
[super viewDidLoad];
NSLog(#"self.interfaceOrientation = %d", self.interfaceOrientation);
}
which also traces 1, even in landscape mode.
Any ideas? Stumped here!
Thanks :)
:-Joe
Here is the answer... Somewhat: (from Apple Dev Forums): ....
"The app is always loaded as if the device is portrait, and then if the device is really landscape the app is told that the device has rotated. This is done so that nibs and code only need to create their UI in one orientation. Otherwise it might be necessary to have two UI layouts for each nib." .... it's not the answer I'd have liked, but that's how iOS works unfortunately!
What does the app delegate report in applicationDidFinishLaunching? Because if it reports the correct value, you can always access the delegate to check the orientation.