所以我使用以下代码来获取所有UIActivityTypes的文本。
@objc func activityViewControllerPlaceholderItem(activityViewController: UIActivityViewController) -> AnyObject {
return ""
}
@objc func activityViewController(activityViewController: UIActivityViewController, itemForActivityType activityType: String) -> AnyObject? {
if activityType == UIActivityTypeMessage {
return "String for message"
} else if activityType == UIActivityTypeMail {
return "String for mail"
} else if activityType == UIActivityTypePostToTwitter {
return "String for twitter"
} else if activityType == UIActivityTypePostToFacebook {
return "String for facebook"
}
return nil
}
func activityViewController(activityViewController: UIActivityViewController, subjectForActivityType activityType: String?) -> String {
if activityType == UIActivityTypeMessage {
return "Subject for message"
} else if activityType == UIActivityTypeMail {
return "Subject for mail"
} else if activityType == UIActivityTypePostToTwitter {
return "Subject for twitter"
} else if activityType == UIActivityTypePostToFacebook {
return "Subject for facebook"
}
return ""
}
}
这就是有趣的部分发生的地方。我将URL中的图像添加到objectsToShare数组中。如果我想让所有图像都显示出来,这样可以正常工作,但我的问题是我不想显示UIActivityTypeMessage
的图像。那个应该只包含文本,而不是图像。我怎么能这样做?
// Load image to share
let articleURL = URLBuilder.sharedInstance.addOnlyHttps((promo?.imageUrl)!)
KingfisherManager.sharedManager.cache.retrieveImageForKey(articleURL, options: nil) { (image, _) -> () in
if image != nil {
objectsToShare.append(image!)
self.openShareViewController(objectsToShare, sender: sender )
} else {
KingfisherManager.sharedManager.downloader.downloadImageWithURL(NSURL(string: articleURL)!, progressBlock: nil, completionHandler: { (image, error, imageURL, originalData) in
if image != nil {
KingfisherManager.sharedManager.cache.storeImage(image!, forKey: articleURL)
objectsToShare.append(image!)
self.openShareViewController(objectsToShare, sender: sender)
}
})
}
}
答案 0 :(得分:3)
创建两个UIActivityItemSource类,如下所示
<强>的SourceOne:强>
class MyStringItemSourceOne: NSObject, UIActivityItemSource {
@objc func activityViewControllerPlaceholderItem(activityViewController: UIActivityViewController) -> AnyObject {
return ""
}
@objc func activityViewController(activityViewController: UIActivityViewController, itemForActivityType activityType: String) -> AnyObject? {
if activityType == UIActivityTypeMessage {
return "String for message"
} else if activityType == UIActivityTypeMail {
return "String for mail"
} else if activityType == UIActivityTypePostToTwitter {
return "String for twitter"
} else if activityType == UIActivityTypePostToFacebook {
return "String for facebook"
}
return nil
}
}
通过添加上面的代码,我们设置要共享的字符串内容。注意根据活动的活动类型我们设置不同的字符串值。
我们的要求是为所有activityType设置图像,期望 UIActivityTypeMessage ,这样我们就可以创建另一个 UIActivityItemSource
<强> SourceTwo 强>
class MyStringItemSourceTwo: NSObject, UIActivityItemSource {
@objc func activityViewControllerPlaceholderItem(activityViewController: UIActivityViewController) -> AnyObject {
return ""
}
@objc func activityViewController(activityViewController: UIActivityViewController, itemForActivityType activityType: String) -> AnyObject? {
let Image: UIImage = UIImage(data: NSData(contentsOfURL: NSURL(string: "http://i.telegraph.co.uk/multimedia/archive/03589/Wellcome_Image_Awa_3589699k.jpg")!)!)!
if activityType == UIActivityTypeMessage {
return nil
} else if activityType == UIActivityTypeMail {
//return "String for mail"
return Image
} else if activityType == UIActivityTypePostToTwitter {
return Image
} else if activityType == UIActivityTypePostToFacebook {
return Image
}
return nil
}
}
接下来将以上两个来源添加到UIActivityViewController中作为 activityitems ,如下所示
@IBAction func Test(sender: AnyObject) {
let activityVC = UIActivityViewController(activityItems: [MyStringItemSourceOne(),MyStringItemSourceTwo()] as [AnyObject], applicationActivities: nil)
//NOTE: Instead of using rootviewcontroller go with your own way.
if let window = (UIApplication.sharedApplication().delegate as? AppDelegate)?.window
{
window.rootViewController?.presentViewController(activityVC, animated: true, completion: nil)
}
}
现在一切都会按照你的要求正常工作。