Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I get YouTube to appear in the share sheet of my OS X app?

According to Apple documentation, YouTube is not included in the available sharing services, and indeed when I look at the Share Menu extensions in System Preferences I do not see it there.

Presenting the sharing sheet in my own app using NSSharingServicePicker as follows does not include YouTube either.

NSSharingServicePicker *sharingServicePicker = [[NSSharingServicePicker alloc] initWithItems:@[movieFileURL]];
[sharingServicePicker showRelativeToRect:myView.bounds ofView:myView preferredEdge:NSMinYEdge];

However when using the share sheet in QuickTime Player or iMovie, YouTube is an option, as shown below. Is there any way to get YouTube to appear as an option in my app or have Apple just added YouTube to these apps specifically without adding it to the operating system wide list?

YouTube sharing in QuickTime PlayerYouTube sharing in iMovie

like image 524
DanielGibbs Avatar asked Sep 28 '22 05:09

DanielGibbs


1 Answers

It seems that the YouTube sharing option is not available at the operating system level and that QuickTime Player and iMovie implement it themselves. If you implement the sharing mechanism yourself (e.g. using Google's Objective C API) you can create a sharing menu containing YouTube as follows (this assumes that you have a NSSharingService subclass called YouTubeSharingService):

- (void)addSharingMenuItemsToMenu:(NSMenu *)menu {
    // Get the sharing services for the file.
    NSMutableArray *services = [[NSSharingService sharingServicesForItems:@[self.fileURL]] mutableCopy];
    [services addObject:[YouTubeSharingService new]];

    // Create menu items for the sharing services.
    for (NSSharingService *service in services) {
        NSMenuItem *menuItem = [[NSMenuItem alloc] init];
        menuItem.title = service.menuItemTitle;
        menuItem.image = service.image;
        menuItem.representedObject = service;
        menuItem.target = self;
        menuItem.action = @selector(executeSharingService:);
        [menu addItem:menuItem];
    }
}

- (void)executeSharingService:(id)sender {
    if ([sender isKindOfClass:[NSMenuItem class]]) {
        NSMenuItem *menuItem = sender;
        if ([menuItem.representedObject isKindOfClass:[NSSharingService class]]) {
            NSSharingService *sharingService = menuItem.representedObject;
            [sharingService performWithItems:@[self.fileURL]];
        }
    }
}
like image 175
DanielGibbs Avatar answered Nov 12 '22 23:11

DanielGibbs