Making a server call when the home button is pressed - objective-c

I'm developing an iOS application and I need to make a server call when the home button is pressed... I've been reading about and I know there are a couple of methods that are called when the user press that button but I'm really not sure if the app has enought time to make that call...
Here is where I was thinking in adding the server call...
- (void)applicationDidEnterBackground:(UIApplication *)application{
}
Do I have enough time to make the call? Is just a message, I don't need reply or anything like that...
If it's not possible what do you think is the best option to make this work? Any idea?
Thanks!

Whatever you put in that method will be executed as soon as the application goes to the background. Why don't you test it? Don't you have a way of knowing that the request was sent to the server?
Since there's nothing you need to do upon server's response, including the code for making the request in the app delegate's method you mentioned should do what you're after.

Related

macOS custom urlscheme, do not make app frontmost app?

I have a custom url scheme working well, but there is one small behaviour I am hoping to change... Currently the inbound url is captured and processed in the following code (so, all good here):
- (void) handleURLEvent:(NSAppleEventDescriptor*)event withReplyEvent:(NSAppleEventDescriptor*)replyEvent
{
NSString* url = [[event paramDescriptorForKeyword:keyDirectObject] stringValue];
//I parse and deal with the url here//
}
Is it possible to avoid my app becoming the frontmost application? Whenever the url is received, my app is open already, as I initiated the communication from this app, and are merely listening for the reply. However, sometimes the call is made via an NSTimer so my app may or may not be the frontmost app. I don't want to disrupt the workflow if I am currently say browsing pages in Safari by having my app take the focus from Safari. I hope this makes sense.
Is this possible?
For anyone who's interested i instead set up a group container. I save my payload to the group defaults and then send an NSDistributedNotification from my helper app. The main app gets the notification, and reads the payload data from group defaults.
Such a workaround... but its necessary as both the main and helper apps are sandboxed.
I also discovered that KVO on group defaults doesn't work in the sandbox, and neither does sending a userDictionary in the NSDistributedNOtification.
:(
I have had to write sooo much extra code to make my app play nicely in the sandbox.

RemoteNotification TOKEN not received if push notifications are not allowed

Really confused with this one!
I have an Adobe AIR iOS app and I am using the RemoteNotifier to subscribe for PushNotifications. More details http://www.adobe.com/devnet/air/articles/ios-push-notifications.html
I have the following listeners.
remoteNotifier.addEventListener(RemoteNotificationEvent.TOKEN, onToken);
After I reset my iPod to factory settings and reinstall my app (using the adhoc distribution), I click "Don't allow" for the pushnotifications when I get prompted.
I find that I then don't receive any RemoteNotificationEvent.TOKEN, so my app basically hangs waiting for that.
If I go to my Settings, Notifications and have a play with
Toggle Alert Style from the None to Alerts AND
Then back to None again (as it was)
I find that I can go back and launch my app with not problems. It's just that on the very first time user flow, I don't seem to get any TOKEN back.
Any ideas? Has anybody else seen this behaviour?
Or knows how I should handle this?
Thanks!
Hopefully I am correct, someone please correct me if I'm wrong.
From what I am able to find out, you won't get a RemoteNotificationEvent.TOKEN when the user clicks on "Don't Allow".
For some reasons that I am still unclear about, how after you play with the Settings and relaunch the app, you get the RemoteNotificationEvent.TOKEN, regardless of the notification settings you have set. But maybe that's the way it's meant to be?
So, really I have to change the flow such that I don't wait on that RemoteNotificationEvent.TOKEN before loading my first screen. If the TOKEN comes back, it comes back, otherwise if it doesn't, it's not a big deal as that means the user clicked on "Don't allow" and we don't need to send PUSH notifications anyway.
Everytime I relaunch my app, I do make the call to subscribe and get the TOKEN though, such that if the user was to enable the Notifications in the phone's Settings, I do have a TOKEN ID to send the push notification too.
The only problem I see with this though, is that should the user change the Notification to ON via the phone Settings and doesn't relaunch the app (to retrieve the TOKEN) then the device doesn't get push notifications.
Not sure how to handle this?

canceling API request while application entering into background

While launching the application in my iPhone, I need to download the data from the server. While downloading data(in middle of process) if I press home button to enter the application into background.this time I need to stop the API request which is already executing. currently applicationDidEnterBackground method is calling with delay(after downloading data). In the mean time application is crashing. how can we cancel the URL connection while application is entering to background.
Please help me out.
Thanks in advance.
One way to do it is to set some kind of flag in your app controller that you can periodically check to see if the request is cancelled.
Or, even better, the NSThread object provides a mechanism to indicate that it should be cancelled. If you go into the background, set your detached thread to cancel (via [NSThread cancel]) and then in whatever API callsback you have, you should periodically check to see if [NSThread isCancelled] is returning YES.
Here's a link to Apple's documentation on [NSThread cancel].
http://developer.apple.com/library/ios/documentation/Cocoa/Reference/Foundation/Classes/NSThread_Class/Reference/Reference.html#//apple_ref/occ/instm/NSThread/cancel
hope this helps!

CLLocationManager "turn on location services..." alertview customization ios

I know that I can't change the title or the buttons for this alertview, but i've seen numerous apps that changed the message of the alert view
Something like this
Also, I have the Bump API in my app so everytime the popup shows, it says "Bump uses your location to help determine whom you are bumping." and I don't want that displayed when they first use my app.
Does anybody know how I can change the message or change bump's message?
Thanks
To change the message of the alert, use the "purpose" property of CLLocationManager. Check the docs: http://developer.apple.com/library/IOs/#documentation/CoreLocation/Reference/CLLocationManager_Class/CLLocationManager/CLLocationManager.html
I'm not sure how the Bump API works, but if you are just importing all the classes you need, you should be able to edit the location services message. Otherwise, one option would be to request location access before calling the Bump API's to get permission for your app. Once Bump checks, it will already have permission and skip presenting its own.

how to delay application quit time in iphone sdk?

I want to register my app for push notification when my application terminates so i think if i delay my app quitting time it could be possible.Does someone knows how to delay application quitting time? I think this method
[self performSelector:(SEL)aSelector onThread:(NSThread *)thr withObject:(id)arg waitUntilDone:(BOOL)wait];
will do my job but i don't know how to use this method if someone knows please tell.I need to send some data to a server along with registering for Push Notification when my app quits.
I can't imagine why you would want to do this. If it were even possible it would be extremely annoying for a user to tap the home button and the app to take x amount of time to shut down. This time 'x' being dependent on the server connection creates even more user headache.
Apple have the home button exit apps immediately for a reason.
If you want to register the Push Notifications like you suggest, do it while the app is running. If your worrying that they won't be properly set if the user exits prematurely... don't.
As users, we all know there are sometimes consequences of exiting a program without giving it time to save your settings.
For push notification it is better to register when the app first starts and then send the push token to your server in the background. However, if you have a good reason why you need to do the registration just as the app terminates, I believe you can do this if you are using iOS 4. iOS 4 has a new feature called "task finishing" that allows an app to stay running for a few minutes after the user closes it so that it may finish up any tasks it was in the middle of (such as saving data).