使用 `UIActivityItemSource` 协议无法分享到 Instagram

Posted

技术标签:

【中文标题】使用 `UIActivityItemSource` 协议无法分享到 Instagram【英文标题】:Sharing to Instagram does not work using the `UIActivityItemSource` protocol 【发布时间】:2017-08-07 20:46:08 【问题描述】:

我使用UIActivityViewController 共享文本和图像。图片来自activityImage,这是一个UIImage()。自定义文本来自activityText(),它是从UIActivityItemSource 协议返回的。

问题是,Instagram 仅在特定条件下在 UIActivityViewController 中可见。

总结以下测试:

Instagram 仅在UIActivityViewController activityItems: [activityImage]activityItems: [activityImage, NSNull()]可见

Instagram 对于activityItems: [activityImage, activityText()]隐藏,即使返回的UIActivityItemSource 协议值为""nilNSNull()

问题是需要调用UIActivityItemSource协议,这样不同的共享平台才能分开管理。


问题:

调用UIActivityItemSource协议时如何分享到Instagram?

在调用UIActivityItemSource 协议时,我可以尝试哪些""nilNSNull() 的替代方案?


代码:

class activityText: NSObject, UIActivityItemSource 
    @objc func activityViewControllerPlaceholderItem(activityViewController: UIActivityViewController) -> AnyObject 
        return ""
    
    @objc func activityViewController(activityViewController: UIActivityViewController, itemForActivityType activityType: String) -> AnyObject? 
        switch activityType 
        case UIActivityTypeMessage:
            return "Special text when sharing to Messages."
        default:
            return ""
        
    


func shareEverwhere() 
      var activityImage: UIImage() 
      let activity = UIActivityViewController(activityItems: [activityImage, activityText()], applicationActivities: nil)
      self.presentViewController(activity, animated: true, completion: nil)


测试:

Instagram 在UIActivityViewController可见

let activity = UIActivityViewController(activityItems: [activityImage], applicationActivities: nil)

let activity = UIActivityViewController(activityItems: [activityImage, NSNull()], applicationActivities: nil)

Instagram 隐藏UIActivityViewController

let activity = UIActivityViewController(activityItems: [activityImage, ""], applicationActivities: nil)

let activity = UIActivityViewController(activityItems: [activityImage, activityText()], applicationActivities: nil)

let activity = UIActivityViewController(activityItems: [activityImage, activityText()], applicationActivities: nil)
class activityText: NSObject, UIActivityItemSource 
    @objc func activityViewControllerPlaceholderItem(activityViewController: UIActivityViewController) -> AnyObject 
        return ""
    
    @objc func activityViewController(activityViewController: UIActivityViewController, itemForActivityType activityType: String) -> AnyObject? 
        return NSNull()
    

let activity = UIActivityViewController(activityItems: [activityImage, activityText()], applicationActivities: nil)
class activityText: NSObject, UIActivityItemSource 
    @objc func activityViewControllerPlaceholderItem(activityViewController: UIActivityViewController) -> AnyObject 
        return ""
    
    @objc func activityViewController(activityViewController: UIActivityViewController, itemForActivityType activityType: String) -> AnyObject? 
            return nil
    

let activity = UIActivityViewController(activityItems: [activityImage, activityText()], applicationActivities: nil)
class activityText: NSObject, UIActivityItemSource 
    @objc func activityViewControllerPlaceholderItem(activityViewController: UIActivityViewController) -> AnyObject 
        return ""
    
    @objc func activityViewController(activityViewController: UIActivityViewController, itemForActivityType activityType: String) -> AnyObject? 
            return ""
    

【问题讨论】:

【参考方案1】:

问题的提问和回答地址为:Share image with hashtag via UIActivityViewController (Twitter, Facebook, Instagram)

        UIActivityViewController *activityViewController =
 [[UIActivityViewController alloc] initWithActivityItems:@[vm.image, [[ActivityStringItemSource alloc] initWithString:attributedString]] applicationActivities:nil];

而实现字符串返回(或不返回)的类是:

@interface ActivityStringItemSource()<UIActivityItemSource>

@property(strong, nonatomic) NSMutableAttributedString *stringContent;
@end

@implementation ActivityStringItemSource

- (instancetype)initWithString:(NSMutableAttributedString *) placeholder

    self = [super init];
    if (self) 
        _stringContent = placeholder;
    
    return self;


- (id)activityViewControllerPlaceholderItem:(UIActivityViewController *)activityViewController 
    return [NSObject new];


- (id)activityViewController:(UIActivityViewController *)activityViewController itemForActivityType:(UIActivityType)activityType 
    if([activityType containsString:@"instagram"]) 
        return nil;
     else 
        return _stringContent;
    


@end

正如原始答案所指出的那样,关键是返回一个 NSObject(该类扩展了 NSObject)。然后在项目选择中,只需测试 instagram 以返回 nil,否则返回您的字符串。

【讨论】:

感谢@Olivier Brand,我会检查一下并试一试。 你能解释更多吗?!【参考方案2】:

测试后,您只需将图像添加到 activityViewControllerPlaceholderItem

final class MyImageItemSource: NSObject, UIActivityItemSource 

    final let image: UIImage

    init(image: UIImage) 
        self.image = image
    

    func activityViewControllerPlaceholderItem(_ activityViewController: UIActivityViewController) -> Any 
        image
    

    func activityViewController(_ activityViewController: UIActivityViewController, itemForActivityType activityType: UIActivity.ActivityType?) -> Any? 
        image
    


【讨论】:

以上是关于使用 `UIActivityItemSource` 协议无法分享到 Instagram的主要内容,如果未能解决你的问题,请参考以下文章

如何从 UIActivityItemSource 函数返回多个值

UIActivityItemSource协议可选方法在iOS 8中不起作用

自定义 UIActivity 的主题

多个 UIActivityViewController 占位符项目?

测试使用

第一篇 用于测试使用