How to communicate between two apps
I have two applications (one is actually a Preference Pane) and I need them to communicate; to be more precise, I need the preference pane to ask the application for information if it is running. So I need:
-(BOOL) isApplicationRunning:开发者_Go百科(NSString*)pathToApplication;
-(NSDictionary*) returnSomeDInfoForMe:(NSString*)pathToApplication;
Any idea on how this can be done?
TIA, Oren
Give the NSDistributedNotificationCenter a try - using it will allow to send dictionaries between two apps
You cannot access one apps data from another. You could possibly communicate with a server while running one in background and then use that as a middleman but it's going to be a messy job none-the-less!
iPhone apps with the same bundle identifier have access to the sMe keychain. So you could write data from app1 into the keychain and then access it from app2. You'd need to do smart updating of the keychain to keep flags of when the other app is open by using the right uiapplication delegate methods but it should be possible.
Note that keychain is a key-value storage and not suitable for larger complex data. For that you should use server-client structure.
Reference to keychain docs:
http://developer.apple.com/library/mac/documentation/Security/Conceptual/keychainServConcepts/02concepts/concepts.html
If it's just about finding out if a certain app is running:
: < 10.6 NSWorkspace -> launchedApplications
: > 10.6 NSRunningApplication -> runningApplication
On a Mac of course.
Using IOS, I'd probably do that by hand and utilizing the NSNotificationCenter. I.e. write a status-file somewhere noting that the app is running when the pref-pane wants to check if the app is running. If the app doesn't respong to the notification, the status-file doesn't get written, so you could assume it's not running.
You should use the custom URL scheme approach for this. Here's a tutorial.
Short version: Your applications can call each other at custom URLs. The URL can carry query-string arguments just like any other URL, so you can pass yourself status strings or short commands, etc.
This seems like a good read for this - http://developer.apple.com/library/mac/#documentation/UserExperience/Conceptual/PreferencePanes/Tasks/Communication.html
精彩评论