2

My application can open PDF-files. So I registered it for PDF-files. When I open a PDF-file from the mail-app my application get called and the following function is called:

func application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any] = [:]) -> Bool {
        let rect = app.keyWindow?.rootViewController?.view.bounds
        viewController.view.backgroundColor = UIColor.red
        let picker = UIPickerView(frame: CGRect(x: 0, y: 100, width: (rect?.width)!, height: (rect?.height)! / 3))
        picker.delegate = self
        picker.dataSource = self
        viewController.view.addSubview(picker)
        let okButton = UIButton(frame: CGRect(x: 0, y: 100 + ((rect?.height)! / 3), width: ((rect?.width)! / 2) - 20, height: 30))
        okButton.setTitle("Ok", for: .normal)
        okButton.addTarget(self, action: #selector(AppDelegate.endApp), for: .touchUpInside)
        viewController.view.addSubview(okButton)
        app.keyWindow?.rootViewController?.present(viewController, animated: true, completion: nil)

        return true
    }

This works! If the user click on the ok Button I want to go back to the mail-app. How can I do this? I read, that apple don't allow you to switch between apps. But whatsApp is doing this.

I tried it with removing the viewController:

viewController.view.removeFromSuperview()

But then I only get a black screen.

Hasya
  • 9,792
  • 4
  • 31
  • 46
SamuelTJackson
  • 1,357
  • 3
  • 19
  • 40

3 Answers3

1

The only way to launch other applications is by using their URL schemes, the only way to open mail is by using the mailto: scheme, which will always open the compose view.

let email = "foo@bar.com"

let url = URL(string: "mailto:\(email)")
UIApplication.sharedApplication().openURL(url)

Edit: this answer might help you

Community
  • 1
  • 1
Jeremy
  • 1,461
  • 12
  • 26
-1

This will not help you to go back to mail app viewController.view.removeFromSuperview()

It will just remove views from your viewcontroler and black color window is showing.

There is no way to get back to mail-app even if you use openURL.

[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"mailto:"]];

This will open email composer, but practically you will not be able to get back to mail app.

Swift

let url = URL(string: "mailto:\abc@xyz.com")
UIApplication.sharedApplication().openURL(url)

This will open email composer with sender as abc@xyz.com.

If you want to attach file and send it through email composer, then make like below code.

import UIKit import MessageUI

class ViewController: UIViewController, MFMailComposeViewControllerDelegate {

@IBAction func launchEmail(sender: AnyObject) {

var emailTitle = "EmailTitle"
var messageBody = "Email BodyFeature request or bug report?"
var toRecipents = ["abc@xyz.com"]
var mc: MFMailComposeViewController = MFMailComposeViewController()
mc.mailComposeDelegate = self
mc.setSubject(emailTitle)
mc.setMessageBody(messageBody, isHTML: false)
mc.setToRecipients(toRecipents)

self.presentViewController(mc, animated: true, completion: nil)
}

func mailComposeController(controller:MFMailComposeViewController, didFinishWithResult result:MFMailComposeResult, error:NSError) {
    switch result {
    case MFMailComposeResultCancelled:
        print("Mail cancelled")
    case MFMailComposeResultSaved:
        print("Mail saved")
    case MFMailComposeResultSent:
        print("Mail sent")
    case MFMailComposeResultFailed:
        print("Mail sent failure: \(error?.localizedDescription)")
    default:
        break
    }
    self.dismissViewControllerAnimated(true, completion: nil)
}

}
Hasya
  • 9,792
  • 4
  • 31
  • 46
  • 1
    Well but how is Whatsapp doing it? – SamuelTJackson Nov 24 '16 at 13:40
  • That opens email composer only. doesnt redirect back to mail app. http://stackoverflow.com/questions/8821934/launch-apple-mail-app-from-within-my-own-app – Hasya Nov 24 '16 at 13:42
  • Thats not true. If I open a file from the mail application with whatsapp I can send this file to a contact. After I send this file to a contact WhatsApp is closed and you see the mail I opended before(with the file) again. Not the email composer – SamuelTJackson Nov 24 '16 at 13:44
  • Try out. If you find a way, kindly update as answer in your question. – Hasya Nov 24 '16 at 13:51
-2

I made it with the following code:

viewController.dismiss(animated: false, completion: nil)   
let mailURL = NSURL(string: "message://")!
        if UIApplication.shared.canOpenURL(mailURL as URL) {
            UIApplication.shared.openURL(mailURL as URL)
        }

Thats good enough for me.

thank you @Jeremy your link helped me!

SamuelTJackson
  • 1,357
  • 3
  • 19
  • 40