如何从UIActivityItemSource函数返回多个值

How to return multiple values from UIActivityItemSource function(如何从UIActivityItemSource函数返回多个值)

本文介绍了如何从UIActivityItemSource函数返回多个值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以从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类?并像这样将它们分配给活动项?:

let 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)

................
    }

推荐答案

这就是我昨晚做的。不过,就适当的编码风格而言,我不确定这种方式是否有效。我也不确定我是否真正理解了ActivitySourceItems的工作方式。也许有人可以用更有效的方式来写它。

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

谢谢你的链接,拉姆。我以前的搜索没有显示这些帖子。

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

        }
    }
}

这篇关于如何从UIActivityItemSource函数返回多个值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:如何从UIActivityItemSource函数返回多个值

基础教程推荐