我使用UIActivityViewController
分享文字和图片。该图片来自activityImage
,即UIImage()
。自定义文本来自activityText()
,它是从UIActivityItemSource
协议返回的。
麻烦的是,Instagram只能在UIActivityViewController
的某些条件下显示。
总结以下测试:
Instagram仅UIActivityViewController
activityItems: [activityImage]
和activityItems: [activityImage, NSNull()]
即使返回的activityItems: [activityImage, activityText()]
协议值为UIActivityItemSource
,""
,nil
,Instagram仍为NSNull()
隐藏 }。
问题是需要调用UIActivityItemSource
协议,以便可以单独管理不同的共享平台。
问题:
如何在调用
UIActivityItemSource
协议时与Instagram分享?在调用
""
协议时,我可以尝试nil
,NSNull()
和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 ""
}
}
答案 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,否则返回你的字符串。