Multitasking aware applications in iOS 4 and Custom URL Schemes - iphone

I'm trying to implement OAuth securely as detailed here: http://fireeagle.yahoo.net/developer/documentation/oauth_best_practice#custom-url-osx. I seem to have hit a stumbling block, as I am unable to figure out how to handle a url which launches my application when in the background.
I have registered my application to handle oauthtest. I have confirmed that oauthtest:// and oauthtest://callbacktest both launch my application and operate as intended when my application is not running in the background.
I am implementing
application:(UIApplication *) didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
which is successfully called when my application starts up cold. I can easily get the url passed to my application.
However, if my application is already running in the background, neither
application:(UIApplication *) didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
nor
application:(UIApplication *) handleOpenURL:(NSURL *)url
is called and I have no way of getting the parameters passed to my application as part of the URL.
Does anyone know how to get the parameters passed to a backgrounded application by a custom url scheme?
I am aware that I could work around this issue by disabling multitasking, but I would rather not do that for obvious reasons. Thanks in advance.

Here's some sample code that seemed to work for me, tested in iOS4:
- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url {
NSLog(#"handleOpenURL - %#", [url absoluteURL]);
return YES;
}
- (void)applicationDidFinishLaunching:(UIApplication *)application {
NSLog(#"applicationDidFinishLaunching");
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
NSLog(#"didFinishLaunchingWithOptions - %#", [launchOptions objectForKey:#"UIApplicationLaunchOptionsURLKey"]);
return NO;
}
If I launch the application for the first time, didFinishLaunching: handles the URL. If I then put the app in the background, go back to Safari, and tap a link that brings the app back into the foreground, then handleOpenURL: takes care of the URL. Good luck!

Related

is there a way to differentiate the IOS application launched using custom url and on clicking the application icon?

I am developing a ios application. When i launch the application it should open the root view. If i am launching the application using custom url from a website, it should initialize with another nib file. Can someone help me with the problem.
Once again thank you for the answers.
In your app delegate you have to implement:
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
This will be called if your app is opened via URL scheme. In there you can react appropriately.
You can find the documentation here.
You have to implement open url in app delegate
- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url
{
if (!url)
{
return NO;
}
NSString *URLString = [[url host]stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
return YES;
}
Here is a nice link you can follow link`

detect my apps been killed

I have built a simple apps that connect to a sqlite database, and the app uses storyboards for UI. On first time, it will throw up a login view. If my authenticate was successful, I change my status in the sqlite database to 1. By default, it is 0. After login, I can use the app.
For example if I kill the app, how can I know my app has been killed? I need to change the status become default again. so whenever I click on the app again, I have to sign in again.
Any idea how to do this? thanks.
Your application delegate implements several methods which handle state changes in your application. From the UIApplication.h header.
- (void)applicationDidFinishLaunching:(UIApplication *)application;
- (BOOL)application:(UIApplication *)application willFinishLaunchingWithOptions:(NSDictionary *)launchOptions NS_AVAILABLE_IOS(6_0);
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions NS_AVAILABLE_IOS(3_0);
- (void)applicationDidBecomeActive:(UIApplication *)application;
- (void)applicationWillResignActive:(UIApplication *)application;
- (void)applicationWillTerminate:(UIApplication *)application;
You can implement whichever methods are relevant, in this case the applicationWillTerminate method to change state. Alternatively, always throw up a login view in applicationDidBecomeActive. (This doesn't, however solve the issue updating the database. If the database is local, there's no problem, simply change the database before resigning or terminating. The challenge here would be if your database lives on a server. Although it might bot be an issue, I could see connection timeouts affecting the integrity of the remote values.)
These methods are added to your project automatically in your application delegate, which can be found in the AppDelegate.h & AppDelegate.m files.
For a complete explanation, have a look at the UIApplication Delegate protocol reference.
You can perform close down actions in,
- (void)applicationWillTerminate:(UIApplication *)application
{
// Log out?
}
You can also do the same if your app isn't killed but just gets suspended, such as during an incoming call,
- (void)applicationWillResignActive:(UIApplication *)application
{
// Log out?
}
or,
- (void)applicationDidEnterBackground:(UIApplication *)application
{
// Log out?
}
Alternatively, on resume,
- (void)applicationDidBecomeActive:(UIApplication *)application
{
// Log in?
}
or,
- (void)applicationWillEnterForeground:(UIApplication *)application
{
// Log in?
}
You can't detect if you app was killed, since the process of you app gets killed by the system and no code is called to inform your app that it has been killed.
What you want is to set te some kins of boolean in :
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
Since this is called if you restart the app after it has been killed.
For the other states seen the answer by Moshe
EDIT
use the delegate methods
such as:
applicationWillResignActive

Play sound when each time app is launched

Is there a convenient way to play a sound when the app is launched?
I want it to be played only when the app is launched, not when the app entered background and went out of a background state.
Any ideas where I could put the code for this?
Add your sound code to
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
That method is only called once, when the app first opens.
In your app delegate in the function...
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

UIApplication's openURL Crashes My App

I'm using UIApplication's openURL: method to open a website in Safari. When the user came back to the app (fast switching), it relaunched. It means that my app was quit instead of going to the background.
Does anyone have the same issue? Any ideas? Thank you in advance.
It may be possible that you need to retain the URL object. That was once the issue for me
make sure that you are using
- (BOOL)application:(UIApplication *)application
didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
in your appdelegate rather than
- (void)applicationDidFinishLaunching:(UIApplication *)application

UIApplicationDelegate becomeactive with options

I implemented association with files in my app that works pretty great but I still have ine problem that i cannot fugire out.
im my
(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
I have this code. it doesn't work when the app become active from background.
NSURL *url = (NSURL *)[launchOptions valueForKey:UIApplicationLaunchOptionsURLKey];
any suggestion how can i make this work ?
Thanks.
This method:
(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
is only called when you app just launched. Coming back from background is not launching your app.
You may find these two methods useful.
- (void)applicationWillEnterForeground:(UIApplication *)application {
- (void)applicationDidBecomeActive:(UIApplication *)application {
You need to also hook with applicationDidBecomeActive: when the app became active from multitasking.