如何从 UIActivityViewController 为邮件应用程序设置发件人、主题和正文按摩?

问题描述 投票:0回答:0

我对

UIActivityViewController
有疑问。 在我的应用程序中,我有一个带有反馈部分的屏幕,我想在用户按下“发送反馈”按钮时显示活动弹出窗口,然后在选择邮件应用程序后显示正确的主题、发件人和正文消息。 这是我的代码示例:

@objc func buttonClicked() {
        button.setTitleColor(.gray, for: .normal)
        
        DispatchQueue.main.asyncAfter(deadline: .now() + 0.2) {
            self.button.setTitleColor(.white, for: .normal)
        }
        
        // Additional actions or logic when the button is clicked
        let feedbackText = "feedback"
        let userId = "userId"
        let subject = "subject"
        guard let currentUserEmail = getModule(UserProfileService.self).currentUser?.emailAddress else {
            return
        }
        
        // Create the activity view controller
        let activityViewController = UIActivityViewController(activityItems: [feedbackText], applicationActivities: nil)
        
        // Exclude all apps except for email
        activityViewController.excludedActivityTypes = [
            .addToReadingList,
            .airDrop,
            .assignToContact,
            .copyToPasteboard,
            .message,
            .openInIBooks,
            .postToFacebook,
            .postToFlickr,
            .postToTencentWeibo,
            .postToTwitter,
            .postToVimeo,
            .postToWeibo,
            .print,
            .saveToCameraRoll
        ]
        
        activityViewController.completionWithItemsHandler = { [weak self] (activityType, completed, _, _) in
            if completed, activityType == .mail {
                if MFMailComposeViewController.canSendMail() {
                    let composeVC = MFMailComposeViewController()
                    composeVC.mailComposeDelegate = self
                    
                    // Configure the fields of the interface
                    composeVC.setToRecipients([UserProfileViewController.feedbackEmail])
                    composeVC.setSubject("subject")
                    composeVC.setMessageBody("message")", isHTML: true)
                    
                    if let userId = self?.viewModel.userProfileService.currentUser?.id {
                        let subject = "UserId: \(userId)"
                        composeVC.setSubject("subject")
                    }
                    
                    self?.present(composeVC, animated: true)
                }
            }
        }
        present(activityViewController, animated: true, completion: nil)
    }
swift uikit uiactivityviewcontroller
© www.soinside.com 2019 - 2024. All rights reserved.