NSSharingService:如何与常量进行比较?

时间:2014-02-15 11:50:54

标签: objective-c macos cocoa sharing social

我希望在用户分享到

时执行某些操作
- (void)sharingService:(NSSharingService *)sharingService didShareItems:(NSArray *)items
{
    BOOL isSafariReadingList = [sharingService.description rangeOfString:NSSharingServiceNameAddToSafariReadingList].location != NSNotFound;
}

我在NSSharingService找不到任何可以与常数比较的属性。我错过了什么吗?!

2 个答案:

答案 0 :(得分:2)

由于似乎没有答案(提交http://openradar.appspot.com/16114564),我创建了一个类别

@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

然后您可以通过以下方式使用它:

    [[sharingService activityType] isEqualToString:NSSharingServiceNameAddToSafariReadingList];

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

我想Apple认为我们不想知道人们从Picker中挑选了哪些服务。

解析“com.apple.share.System”显然很危险,但这是我看到避免一组rangeOfString || rangeOfString || rangeOfString….的唯一方法。

有关详细信息,请查看 https://github.com/stuffmc/SharingPicker

此外,作为参考,以下是NSSharingServiceName*常量的一些值,所有值均以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

答案 1 :(得分:1)

此检查的等价物适用于我的Swift代码:

sharingService == NSSharingService(named: NSSharingServiceNameAddToSafariReadingList)