Doing something even if application is closed in iphone - iphone

I am developing iphone application where i want to develop feature of taking backup of files inside the application on to the server for every hour even if the application is completely closed.
I have tried to use NSLocalNotification but it is not calling method
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
in appdelegate. The notifier showing alert box with the buttons cancel and view , If I click in view it opens the application but not calling the method. Even if user say cancel then also it should call this method.
So it is not calling web service to take backup of files. Can any one please direct me to link which does the same.
Thanking You,
Rohit Jankar

You really can't do what you want. Since iOS does not allow application to run in the background unless it's is a VOIP client, AudioPlayer or track the user location.
The problem with the solution with UILocalNotification is that iOS handles the notification. When the user clicks cancel your app does not get informed about this. You can only handle the view button clicks.
The-(void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification is only called if you app is run or is restored from background running.
If you app gets start by the system when you user click view on the Local notification you will need to check if there is a notification in the - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions method:
UILocalNotification *notification = [launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey];
if (notification) {
//handle the notification.
}

I am developing iphone application where i want to develop feature of
taking backup of files inside the application on to the server for
every hour even if the application is completely closed.
If you're talking about backing up files to your own server every few hours, there's really no way to do that. On the other hand, there should also be no need to do it when the app isn't running -- if the app isn't running, it can't modify data in its files and there shouldn't be any need to back up. Instead, just make sure that you back up your data periodically when the app is running.
If you just want to make sure that the app's data is backed up somewhere, consider using iCloud. Once you've set up your app to use iCloud, your data will be backed up whenever you update it.

Related

iPhone how to access the notification center programmatically

As per the Apple guide:
If the application icon is tapped on a device running iOS, the application calls the same method, but furnishes no information about the notification . If the application icon is clicked on a computer running
https://developer.apple.com/library/ios/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/Introduction.html#//apple_ref/doc/uid/TP40008194
As far as I have known, it seems not be able to detect the notification when the application icon is tapped.
So I tried to retrieve the notification center programmatically but it also seems to be impossible.
Is it impossible to retrieve the notification center programmatically?
What I want to do is detecting whether the notifications received or not even when the application is in the background.
Is it impossible to retrieve the notification center programmatically?
No, it's not possible with any public API.
Your app is agnostic about the current state of the Notification Center, as they are two decoupled entities.
Anyway, as noted by AdamG, in iOS 7 you can implement
application:didReceiveRemoteNotification:fetchCompletionHandler:
which, according to the documentation, is called regardless of the state of your app (so even when it's not running or in background).
In order to use it, you have to support the remote-notification background mode. Here's how:
In Xcode 5 and later, you declare the background modes your app supports from the Capabilities tab of your project settings. Enabling the Background Modes option adds the UIBackgroundModes key to your app’s Info.plist file. Selecting one or more checkboxes adds the corresponding background mode values to that key.
Now, while you still cannot programmatically access to the Notification Center, you can track the notifications as they come.
An mocked implementation would go as follows:
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
// Hey we got a notification!
// Now we have 30 seconds to do whatever we like...
// ...and then we have to call the completion handler
completionHandler(UIBackgroundFetchResultNoData);
}
Under iOS 7 there is a way to do this.
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult result))handler
And then do whatever you need to do in the app to handle the notification and make sure to register that it was received by the phone.
When a push notification arrives, the system displays the notification to the user and launches the app in the background (if needed) so that it can call this method. Use this method to download any data related to the push notification. When your method is done, call the block in the handler parameter.
Found here.

Proper appDelegate method for Flurry startsession?

Flurry docs recommend placing the startSession call in applicationDidFinishLaunching:.
Two problems with this...
- (void)applicationDidFinishLaunching:(UIApplication *)application
{
[FlurryAnalytics startSession:#"AWESOMEAPIKEY"];
// ...
}
1) Isn't application:didFinishLaunchingWithOptions: the new approved launch point?
2) This is only called once on launch, but don't we want session info every time a user opens or switches back to the app? Or does Flurry handle all that on their own by listening to some event or NSNotification?
Wouldn't a better place to put the startSession call be in applicationDidBecomeActive: or applicationWillEnterForeground:, like so?
- (void)applicationDidEnterBackground:(UIApplication *)application
{
// ... Flurry AppCircle setup
[FlurryAnalytics startSession:#"AWESOMEAPIKEY"];
// ... your setup
}
for your case 1)
correct place to put [FlurryAnalytics startSession:#"SOMESESSIONKEY"]; is
application:didFinishLaunchingWithOptions:
you can place it there without worries. I have done this by myself and the app is working awesome at appstore and providing the stats perfectly.
for case 2), your secession will be automatically resumed when app returns to foreground so you dont have to do any special handling here.
I was real curious about this too. I looked at my inherited code for my app and didn't see any flurry activity in didbecomeactive, foreground, etc. I only saw the startsession in didfinishlaunchingwithoptions. I saw the below on the flurry site re: startsession, but i still don't get how it works, just behind the scenes stuff the flurry library does? #samfisher, can you elaborate?
"This method serves as the entry point to Flurry Analytics collection. It must be called in the scope of applicationDidFinishLaunching. The session will continue for the period the app is in the foreground until your app is backgrounded for the time specified in setSessionContinueSeconds:. If the app is resumed in that period the session will continue, otherwise a new session will begin."
FlurryApi.h shows the default as 10 for setSessionContinueSeconds so I guess Flurry handles it, I'm just looking for more confirmation.
http://support.flurry.com/sdkdocs/iOS/interface_flurry_analytics.html#a78b0b92085b38875d51f1ca0d699849a

How to call a method upon iPhone app re-opening

I have a method that I want to call not the first time the app launches from being not open at all, but whenever the app opens at all, whenever. So if the app is open, then the user closes it, but it is still running through multitasking, I want to the method to run when they resume the app as well.
applicationWillEnterForeground:
In iOS 4.0 and later, this method is called as part of the transition from the background to the active state. Specifically, it is not called when the application is launched for the first time -- which is what you are looking for.
- (void) applicationWillEnterForeground:(UIApplication *)application {
}

iPhone Application Themes

I tried using a multi-value settings bundle to change the view. I would do the if statements in the applicationdidfinishloading in the application delegate. Apparently the method isn't called every time the app is loaded, and it would not work correctly.
If anyone has done this, or has any suggestions, links to tutorials. I would really appreciate it. I'm just trying to load views (nibs) based on user preference.
I think you can put your code in
- (void)applicationDidBecomeActive:(UIApplication *)application
or
- (void)applicationWillEnterForeground:(UIApplication *)application
methods also because from iOS 4.0 due to multitasking your app is just in the background state so it wont call applicationdidfinishloading method when the user presses the icon of your app again.

How to know the app is invoked from background process or not

Thanks in advance.
I used push notification service in my application.If the app is running in background i am able get the alert view, but the app is in active state it is not displaying alert. Is there any way to display alert.
Actually to display alert i am creating an alert in
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo method.
But there is a problam with this alert i.e., of the app is running in background we will get remote alert at the same time we will get this alert also.
So is there any way to find if the app is in active state or in background while receiving remote notification using didReceiveRemoteNotification method.
A simple solution I can think of is to make use of applicationDidBecomeActive: and applicationDidEnterBackground:. Declare a property in your app delegate and set it properly in those two methods. Then you can do anything you want based on this property, like [[[UIApplication sharedApplication] delegate] isInBackground].