2

我刚刚实现了一个共享按钮,它有一个共享菜单:

 [_shareButton sendActionOn:NSLeftMouseDownMask];

并已连接此操作:

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

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

现在我的问题是,我不希望 Facebook 和 Twitter 成为菜单中的一个选项。我只希望电子邮件和消息可用。另外我想添加“打印”,但不知道我是否可以这样做。

那可能吗?

谢谢

(没有足够的代表点来添加“NSSharingService”作为标签)

4

5 回答 5

1

通过使用解决它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;

}

于 2013-05-23T07:19:17.727 回答
1

将提议的服务与新的命名实例进行比较是可行的。这是我项目中的一个简单的 Swift 代码:

let excludedNames = [
    NSSharingServiceNamePostOnFacebook,
    NSSharingServiceNamePostOnTwitter,
]

var excludedServices = [NSSharingService]()

for name in excludedNames {
    if let service = NSSharingService(named: name) {
        excludedServices += [service]
    }
}

return proposedServices.filter {
    !excludedServices.contains($0)
}

无需使用私有name财产。

于 2015-10-09T13:59:23.767 回答
1

而不是试图说出您不想要什么,而是简单地返回您想要什么的列表。

- (NSArray<NSSharingService *> *)sharingServicePicker:(NSSharingServicePicker *)sharingServicePicker sharingServicesForItems:(NSArray *)items proposedSharingServices:(NSArray<NSSharingService *> *)proposedServices
{
    NSArray *result = @[[NSSharingService sharingServiceNamed:NSSharingServiceNameComposeEmail], [NSSharingService sharingServiceNamed:NSSharingServiceNameComposeMessage]];
    return result;
}
于 2016-10-01T18:30:12.983 回答
0

这是一个更好的方法 - 不需要私有 API 访问。

NSArray *excludedServices = @[NSSharingServiceNamePostOnFacebook,
                              NSSharingServiceNamePostOnTwitter];

NSMutableArray *includedServices = [[NSMutableArray alloc] init];
for (NSSharingService *service in proposedServices) {
    if ([excludedServices indexOfObject:service] == NSNotFound) {
        [includedServices addObject:service];
    }
}

return includedServices;
于 2015-03-15T05:30:56.467 回答
0

通过以下方式略有不同的方法proposedSharingServices

  

- (NSArray*)sharingServicePicker:(NSSharingServicePicker *)sharingServicePicker sharingServicesForItems:(NSArray *)items proposedSharingServices:(NSArray *)proposedServices {

    NSArray *excludedServices = @[NSSharingServiceNamePostOnFacebook,
                                  NSSharingServiceNamePostOnTwitter];

    NSArray *sharingServices = [proposedServices filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"NOT (name IN %@)", excludedServices]];

    return sharingServices;
}
于 2014-08-10T13:51:54.420 回答