xcode 子类化并将 UIActivityItemProvider 与 UIActivityViewController 一起使用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13494611/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Subclass and using UIActivityItemProvider with UIActivityViewController
提问by Astral Walker
I finally find someone who was facing the same problem than me.
我终于找到了一个和我面临同样问题的人。
UIActivityViewController customize text based on selected activity
UIActivityViewController 根据选定的活动自定义文本
I want to customize the content share with the activities of the UIActivityViewController. The good answer is the following:
我想自定义与 UIActivityViewController 的活动的内容共享。好的答案如下:
"Instead of passing the text strings into the initWithActivityItems call, pass in your own sub-class of the UIActivityItemProvider class and when you implement the itemForActivityType method it will provide the sharing service as the 'activityType' parameter.
You can then return the customized content from this method."
“不是将文本字符串传递到 initWithActivityItems 调用中,而是传入您自己的 UIActivityItemProvider 类的子类,当您实现 itemForActivityType 方法时,它将提供共享服务作为 'activityType' 参数。
然后,您可以从此方法返回自定义内容。”
I understand tricks, but I'm not getting the way to do it...
我了解技巧,但我不知道如何做到这一点......
I did this as a subclass:
我是作为一个子类这样做的:
@interface SharingItems : UIActivityItemProvider
@implementation SharingItems
-(id)activityViewController:(UIActivityViewController *)activityViewController itemForActivityType:(NSString *)activityType
{
// Here depending on the activityType i want to share NSString or UIImage
}
@end
But I don't know what to do now in my original viewController:
但我不知道现在在我原来的 viewController 中该做什么:
-(void)actionSheet
{
if ([[UIActivityViewController class] respondsToSelector:@selector(alloc)])
{
__block NSString *imgName = [[NSString alloc] initWithFormat:@"%@", _sharingUrl];
NSArray *activityItems = [NSArray arrayWithObjects:imgName, nil];
UIActivityViewController *activityController = [[UIActivityViewController alloc] initWithActivityItems:activityItems applicationActivities:nil];
[self presentViewController:activityController animated:YES completion:nil];
__block NSString *chan = [[NSString alloc] initWithFormat:@"%@", _channel];
[activityController setCompletionHandler:^(NSString* activityType, BOOL completed)
{
if (completed)
{
}
}];
}
else
[self displayActionSheet];
}
回答by mangtronix
Here's an example UIActivityItemProvider (not tested but adapted from working code):
这是一个 UIActivityItemProvider 示例(未经测试但改编自工作代码):
@implementation StringProvider
- (id)initWithPlaceholderString:(NSString*)placeholder facebookString:(NSString*)facebookString
{
self = [super initWithPlaceholderItem:placeholder];
if (self) {
_facebookString = facebookString;
}
return self;
}
- (id)item
{
if ([self.activityType isEqualToString:UIActivityTypePostToFacebook]) {
return _facebookString;
} else {
return self.placeholderItem;
}
}
@end
Then when you set up the activity view controller:
然后当你设置活动视图控制器时:
StringProvider *stringProvider = [[StringProvider alloc] initWithPlaceholderString:@"Default string" facebookString:@"Hello, Facebook."];
UIActivityViewController *shareController = [[UIActivityViewController alloc] initWithActivityItems:@[stringProvider] applicationActivities:nil];
Basically you create UIActivityItemProviders that provide the right data when the -(id)item method is called and pass in those activity item providers when you create the activity view controller. You need to initialize with a placeholder item so the OS knows what class the final item will be (most likely NSString, NSURL, UIImage). Hope that helps!
基本上,您创建 UIActivityItemProviders 以在调用 -(id)item 方法时提供正确的数据,并在创建活动视图控制器时传入这些活动项提供程序。您需要使用占位符项进行初始化,以便操作系统知道最终项将是什么类(最有可能是 NSString、NSURL、UIImage)。希望有帮助!

