如何从 UIActivityItemSource 函数返回多个值

Posted

技术标签:

【中文标题】如何从 UIActivityItemSource 函数返回多个值【英文标题】:How to return multiple values from UIActivityItemSource function 【发布时间】:2018-12-04 04:07:50 【问题描述】:

是否可以从 UIActivityItemSource 函数返回多个值,并将它们传递给 UIActivityViewController 的 activityItems?

我想返回 [message, image, url] 数组并传递给 activityItems。但是我使用的切换代码只返回一个值,消息、图像或 url,显然取决于选择的活动。

如果我改变: func activityViewController(_ activityViewController: UIActivityViewController, itemForActivityType activityType: UIActivity.ActivityType?) -> Any? 转:func.... -> [Any]?

UIActivityItemSource 抱怨:

“类型'ActivityItemSource'不符合协议'UIActivityItemSource'”

并让你用“-> Any?”插入另一组相同的函数

也许我不明白如何处理 init(),但如果有人能给我一个解决这个问题的方向,我将不胜感激。

我在Multiple data from UIActivityItemSource 发现了一个类似的问题 海报本人认为: “原来我需要做的就是向活动控制器提供多个项目,因为每个活动项目源只能提供一个项目。”

这是否意味着我需要为消息创建 ActivityItemSource1 类,为图像创建 ActivityItemSource2 类,为 url 创建 ActivityItemSource3 类?并像这样将它们分配给activityItems?:

让activityItems = [ActivityItemSource1(message: message), ActivityItemSource2(image: image), ActivityItemSource3(url: url)]

================================================ ====================

class ActivityItemSource: NSObject, UIActivityItemSource 

var message: String!
var image: UIImage!
var url: URL!

init(message: String, image: UIImage, url: URL) 
    self.message = message
    self.image = image
    self.url = url


func activityViewControllerPlaceholderItem(_ activityViewController: UIActivityViewController) -> Any 
    return message


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

    switch activityType 
    case UIActivity.ActivityType.postToFacebook:
        return image
    case UIActivity.ActivityType.postToTwitter:
        return message
    case UIActivity.ActivityType.mail:
        return message
    case UIActivity.ActivityType.copyToPasteboard:
        return message
    case UIActivity.ActivityType.markupAsPDF:
        return message
    case UIActivity.ActivityType.message:
        return message
    case UIActivity.ActivityType.postToFlickr:
        return image
    case UIActivity.ActivityType.postToTencentWeibo:
        return message
    case UIActivity.ActivityType.postToVimeo:
        return image
    case UIActivity.ActivityType.print:
        return message
    case UIActivity.ActivityType(rawValue: "com.apple.reminders.RemindersEditorExtension"):
        return message
    case UIActivity.ActivityType(rawValue: "com.apple.mobilenotes.SharingExtension"):
        return message

    default:
       return message

     

   



class TodaysTasksTableViewcontroller: TableViewController 
    var image: UIImage
    var message: String
    var url: URL

    image = UIImage(named: "PoliPoliIconLarge")!
    message = "I will complete the following task(s) today :"
    url = URL(string: "http://www.beckos.com")!

let activityItems = [ActivityItemSource(message: message, image: image, url: url)]

    let activityVC =      UIActivityViewController(activityItems: activityItems, applicationActivities: nil)

// Exclude some activities from share popup screen
    activityVC.excludedActivityTypes = [
        UIActivity.ActivityType.assignToContact,
        UIActivity.ActivityType.print,
        UIActivity.ActivityType.addToReadingList,
        UIActivity.ActivityType.saveToCameraRoll,
        UIActivity.ActivityType.openInIBooks,
        //UIActivity.ActivityType(rawValue: "com.apple.reminders.RemindersEditorExtension"),
        //UIActivity.ActivityType(rawValue: "com.apple.mobilenotes.SharingExtension"),
    ]


    self.present(activityVC, animated: true, completion: nil)

................
    

【问题讨论】:

***.com/questions/20694382/… ? ***.com/questions/13029340/… ? 【参考方案1】:

这就是我昨晚所做的。不过,我不确定这种方式在正确的编码风格方面是否有效。此外,我不确定我是否真正了解 ActivitySourceItems 的工作方式。有人可能会以更有效的方式编写它。

基本上我创建了三个类,一个用于消息,一个用于图像,一个用于 url。然后将三个返回值传递给一个常量,activityItems。不过,Instagram 和 Snapchat 仍然无法使用它。除此之外,它还是做了我想做的事。

感谢链接,Larme。我之前的搜索没有显示这些帖子。

class TableViewController: UITableViewController      
    let activityItems = [ActivityItemSource(message: message),    ActivityItemSourceImage(image: image), ActivityItemSourceURL(url: url)]
    let activityVC = UIActivityViewController(activityItems: activityItems, applicationActivities: nil)

    // some exclusion goes here...

    self.present(activityVC, animated: true, completion: nil)



class ActivityItemSource: NSObject, UIActivityItemSource 

    var message: String!

    init(message: String) 
        self.message = message
    

    func activityViewControllerPlaceholderItem(_ activityViewController: UIActivityViewController) -> Any 
        return message
        // to display Instagram button, return image
        // image: Mail, Message, Notes, Twitter, Instagram, Shared Album, Post to Google Maps, Messenger, LINE, Snapchat, Facebook
        // message: Mail, Message, Notes, Twitter, Messenger, LINE, Facebook, LinkedIn
    

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

        switch activityType 
        case UIActivity.ActivityType.postToFacebook:
            return nil
        case UIActivity.ActivityType.postToTwitter:
            return message
        case UIActivity.ActivityType.mail:
            return message
        case UIActivity.ActivityType.copyToPasteboard:
            return message
        case UIActivity.ActivityType.markupAsPDF:
            return message
        case UIActivity.ActivityType.message:
            return message
        case UIActivity.ActivityType.postToFlickr:
            return message
        case UIActivity.ActivityType.postToTencentWeibo:
            return message
        case UIActivity.ActivityType.postToVimeo:
            return message
        case UIActivity.ActivityType.print:
            return message
        case UIActivity.ActivityType(rawValue: "com.apple.reminders.RemindersEditorExtension"):
            return message
        case UIActivity.ActivityType(rawValue: "com.apple.mobilenotes.SharingExtension"):
            return message
        case UIActivity.ActivityType(rawValue: "com.burbn.instagram.shareextension"):
            return nil
        case UIActivity.ActivityType(rawValue: "jp.naver.line.Share"):
            return message

        default:
            return nil
        
    



class ActivityItemSourceImage: NSObject, UIActivityItemSource 

    var image: UIImage!


    init(image: UIImage) 
        //self.image = image
        self.image = UIImage(named: "PoliPoliIcon")!

    


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

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


        switch activityType 
        case UIActivity.ActivityType.postToFacebook:
            return nil
        case UIActivity.ActivityType.postToTwitter:
            return nil
        case UIActivity.ActivityType.mail:
            return image
        case UIActivity.ActivityType.copyToPasteboard:
            return image
        case UIActivity.ActivityType.markupAsPDF:
            return image
        case UIActivity.ActivityType.message:
            return image
        case UIActivity.ActivityType.postToFlickr:
            return image
        case UIActivity.ActivityType.postToTencentWeibo:
            return image
        case UIActivity.ActivityType.postToVimeo:
            return image
        case UIActivity.ActivityType.print:
            return image
        case UIActivity.ActivityType(rawValue: "com.apple.reminders.RemindersEditorExtension"):
            return nil
        case UIActivity.ActivityType(rawValue: "com.apple.mobilenotes.SharingExtension"):
            return nil
        case UIActivity.ActivityType(rawValue: "com.burbn.instagram.shareextension"):
            return image
        case UIActivity.ActivityType(rawValue: "jp.naver.line.Share"):
            return image
        default:
            return image

        
    




class ActivityItemSourceURL: NSObject, UIActivityItemSource 

    var url: URL!


    init(url: URL) 
        self.url = url
    

    func activityViewControllerPlaceholderItem(_ activityViewController: UIActivityViewController) -> Any 
        return url

    

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

        switch activityType 
        case UIActivity.ActivityType.postToFacebook:
            return url
        case UIActivity.ActivityType.postToTwitter:
            return url
        case UIActivity.ActivityType.mail:
            return url
        case UIActivity.ActivityType.copyToPasteboard:
            return nil
        case UIActivity.ActivityType.message:
            return url
        case UIActivity.ActivityType.postToFlickr:
            return url
        case UIActivity.ActivityType.postToTencentWeibo:
            return url
        case UIActivity.ActivityType.postToVimeo:
            return url
        case UIActivity.ActivityType.print:
            return url
        case UIActivity.ActivityType(rawValue: "com.apple.reminders.RemindersEditorExtension"):
            return url
        case UIActivity.ActivityType(rawValue: "com.apple.mobilenotes.SharingExtension"):
            return url
        case UIActivity.ActivityType(rawValue: "com.burbn.instagram.shareextension"):
            return nil
        case UIActivity.ActivityType(rawValue: "jp.naver.line.Share"):
            return url
        //case UIActivity.ActivityType(rawValue: "com.snapchat.Share"):
          //  return nil

        default:
            return url

        
    

【讨论】:

【参考方案2】:

在我的情况下,我总是想分享一张图片,但我还包含一条消息 (String),其中包含一个 URL 作为消息的一部分,当在某些允许它的平台上共享时。默认情况下,我不显示图像,因为如果消息包含 URL,某些应用程序会删除图像。

我为消息字符串和图像创建了两类UIActivityItemSource

class MessageActivityItemSource: NSObject, UIActivityItemSource 

    var message: String

    init(_ message: String) 
        self.message = message
    

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

    func activityViewController(_ activityViewController: UIActivityViewController,
                                itemForActivityType activityType: UIActivity.ActivityType?) -> Any? 
        switch activityType 
        case
            UIActivity.ActivityType.message,
            UIActivity.ActivityType.postToTwitter,
            UIActivity.ActivityType.mail,
            UIActivity.ActivityType.copyToPasteboard,
            UIActivity.ActivityType.discord,
            UIActivity.ActivityType.viber,
            UIActivity.ActivityType.telegram,
            UIActivity.ActivityType.line:
                return message

        default: return nil
        
    


class ImageActivityItemSource: NSObject, UIActivityItemSource 

    var image: UIImage

    init(_ image: UIImage) 
        self.image = image
    

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

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

我也有这个小扩展,所以我可以检测其他一些应用程序:

extension UIActivity.ActivityType 

    static let discord = UIActivity.ActivityType(rawValue: "com.hammerandchisel.discord.Share")
    static let viber = UIActivity.ActivityType(rawValue: "com.viber.app-share-extension")
    static let telegram = UIActivity.ActivityType(rawValue: "ph.telegra.Telegraph.Share")
    static let line = UIActivity.ActivityType(rawValue: "jp.naver.line.Share")

然后呈现共享屏幕:

let activityItems = [
    ImageActivityItemSource(someUIImage),
    MessageActivityItemSource("Some text with included URL https://someApp.com/")
]

let activityVC = UIActivityViewController(activityItems: activityItems, applicationActivities: nil)

activityVC.excludedActivityTypes = [
    .assignToContact,
    .addToReadingList,
    .print,
    .postToVimeo,
    .markupAsPDF,
    .postToFlickr
]

present(activityVC, animated: true, completion: nil)

【讨论】:

以上是关于如何从 UIActivityItemSource 函数返回多个值的主要内容,如果未能解决你的问题,请参考以下文章

UIActivityItemSource Protocole 设置复杂对象

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

自定义 UIActivity 的主题

多个 UIActivityViewController 占位符项目?

如何从其他面板从 JTextField 获取输入

如何从外部从 GitHub 加载 JavaScript 文件? [复制]