使用`UIActivityItemSource`协议无法使用Instagram共享

时间:2017-08-07 20:46:08

标签: ios swift optimization uiactivityviewcontroller uiactivityitemprovider

我使用UIActivityViewController分享文字和图片。该图片来自activityImage,即UIImage()。自定义文本来自activityText(),它是从UIActivityItemSource协议返回的。

麻烦的是,Instagram只能在UIActivityViewController的某些条件下显示。

总结以下测试:

  • Instagram仅UIActivityViewController activityItems: [activityImage]activityItems: [activityImage, NSNull()]

  • 可见
  • 即使返回的activityItems: [activityImage, activityText()]协议值为UIActivityItemSource""nil,Instagram仍为NSNull() 隐藏 }。

问题是需要调用UIActivityItemSource协议,以便可以单独管理不同的共享平台。

  

问题:

     
      
  • 如何在调用UIActivityItemSource协议时与Instagram分享?

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

  •   

代码:

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 个答案:

答案 0 :(得分:3)

问题在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,否则返回你的字符串。