-1

I have an issue with the UIActivityViewController. In my app, I have a screen with a feedback section, and I want to show the activity popup when the user presses the "send feedback" button and then displays the correct subject, sender, and body message after choosing the mail app. Expected result:

  1. Show the activity popup.
  2. If the mail app is chosen(default ios mail app, Gmail, spark, etc.), show the correct recipe, subject, and mail body text.

My code shows the activity popup, but if you press any mail apps, the recipe, the subject, and the body text are empty.

Here is my code example:

@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)
    }
NexusUA
  • 217
  • 1
  • 3
  • 13
  • You should probably just use a `mailto:` URL if you exclude all the other activity types. – Sweeper Aug 04 '23 at 09:36
  • 1
    Combining activity view controller with mail compose view controller makes no sense. – matt Aug 04 '23 at 09:37
  • @matt so, how should it be? – NexusUA Aug 04 '23 at 09:38
  • @Sweeper, if I use mailto:, my user will leave my app. That's not a good idea – NexusUA Aug 04 '23 at 13:37
  • The activity controller's completion handler is called *after* the user has already sent the email (assuming they choose Mail). Having code that then launches `MFMailComposeViewController` makes no sense. The user will end up being asked to send a 2nd email. – HangarRash Aug 04 '23 at 16:54
  • @HangarRash is it possible to implement how I want? – NexusUA Aug 07 '23 at 08:25

0 Answers0