【问题标题】:UIActivities in Swift 3Swift 3 中的 UIActivity
【发布时间】:2016-09-20 16:56:57
【问题描述】:

尝试在我的应用程序中重新集成共享按钮。 Apple 再次将 Swift 3 中的内容更改为 iOS 10。我刚刚更新到Xcode 8 发布版本,它给我带来了UIActivities 的一些问题。

发现其中许多现在以UIActivityType. 开头,以使编译器不会失败。然而,他们中的许多人仍在使用com.apple,这也让我出于某种原因将activityType 数组转换为[Any]

那么……有谁知道他们为什么这样做?你在 activityTypesToExclude.contains(where: (Any) throws -> Bool) 方法的 for 参数中放了什么?

 internal func _shouldExcludeActivityType(_ activity: UIActivity) -> Bool {

            let activityTypesToExclude = [

                "com.apple.reminders.RemindersEditorExtension",
                UIActivityType.openInIBooks,
                UIActivityType.print,
                UIActivityType.assignToContact,
                UIActivityType.postToWeibo,
                "com.google.Drive.ShareExtension",
                "com.apple.mobileslideshow.StreamShareService"

            ] as [Any]

            if let actType = activity.activityType {
                if activityTypesToExclude.contains(where: (Any) throws -> Bool) {
                    return true
                }
                else if super.excludedActivityTypes != nil {
                    return super.excludedActivityTypes!.contains(actType)
                }
            }
            return false
}

【问题讨论】:

    标签: ios swift swift3


    【解决方案1】:

    UIActivityType 是一个类型安全的瘦包装器,用于表示活动类型的字符串。如果您发现一些没有预定义常量的活动类型,您可以使用扩展定义自己的。

    extension UIActivityType {
        static let remindersEditorExtension = UIActivityType(rawValue: "com.apple.reminders.RemindersEditorExtension")
        static let googleDriveShareExtension = UIActivityType(rawValue: "com.google.Drive.ShareExtension")
        static let streamShareService = UIActivityType(rawValue: "com.apple.mobileslideshow.StreamShareService")
    }
    

    使用上面的扩展,你可以这样写:

    internal func _shouldExcludeActivityType(_ activity: UIActivity) -> Bool {
    
        let activityTypesToExclude: [UIActivityType] = [
            .remindersEditorExtension,
            .openInIBooks,
            .print,
            .assignToContact,
            .postToWeibo,
            .googleDriveShareExtension,
            .streamShareService
        ]
    
        if let actType = activity.activityType {
            if activityTypesToExclude.contains(actType) {
                return true
            } else if let superExcludes = super.excludedActivityTypes {
                return superExcludes.contains(actType)
            }
        }
        return false
    }
    

    您不应该直接在 Swift 3 代码中使用原始字符串表示。

    那么,关于他们为什么这样做?:通过这种变化,程序员在需要 activityType 标识符的地方意外包含非 activityType 字符串的机会要少得多。

    对一些字符串常量进行了类似的更改,例如Notification.Name

    【讨论】:

    • 我对如何使用这个功能有点困惑。我试过activityViewController.excludedActivityTypes = [UIActivityType.streamShareService],但它不起作用。你能解释一下如何使用函数_shouldExcludeActivityType,以便我可以排除com.apple.mobileslideshow.StreamShareService
    • 首先,检查this thread。 (也许您已经检查过它,但再次仔细检查。)正如接受的答案中所写,_shouldExcludeActivityType 是一个私有 API,使用此 API 的应用程序有被 Apple 拒绝的风险。而且苹果会随时更改一些详细的实现而不需要通知。您可以自担风险尝试它们,但我无法解释“如何使用它”,因为我不知道实际可行的方法。上面的答案只是解释了如何创建UIActivityType的实例。
    猜你喜欢
    • 2017-01-02
    • 2015-04-29
    • 2018-08-30
    • 2020-11-07
    • 1970-01-01
    • 2017-01-18
    • 2017-07-26
    • 2017-04-02
    • 2017-02-13
    相关资源
    最近更新 更多