NSPerformService "Tweet" with an Image (Twitter.app) - objective-c

Is there anyway to NSPerformService(#"Tweet", [NSPasteboard generalPasteboard]); with an image? I know that simply text is possible, but i don't know about the image.If yes, example code would be nice.
Thank you.
PS: for those who don't know, this is only possible if the user has the Twitter.app installed.

The types that you can send to a service depends upon the types that the registered application has declared in its Info.plist. For Twitter.app, this looks like:
<key>NSMenuItem</key>
<dict>
<key>default</key>
<string>Tweet</string>
</dict>
<key>NSSendTypes</key>
<array>
<string>NSStringPboardType</string>
</array>
Unfortunately, this means that you will have to transform the image into a string before it will be accepted by the service.

Related

What plist settings should I use to share documents in iOS8

I'm trying to share a document using SLComposeServiceViewController in iOS, the document I'm testing this out with is a pdf (though I need the ability to share any file).
I send my PDF to Mail on the iPhone, press and hold on the attachment, but my icon never shows.
I have image, URL and Text share working fine, I just can't seem to get any type of document to work.
I'm assuming NSExtensionActivationSupportsAttachmentsWithMaxCount and/or NSExtensionActivationSupportsFileWithMaxCount need to be set, but I have no idea what the difference is between them.
This is my plist at the moment...
Can anyone help?
<key>NSExtension</key>
<dict>
<key>NSExtensionAttributes</key>
<dict>
<key>NSExtensionActivationRule</key>
<dict>
<key>NSExtensionActivationSupportsImageWithMaxCount</key>
<integer>10</integer>
<key>NSExtensionActivationSupportsAttachmentsWithMinCount</key>
<integer>0</integer>
<key>NSExtensionActivationSupportsAttachmentsWithMaxCount</key>
<integer>20</integer>
<key>NSExtensionActivationSupportsMovieWithMaxCount</key>
<integer>0</integer>
<key>NSExtensionActivationSupportsText</key>
<true/>
<key>NSExtensionActivationSupportsVideoWithMaxCount</key>
<integer>0</integer>
<key>NSExtensionActivationSupportsWebPageWithMaxCount</key>
<integer>0</integer>
<key>NSExtensionActivationSupportsWebURLWithMaxCount</key>
<integer>1</integer>
<key>NSExtensionActivationSupportsFileWithMaxCount</key>
<integer>1</integer>
</dict>
</dict>
<key>NSExtensionMainStoryboard</key>
<string>MainInterface</string>
<key>NSExtensionPointIdentifier</key>
<string>com.apple.share-services</string>
</dict>
These are predicates and all of them have to be satisfied for your extension to be shown. You probably only need NSExtensionActivationSupportsAttachmentsWithMinCount like this:
<dict>
<key>NSExtensionAttributes</key>
<dict>
<key>NSExtensionActivationRule</key>
<dict>
<key>NSExtensionActivationSupportsAttachmentsWithMinCount</key>
<integer>1</integer>
</dict>
<key>NSExtensionPointName</key>
<string>com.apple.share-services</string>
<key>NSExtensionPointVersion</key>
<string>1.0</string>
</dict>
Test using just TRUEPREDICATE to make sure the predicates are really the cause of the problem.
One more thing: share extensions are not enabled by default, so make sure you check the More [...] button at the end of the list of share targets.
You can get files from Mail (or similar) and Dropbox (Box, iCloud or similar) with this code in your extension info.plist
<key>NSExtension</key>
<dict>
<key>NSExtensionAttributes</key>
<dict>
<key>NSExtensionActivationRule</key>
<dict>
<key>NSExtensionActivationSupportsFileWithMaxCount</key>
<integer>1</integer>
<key>NSExtensionActivationSupportsWebURLWithMaxCount</key>
<integer>1</integer>
</dict>
</dict>
<key>NSExtensionMainStoryboard</key>
<string>MainInterface</string>
<key>NSExtensionPointIdentifier</key>
<string>com.apple.share-services</string>
</dict>

Multi Value Setting (Settings.bundle) issue

I just can't get Multi Value settings to work in my Ipad app.
This is my plist code for the multi value:
<dict>
<key>Type</key>
<string>PSMultiValueSpecifier</string>
<key>DefaultValue</key>
<string>0</string>
<key>Values</key>
<array>
<string>0</string>
<string>1</string>
<string>2</string>
</array>
<key>Title</key>
<string>Project</string>
<key>Key</key>
<string>project</string>
</dict>
I already tried everything, reset the iPad simulator, added other settings... and multi values keep on not showing in the settings. Am i missing something?
Solved this by defining the array of "Titles". It won't show the setting without it.

How to define a Uniform Type Identifier in a plist file?

My application uses the following NSApplicationDelegate function.
- (void)application:(NSApplication*)sender openFiles:(NSArray*)filenames;
I want to use this function to enable the user to drag and drop image files onto the application icon in the dock.
How do I have to define certain file types in my plist file to restrict them to be images? I found out the structure has to look something like this.
// plist file contents taken from Preview.app
[...]
<key>CFBundleDocumentTypes</key>
<array>
<dict>
<key>CFBundleTypeIconFile</key>
<string>jpeg.icns</string>
<key>CFBundleTypeRole</key>
<string>Viewer</string>
<key>LSIsAppleDefaultForType</key>
<true/>
<key>LSItemContentTypes</key>
<array>
<string>public.jpeg</string>
</array>
<key>NSDocumentClass</key>
<string>PVDocument</string>
</dict>
</array>
I added it to the plist file but it does not work. A popup window shows the following error message.
The document "test.jpg" could not be opened. MyApp cannot open files
in the "JPEG image" format.
Further, I read in the documentation that there is public.image which would be what I want to define.
Meanwhile, I found out that the plist file only contains the key CFBundleDocumentTypes if I create a Cocoa Application with the option "Create document-based application.". Can you please clarify what dependencies exist for the option?
<key>CFBundleDocumentTypes</key>
<array>
<dict>
<key>CFBundleTypeExtensions</key>
<array>
<string>png</string>
<string>jpg</string>
... add as many types as you need
</array>
... other keys
</dict>
</array>
Update: The CFBundleDocumentTypes key is deprecated in Mac OS X v10.5. The new key LSItemContentTypes should be used instead. The items are UTI strings:
<key>LSItemContentTypes</key>
<array>
<string>public.png</string>
</array>
If your document types are common types, you could use UTI's
About UTI's

How do I make a file that identifies as a custom UTI type?

Right now I have an application that generates an XML file. We'll call it someFile.myapp
When my app saves the file and look at it using mdls it has a kMDItemContentType of "dyn.234kdfsjk23jk24234kjfd"
How can I get the UTI type of the file to be a custom value like com.mycompany.myapp?
UTIs are linked to extensions via a declaration in your application's Info.plist file. Add a section like so:
<key>UTExportedTypeDeclarations</key>
<array>
<dict>
<key>UTTypeConformsTo</key>
<array>
<string>public.xml</string>
</array>
<key>UTTypeDescription</key>
<string>My Special File Type</string>
<key>UTTypeIdentifier</key>
<string>org.myapp.someutiidentifier</string>
<key>UTTypeTagSpecification</key>
<dict>
<key>public.filename-extension</key>
<array>
<string>myapp</string>
</array>
</dict>
</dict>
</array>
Dream up a unique extension for your file, and use an exported UTI declaration to associate the extension with your UTI.
Files are mapped to UTIs by Launch Services, which is theoretically able to use many different kinds of information about the file to identify its type, but in practice will disregard everything other than the file extension. Consequently, if you want your file's type to be identifiable, you need to come up with a unique extension and map it to the relevant type in your app's Info.plist. If your files need to share an extension that has already been claimed by another app, then you're SOL.
As far as I can see -- and I say this as a longtime Mac fan -- this is the one aspect of Mac OS X where it is really apt to say: "Welcome to 1985!"

Registering an icon for my application's document type

I'm trying to register an icon for my app's document type. After reading Declaring New Uniform Type Identifiers and looking at /Developer/Examples/Sketch I came up with something like this in my Info.plist:
<key>CFBundleDocumentTypes</key>
<array>
<dict>
<key>CFBundleTypeRole</key>
<string>Viewer</string>
<key>LSItemContentTypes</key>
<array>
<string>com.mycompany.myextension</string>
</array>
<key>NSDocumentClass</key>
<string>NSString</string>
</dict>
</array>
...
<key>UTExportedTypeDeclarations</key>
<array>
<dict>
<key>UTTypeDescription</key>
<string>Blah blah blah</string>
<key>UTTypeConformsTo</key>
<array>
<string>public.data</string>
</array>
<key>UTTypeIconFile</key>
<string>My-file-icon.icns</string>
<key>UTTypeIdentifier</key>
<string>com.mycompany.myextension</string>
<key>UTTypeTagSpecification</key>
<dict>
<key>public.filename-extension</key>
<array>
<string>myextension</string>
</array>
</dict>
</dict>
</array>
Now, everything is fine and dandy, i.e. my program is opened when I click on a file with my extension, etc. However, the document icon is not registered with the OS, i.e. I see an ugly blank icon instead of my beautiful My-file-icon.icns. I suspect that I'm missing something in the plist above, any ideas?
Try putting the icon name in the CFBundleTypeIconFile key in the CFBundleDocumentTypes array, not in the UTExportedTypeDeclarations array.
And of course make sure that "My-file-icon.icns" is in your target's Copy Bundle Resources build phase and is being copied into Contents/Resources in your app's bundle.
I've been trying to do this in Xcode 11 and it's been a challenge. There are lots of answers here on SO to questions like this but I couldn't find one that had everything I needed to get this working. I'll try to do that here.
Technical Q&A QA1587 Although this is a bit old I think it has everything required but it's a bit vague in some spots like when it says Provide an icon for the document.
I thought I could make it work with an icon in Assets.xcassets but it didn't work for me until I provided an icns file. I took the 1024x1024 png app icon and converted it to an icns here at cloudconvert. Then I added that icns to my project. I don't think it matters where you put it. I put it just below the top level. It's circled in the image below.
Follow the arrows in the image and you end up at the Info tab. I tapped the Add button to create a Document Type and filled it in as shown. When you tap the Add button in the icon section it will ask you for an icns file. Once I had mine in the project it appeared in the window and I could select it.
Same for Configured UTIs.
That was it. In Files, Messages, or Mail the icon appears.
Your UTI declaration in the Info.plist appears to be correct, however I noticed an other issue. If you application is a document-based application you need to replace the NSString in following entry with your NSDocument subclass:
<key>NSDocumentClass</key>
<string>NSString</string>
For example it's "SKTDocument" in Sketch:
<key>NSDocumentClass</key>
<string>SKTDocument</string>
Edit:
Please also make sure to use your own reverse domain name for your exported UTIs. This ensures that UTIs are unique. For example its com.mindnode.MindNode.MindNodeDocument in my case.
I also had a similar problem, turns out I needed to rebuild the launch services database as per what i have documented here....
https://stackoverflow.com/a/12466968/1571635