Search code examples
cocoaosx-mountain-lion

NSSharingService - Remove facebook and twitter and add Print


I've just implemented a share-button, that has a share menu:

 [_shareButton sendActionOn:NSLeftMouseDownMask];

And has this action connected:

-(IBAction)share:(id)sender {   
    NSArray *shareArray = @[@"testShare"];
    NSSharingServicePicker *sharingServicePicker = [[NSSharingServicePicker alloc] initWithItems:shareArray];
    sharingServicePicker.delegate = self;

    [sharingServicePicker showRelativeToRect:[sender bounds]
                                      ofView:sender
                               preferredEdge:NSMinYEdge];
}

Now to my question, I don't want Facebook and Twitter to be an option in the menu. I only want E-Mail and Messages to be available. Also I would like to add "Print", but don't know if I can do that.

Is that possible?

Thanks

(Don't have enough rep points to add 'NSSharingService' as a tag)


Solution

  • Solved it by using proposedSharingServices.

    - (NSArray *)sharingServicePicker:(NSSharingServicePicker *)sharingServicePicker sharingServicesForItems:(NSArray *)items proposedSharingServices:(NSArray *)proposedServices{
    
    // Find and the services you want
    NSMutableArray *newProposedServices = [[NSMutableArray alloc] initWithCapacity:5];
    for (NSSharingService *sharingService in proposedServices) {
        if ([[sharingService title] isEqualToString:@"Email"] || [[sharingService title] isEqualToString:@"Message"]) {
            [newProposedServices addObject:sharingService];
        }
    }
    NSArray *services = newProposedServices;
    
    NSSharingService *customService = [[NSSharingService alloc] initWithTitle:@"Print" image:[NSImage imageNamed:@"PrintImage"] alternateImage:nil handler:^{
        // Do whatever
    }];
    
    services = [services arrayByAddingObject:customService];
    
    return services;
    

    }