swiftmacosnssharingservice

Call NSSharingService from FinderSync extension


I am trying to send an email(open the default mail client application) from my FinderSync extension.

        let service = NSSharingService(named: NSSharingService.Name.composeEmail)
        if(service != nil)
        {
            service!.recipients = ["test@gmail.com"]
            service!.subject = "Test Mail"
           if service!.canPerform(withItems: ["Test Mail body"])
           {
                service!.perform(withItems: ["Test Mail body"])
           }
           else
           {
                  //fail for me
          }
        }

The same code executes from AppDelegate/ViewController while starting the main application but not working when calling from the extension.


Solution

  • FinderSync is an NSExtension which is not allowed to use NSSharingService

    /* @class SHKSharingService (ShareKit.framework) */  
    
    -(char)canPerformWithItems:(NSArray *)items {
        if (([[SHKSharingService class] isShareKitPlugInService] || ([items count] > 0x1388)) {
        return NO;
    }
    

    isShareKitPlugInService is defined as

    [[NSBundle mainBundle] infoDictionary][@"NSExtension"]