限制共享联系人时打开的内容

Limit what opens appear while sharing contact

我想在我的应用程序中共享联系人,但我只想让用户通过“消息”和“邮件”进行。我可以在警报 sheet 上屏蔽所有其他选项吗?

func shareContacts(contacts: [CNContact]) throws {

        guard let directoryURL = FileManager.default.urls(for: .cachesDirectory, in: .userDomainMask).first else {
            return
        }

        var filename = NSUUID().uuidString

        // Create a human friendly file name if sharing a single contact.
        if let contact = contacts.first, contacts.count == 1 {

            if let fullname = CNContactFormatter().string(from: contact) {
                filename = fullname.components(separatedBy:" ").joined(separator: "")
            }
        }

        let fileURL = directoryURL
            .appendingPathComponent(filename)
            .appendingPathExtension("vcf")

        let data = try CNContactVCardSerialization.data(with: contacts)

        try data.write(to:fileURL, options: [.atomicWrite])
         let textToShare = "This is my clear captions text test"
         let objectsToShare = [textToShare, fileURL] as [Any]
                let activityViewController = UIActivityViewController(
                    activityItems: objectsToShare,
                    applicationActivities: nil
                )

        present(activityViewController, animated: true, completion: {})
    }

不可能简单地排除邮件和 iMessage 之外的所有内容,但您可以执行以下操作。

您可以使用功能排除 UIActivityViewController 的选项,但您只能禁用部分应用程序。要禁用更多,您需要一个私有 API,并且您会违反 Apple 对所有 iOS 个应用程序制定的应用程序指南。

您可以禁用这些类型:

UIActivityTypePostToFacebook, 
UIActivityTypePostToTwitter, 
UIActivityTypePostToWeibo, 
UIActivityTypeMessage, 
UIActivityTypeMail, 
UIActivityTypePrint, 
UIActivityTypeCopyToPasteboard, 
UIActivityTypeAssignToContact,
UIActivityTypeSaveToCameraRoll,
UIActivityTypeAddToReadingList,
UIActivityTypePostToFlickr,
UIActivityTypePostToVimeo,
UIActivityTypePostToTencentWeibo,
UIActivityTypeAirDrop

通过使用此代码(Xcode 向您建议确切的类型):

activityController.excludedActivityTypes = [
    UIActivityType.assignToContact,

    // ... and all of the types you want to disable

    // If you know the rawValue/BundleID of other types you can try to disable them too like this
    UIActivityType(rawValue: "..."),
]

Apple Documentation about UIActivityViewController

看看这个问题: