NSSharingService: How to compare with the constants?

818 views Asked by At

I want to do something when the user shared to

- (void)sharingService:(NSSharingService *)sharingService didShareItems:(NSArray *)items
{
    BOOL isSafariReadingList = [sharingService.description rangeOfString:NSSharingServiceNameAddToSafariReadingList].location != NSNotFound;
}

I can't find any property on NSSharingService that I can compare to the constants. Am I missing something?!

2

There are 2 answers

1
StuFF mc On

Since it seems there's no answer to that (filed http://openradar.appspot.com/16114564), I created a category

@implementation NSSharingService (ActivityType)

- (NSString*)activityType {
    NSRange range = [self.description rangeOfString:@"\\[com.apple.share.*\\]" options:NSRegularExpressionSearch];
    range.location++; // Start after [
    range.length -= 2; // Remove both [ and ]
    return [self.description substringWithRange:range];
}
@end

then you can use it these ways:

    [[sharingService activityType] isEqualToString:NSSharingServiceNameAddToSafariReadingList];

    [@[NSSharingServiceNameAddToSafariReadingList, NSSharingServiceNameAddToIPhoto]
         containsObject:[sharingService activityType]];

I guess Apple just didn't think we'd want to know which service people picked from the Picker.

Obviously dangerous to parse that “com.apple.share.System” but it’s the only way I see to avoid a set of rangeOfString || rangeOfString || rangeOfString…..

For more information check out https://github.com/stuffmc/SharingPicker

Also, as a reference, here are some of the values of those NSSharingServiceName* constants, all starting with com.apple.share.

PostOnFacebook                  Facebook.post
PostOnTwitter                   Twitter.post
PostOnSinaWeibo                 SinaWeibo.post
PostOnTencentWeibo              TencentWeibo.post
PostOnLinkedIn                  LinkedIn.post
ComposeEmail                    Mail.compose
ComposeMessage                  Messages.compose
SendViaAirDrop                  AirDrop.send
UseAsTwitterProfileImage        Twitter.set-profile-image
UseAsFacebookProfileImage       Facebook.set-profile-image
UseAsLinkedInProfileImage       LinkedIn.set-profile-image
PostImageOnFlickr               Video.upload-image-Flickr
AddToSafariReadingList          System.add-to-safari-reading-list
AddToIPhoto                     System.add-to-iphoto
0
pointum On

An equivalent of this check works in my Swift code:

sharingService == NSSharingService(named: NSSharingServiceNameAddToSafariReadingList)