Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add subject on email by share UIActivityViewController in Swift

Tags:

ios

swift

I use UIActivityViewController to share data.

My code:

dispatch_async(dispatch_get_main_queue()) { () -> Void in
    let activityViewController : UIActivityViewController = UIActivityViewController(
        activityItems: [text!], applicationActivities: nil)
    // This line remove the arrow of the popover to show in iPad
    activityViewController.popoverPresentationController?.permittedArrowDirections = UIPopoverArrowDirection()
    activityViewController.popoverPresentationController?.sourceRect = CGRect(x: 150, y: 150, width: 0, height: 0)
    
    // Anything you want to exclude
    activityViewController.excludedActivityTypes = [
        UIActivityTypePostToWeibo,
        UIActivityTypePrint,
        UIActivityTypeAssignToContact,
        UIActivityTypeSaveToCameraRoll,
        UIActivityTypeAddToReadingList,
        UIActivityTypePostToFlickr,
        UIActivityTypePostToVimeo,
        UIActivityTypePostToTencentWeibo
    ]
    
    self.presentViewController(activityViewController, animated: true, completion: nil)            
}

I can show SMS, email, and notes, and I can add body text in SMS, email, and notes. But I have a question for email: I only add the body text in the email, but I also want to add a subject for the email. How can I do that?

like image 978
Coucou Avatar asked Jan 17 '17 11:01

Coucou


3 Answers

Just set value of subject to your title like this:

activityViewController.setValue("This is my title", forKey: "Subject")
like image 97
Nyakiba Avatar answered Oct 06 '22 02:10

Nyakiba


Use this line of code to add the subject in the mail, but this will add the subject in default iOS mail, not in the google mail.

let string : String = "Hello this is content to Body of my mail."

let activityViewController = UIActivityViewController(activityItems: [string], applicationActivities: nil)

activityViewController.setValue("Subject Title",forkey "subject")

// This line of code will support alertcontroller as popover for iPAd also.                   
 if let popoverController = activityViewController.popoverPresentationController {
           popoverController.sourceView = super.view  
}

navigationController?.present(activityViewController, animated: true){
                 }
like image 41
user3449185 Avatar answered Oct 06 '22 01:10

user3449185


Try this:

let obj = UIActivityViewController(activityItems: [Text], applicationActivities: [])
  
obj.setValue("Your Subject", forKey: "Subject")
like image 32
Prasann Avatar answered Oct 06 '22 02:10

Prasann