I'm trying to send an email with a photo attachment without showing the editor. Eventually I'm trying to make it so when someone logs into the app it will send them an email automatically.
My implementation:
if let videoConnection = stillImageOutput!.connectionWithMediaType(AVMediaTypeVideo) {
videoConnection.videoOrientation = AVCaptureVideoOrientation.Portrait
//initiates a still image and returns
//samplebugger data was captured
stillImageOutput?.captureStillImageAsynchronouslyFromConnection(videoConnection, completionHandler: {(sampleBuffer, error) in
if (sampleBuffer != nil) {
var imageData = AVCaptureStillImageOutput.jpegStillImageNSDataRepresentation(sampleBuffer)
//creates core graphics image
var dataProvider = CGDataProviderCreateWithCFData(imageData)
var cgImageRef = CGImageCreateWithJPEGDataProvider(dataProvider, nil, true, kCGRenderingIntentDefault)
var image = UIImage(CGImage: cgImageRef, scale: 1.0, orientation: UIImageOrientation.Right)
//saves image after taken
UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil)
var myController = MFMailComposeViewController()
myController.mailComposeDelegate = self
myController.setSubject(" Your Sherpa Photo")
myController.setMessageBody("hello World", isHTML: false)
myController.setToRecipients(["[email protected]"])
var emailimageData = UIImagePNGRepresentation(image)
myController.addAttachmentData(emailimageData, mimeType: "image/png", fileName: "image")
self.presentViewController(myController, animated: true, completion: nil)
}
})
}
// ...
func mailComposeController(controller: MFMailComposeViewController!, didFinishWithResult result: MFMailComposeResult, error: NSError!) {
if result.value == MFMailComposeResultSent.value {
let alertView = UIAlertView()
alertView.message = "Mail Sent!"
alertView.addButtonWithTitle("OK")
alertView.show()
}
self.dismissViewControllerAnimated(false, completion: nil)
}
Short answer: You can't do that, by design. The only way you can send email without display a compose mail view controller is if you have a server that offers mail services, but you will have to collect the user's mail credentials.
Apple does not want 3rd party developers sending email from a user's account with the user being the one to click the button and do the sending. This protects the user from apps sending mail without the user's knowledge or permission.
Imagine the explosion of spam if this restriction was not in place.