how to call an iphone application from different application - iphone

is it any possibilities to invoke/call iphone application from different application, if so means, whats the snippet for that..

This is a specific example, but, if you setup a protocol handler, when a url is loaded by Safari that it can't handle (yourappProtocol://) it will fire off your application to handle it.
You need to add runtime config to your Info.plist.
And then implement the delegate:
- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url {
[viewController handleURL:url];
return YES;
}
Read more info here: http://www.mobileorchard.com/apple-approved-iphone-inter-process-communication/
And here: http://blog.innerfence.com/2009/01/05/2-way-app-integration-on-the-iphone-how-it-works/

Note that the protocol handler for your application must be unique - if more than one application installed on an iPhone respond to the same handler, there is no way of knowing which app will launch.
i.e.
tweet://... - bad.
mySuperTwitterApp://... - good.

Related

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

Differentiate between application launch and application awake

I need to be able to differentiate between application didFinishLaunching and application entering background and being loaded back into the foreground. I noticed that if I register for the UIApplicationDidBecomeActiveNotification, it is called in both instances. How do I distinguish between the two?
Use the didFinishLaunchingWithOptions vs applicationWillEnterForeground to distinguish if app is starting cold or returning from background.
I tried listening to the UINotification's in one of my apps, but the notification was being delivered too late, so I resorted to listening to these method calls directly.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// only call when app is freshly launched
}
- (void)applicationWillEnterForeground:(UIApplication *)application
{
// Only called when app is returning from background
}
I need to be able to differentiate between application
didFinishLaunching
UIApplicationDidFinishLaunchingNotification
application entering background
UIApplicationDidEnterBackgroundNotification
being loaded back into the foreground
UIApplicationWillEnterForegroundNotification

Lauching App with URL (via UIApplicationDelegate's handleOpenURL) working under iOS 4, but not under iOS 3.2

I have implemented UIApplicationDelegate's
application:didFinishLaunchingWithOptions:
and
application:handleOpenURL:
according to specification, i.e.,
application:didFinishLaunchingWithOptions:
returns YES
and
application:handleOpenURL: opens the URL.
The code works under iOS 4 (in both cases, i.e., when the app is launched and when it becomes active from suspended state). However, the code does not work under iOS 3.2.
I give an answer to my own question. Finding out the solution took me a while and was quite frustrating. If you do an internet search you find some partial answers, but it still took me a while to work out the following solution and I do hope it adds some clarity.
So first, the recommended behavior of your app appears to be the following (see Opening Supported File Types in iOS Ref Lib):
Do not implement applicationDidFinishLaunching: (see the note at UIApplicationDelegate).
Implement application:didFinishLaunchingWithOptions: and check the URL, return YES if you can open it, otherwise NO, but do not open it.
Implement application:handleOpenURL: and open the URL, return YES if successful, otherwise NO.
In iOS 4, passing an URL to an app results in one of the following two behaviors:
If the app is launched then application:didFinishLaunchingWithOptions: is called and application:handleOpenURL: is called if and application:didFinishLaunchingWithOptions: returned YES.
If the app is becoming active from suspended state then application:didFinishLaunchingWithOptions: is not called but application:handleOpenURL: is called.
However, in iOS 3.2 it appears as if application:handleOpenURL: is never called! A hint that the behavior is different under iOS 3.2 can be found in Handling URL Requests. There you find that application:handleOpenURL: is called if application:didFinishLaunchingWithOptions: is not implemented, but applicationDidFinishLaunching: is implemented. But application:handleOpenURL: is not called if application:didFinishLaunchingWithOptions: is implemented.
Hence, one solution to make the code work under 3.2 and 4.0 is:
Open the URL in application:didFinishLaunchingWithOptions:, but then return NO to prevent that application:handleOpenURL: is called.
Open the URL in application:handleOpenURL:, in case you are under 4.0 and the app was in suspended state.
I found this solution in another post, but I was confused, because it contradicted the recommendation in iOS Ref Lib documentation (namely that we should return YES in application:didFinishLaunchingWithOptions:). (At that point I did not realize that the documentation contradicts it self).
I believe that the current iOS 4.0 behavior will be the future behavior I prefer the following solution:
Do not implement applicationDidFinishLaunching:.
Implement application:didFinishLaunchingWithOptions: and check the URL, return YES if you can open it, otherwise NO, but do not open it. If we are on 3.2, open the URL.
Implement application:handleOpenURL: and open the URL, return YES if successful, otherwise NO.
So in summary, I implement the iOS 4 behavior and added the following line to application:didFinishLaunchingWithOptions:
if([[[UIDevice currentDevice] systemVersion] hasPrefix:#"3.2"]) {
[self application:application handleOpenURL:url];
}
which make the code work under 3.2.
application:handleOpenURL: is now DEPRECATED.
As of iOS 4.2, you can use this for opening URLs:
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url
sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
Documentation:
https://developer.apple.com/library/ios/#DOCUMENTATION/UIKit/Reference/UIApplicationDelegate_Protocol/Reference/Reference.html
I started writing application which used Dropbox api. To understand concept, I ran a sample application using my Key/secret mentioned at dropbox/developer documentation.
Once sample app started working, I used same key/secret values for my application.
For sample app, implementation of handleOpenURL (or openURL on iOS 4.2) gets executed as expected. For some odd reason, it wasn't the case for my app. My app entered background in order to show login screen and authentication page of dropbox. After successful login and authentication, my app never entered foreground. It was true for both platform Simulator and device (iPad)
I tried almost everything listed on internet including this post. Thanks. There was NO success, though.
At last, it STARTED working for my application when I did following:
On simulator, select "iOS Simulator --> Reset Content and Settings", and reset.
On device, I deleted sample application related executable and which in turn delete cache associated to it.
Add the following to the end of application:DidFinishLaunchingWithOptions:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
...
NSURL *url = (NSURL *)[launchOptions valueForKey:UIApplicationLaunchOptionsURLKey];
if (url != nil && [url isFileURL]) {
return YES;
} else return NO;
} // End of application:didFinishLaunchingWithOptions:
// New method starts
-(BOOL) application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
{
mvc = [nc.viewControllers objectAtIndex:0];
if (url != nil && [url isFileURL]) {
[mvc handleOpenURL:url];
}
return YES;
}
where mvc is my main ViewController, and nc my navigation controller.
Then in the MainViewController, do something like this:
- (void)handleOpenURL:(NSURL *)url {
[self.navigationController popToRootViewControllerAnimated:YES];
// Next bit not relevant just left in as part of the example
NSData *jsonData = [NSData dataWithContentsOfURL:url];
NSError *error;
NSDictionary *dictionary = [[NSJSONSerialization JSONObjectWithData:jsonData options:kNilOptions error:&error] objectAtIndex:0];
[self managedObjectFromStructure:dictionary withManagedObjectContext:self.context];
...
}
after declaring handleOpenURL in the .h of course.
Thanks goes to Christian for putting in the effort for this.

Multitasking aware applications in iOS 4 and Custom URL Schemes

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!

How do I tell if my iPhone app is running when a Push Notification is received?

I am sending Push Notifications to my iPhone app, and I'd like a different set of instructions to execute depending on whether the app is already launched or not. I'm new to iPhone development, and while I suspect UIApplication or my project's AppDelegate class has the solution, I haven't found a good answer. Is there an easy way to check for this?
Here's the more appropriate way of handling active/inactive state of the app.
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
// check for the app state
UIApplicationState state = [application applicationState];
if (state == UIApplicationStateActive) {
//the app is in the foreground, so here you do your stuff since the OS does not do it for you
//navigate the "aps" dictionary looking for "loc-args" and "loc-key", for example, or your personal payload)
}
application.applicationIconBadgeNumber = 0;
}
didReceiveRemoteNotification: is called when the app is running, yes, but when it is suspended, the iOS takes care of putting up the badge, etc. If the app is in the foreground, the OS does nothing, and just calls your didReceiveRemoteNotification:.
Depending upon what you mean by "launched", you are either looking for:
Kevin's answer above (differentiates between launched or not launched)
or this (differentiates between suspended or active, but already launched):
Use a flag that is set true when the application becomes active, and false when the application is not active.
Flag (in header file [.h]):
BOOL applicationIsActive;
Code (in implementation file [.m]):
- (void)applicationDidBecomeActive:(UIApplication *)application {
applicationIsActive = YES;
}
- (void)applicationWillResignActive:(UIApplication *)application {
applicationIsActive = NO;
}
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
if (applicationIsActive) {
// Handle notification in app active state here
}
else {
// Handle notification in app suspended state here
}
This works because when the application is suspended, the OS calls "applicationDidReceiveRemoteNotification" before it calls "applicationDidBecomeActive" during the "wake-up" process.
The "complete" answer is actually Kevin's answer plus this answer.
Hope this helps.
The UIApplication delegate has the method
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
which you need to implement. This receives the notification when the app is running.
If your app is not currently running and a notification is received then your app can be launched with
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
with the notification details held in the launchOptions dictionary. if the dictionary is nil then the user tapped the application icon as normal.
If you are going to check applicationState on iOS less than 4, you'll need to check that applicationState is supported:
if ([application respondsToSelector:#selector(applicationState)] ){
// Safe to check applicationState
UIApplicationState state = [application applicationState];
}
The Apple documentation for push notifications explains this:
However, there are two situations where applicationDidFinishLaunching: is not a suitable implementation site:
The application is running when the notification arrives.
The notification payload contains custom data that the application can use.
In the first case, where the application is running when iPhone OS receives a remote notification, you should implement the application:didReceiveRemoteNotification: method of UIApplicationDelegate if you want to download the data immediately. After downloading, be sure to remove the badge from the application icon. (If your application frequently checks with its provider for new data, implementing this method might not be necessary.)
This means that if your application:didReceiveRemoteNotification: delegate method is called, your app is running.