How to get filename when using Launch Services - cocoa

I register my app with:
NSString *self_id=[[NSBundle mainBundle] bundleIdentifier];
CFStringRef cfString;
cfString = UTTypeCreatePreferredIdentifierForTag(
kUTTagClassFilenameExtension,
CFSTR("fdp"),
kUTTypeData);
OSStatus a = LSSetDefaultRoleHandlerForContentType((CFStringRef)cfString,kLSRolesViewer,(CFStringRef)self_id);
But on the other side, how can i get the filename the finder send to my app?
what the mechanism should i set for getting the filename?
Is there any document about this problem?

If your application is document-based, the document controller will create a document object for each opened file automatically. You don't need to do anything at run time to handle the files; just implement your document class and declare the right things in your Info.plist, as described in Document-Based Applications Overview.
If your application is not document-based, you need to create an object to be the delegate of the application object. In that object, respond to the application:openFiles: message.

Related

Trouble populating an input with an App Properties value

Working on my first Xamarin Forms app. I put this in my App's OnStart() method:
Application.Current.Properties["Email"] = "gern#blanston.org";
That should set the value as soon as the Application starts, right?
Then I created a Login content page with an EmailAddress entry. In the page initialization, I've got this:
if (Application.Current.Properties.ContainsKey("Email"))
{
EmailAddress.Text = Application.Current.Properties["Email"] as string;
}
When I run the app, the EmailAddress Entry element is not populated.
What am I doing wrong?
OnStart command is called after App constructor and you probably creating login page in App constructor and checking in login page constructor but OnStart is not called yet. Set it before you created login/main page
Application.Current.Properties["Email"] = "gern#blanston.org";
var navPage = new MainPage();
It should work.
Here is the Documentation.
You can try to use SavePropertiesAsync
The Properties dictionary is saved to the device automatically. Data added to the dictionary will be available when the application returns from the background or even after it is restarted.
Xamarin.Forms 1.4 introduced an additional method on the Application class - SavePropertiesAsync() - which can be called to proactively persist the Properties dictionary. This is to allow you to save properties after important updates rather than risk them not getting serialized out due to a crash or being killed by the OS.

iOS8: Sharing common files between apps

Two iOS: AppA and AppB
Both Apps are created by me and I would like to share one single file between both of the apps.
i.e. AppA launches on deviceA and the User saves data on fileA. Later the User launches AppB on the same (deviceA) and also saves data on fileA. Both apps are saving data on the same file.
I'm aware that I can use NSUserDefaults and share Keychain between apps, but that's not what I'm looking for.
I read up on document extensions provider and app groups, but I'm confused if I can use these for this scenario? Or is there any other way to accomplish this?
You can do it using Application Group Container Directory:
NSFileManager *fm = [NSFileManager defaultManager];
NSString *appGroupName = #"Z123456789.com.example.app-group"; /* For example */
NSURL *groupContainerURL = [fm containerURLForSecurityApplicationGroupIdentifier:appGroupName];
NSError* theError = nil;
if (![fm createDirectoryAtURL: groupContainerURL withIntermediateDirectories:YES attributes:nil error:&theError]) {
// Handle the error.
}
You could just upload the files after saving to your server and make both apps request updates for the file whenever they are launched.
Hope that helps :)

NSWorkspace throws error when launch the mail application

I tried the below code, it is working fine for me. Also am able to launch the mail application as well.
//Note the below path is coming from bundle identifier of Mail APP
NSString *path=#"/Applications/Mail.app"
NSURL *mailURL = [NSURL URLWithString:path];
NSError *err=nil;
[[NSWorkspace sharedWorkspace] launchApplicationAtURL:mailURL
options:NSWorkspaceLaunchDefault
configuration: someData
error:&err];
But it throws me the below error message on console, What it means actually. How to resolve the below issue.
CFURLCopyResourcePropertyForKey failed because it was passed this URL which has no scheme: /Applications/Mail.app
The error is thrown because you are not creating a valid URL. The URL needs a scheme, in your case it is file: so the correct URL is file:///Applications/Mail. You need to create a file URL which works as follows:
NSString *path=#"/Applications/Mail.app";
NSURL *mailURL = [NSURL fileURLWithPath:path];
Note that your code breaks if the user ha moved Mail.app to another location. Also note that if the user doesn't use Apple's Mail app, it won't work well for the user either.
One possibility of doing it a more correct way is given here: How to launch New Message window in Mail.app from my application
Another option is to get the URL for Mail.app in a more fleixble way covering for users that have moved Mail.app. The idea is to use the bundle identifier and then ask NSWorkspace to launch this application by using
- (BOOL)launchAppWithBundleIdentifier:(NSString *)bundleIdentifier
options:(NSWorkspaceLaunchOptions)options
additionalEventParamDescriptor:(NSAppleEventDescriptor *)descriptor
launchIdentifier:(NSNumber **)identifier
(see also in detail here http://theocacao.com/document.page/183).

Opening a url on launch

What method must I implement in my cocoa application’s delegate so that on launch, it’ll open a url? (http/https, in this case) I’ve already implemented the url schemes, I just need to know how I can get my application to open on a url notification.
Update: I’m sorry, I wasn’t very clear. My application IS a browser that support https/http urls, but can only open them when it’s already running. What can I do to implement support for open urls in my app on launch?
When an application finishes launching on OS X, NSApp (the global NSApplication instance for the program) sends its delegate the applicationDidFinishLaunching: message (via the notification system). You can implement that method in your delegate to handle the notification and open a browser window in response, using NSWorkspace. Something like the following would work:
// Your NSApp delegate
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
[[NSWorkspace sharedWorkspace] openURL:[NSURL URLWithString:#"http://www.example.com/"]];
}
It's not a delegate method. You need to implement an Apple Event handler for the getURL event.
As luck would have it, this is exactly the case Apple uses to demonstrate implementing an Apple Event handler.
I already had implemented the getURL event, so that alone isn’t enough to get the application to open a url on launch. The trick is that the AppleEvent must be installed in applicationWillFinishLaunching: not applicationDidFinishLaunching:. Otherwise, the event isn’t sent at all because the app hasn’t registered it in time.
To implement a protocol handler that you can select (in Safari preferences, for example) as the "default browser" and which will launch in response to HTTP / HTTPS, you need to do a few things.
Add .scriptSuite and .scriptTerminology files to your project resources. These will tell Mac OS X that you'll be handling the GetURL command.
Add a CFBundleURLTypes key to your Info.plist file listing the "URL Schemes" that your app will handle.
Also in Info.plist, add the NSAppleScriptEnabled key with the value YES.
Add a new class to your application as a subclass of NSScriptCommand and implement the -(id)performDefaultImplementation selector. From within this function you will find the clicked URL in [self directParameter]. Pass this on to your app's URL handler!
For the full details check out the article:
http://www.xmldatabases.org/WK/blog/1154_Handling_URL_schemes_in_Cocoa.item

Cocoa equivalent of .NET's Environment.SpecialFolder for saving preferences/settings?

How do I get the reference to a folder for storing per-user-per-application settings when writing an Objective-C Cocoa app in Xcode?
In .NET I would use the Environment.SpecialFolder enumeration:
Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
What's the Cocoa equivalent?
In Mac OSX application preferences are stored automatically through NSUserDefaults, which saves them to a .plist file ~/Library/Preferences/. You shouldn't need to do anything with this file, NSUserDefaults will handle everything for you.
If you have a data file in a non-document based application (such as AddressBook.app), you should store it in ~/Library/Application Support/Your App Name/. There's no built-in method to find or create this folder, you'll need to do it yourself. Here's an example from one of my own applications, if you look at some of the Xcode project templates, you'll see a similar method.
+ (NSString *)applicationSupportFolder;
{
// Find this application's Application Support Folder, creating it if
// needed.
NSString *appName, *supportPath = nil;
NSArray *paths = NSSearchPathForDirectoriesInDomains( NSApplicationSupportDirectory, NSUserDomainMask, YES );
if ( [paths count] > 0)
{
appName = [[NSBundle mainBundle] objectForInfoDictionaryKey:#"CFBundleExecutable"];
supportPath = [[paths objectAtIndex:0] stringByAppendingPathComponent:appName];
if ( ![[NSFileManager defaultManager] fileExistsAtPath:supportPath] )
if ( ![[NSFileManager defaultManager] createDirectoryAtPath:supportPath attributes:nil] )
supportPath = nil;
}
return supportPath;
}
Keep in mind that if your app is popular you'll probably get requests to be able to have multiple library files for different users sharing the same account. If you want to support this, the convention is to prompt for a path to use when the application is started holding down the alt/option key.
For most stuff, you should just use the NSUserDefaults API which takes care of persisting settings on disk for you.

Resources