2016-11-24 77 views
2

我的應用程序可以打開PDF文件。所以我將它註冊爲PDF文件。當我從郵件應用程序打開PDF文件時,我的應用程序會被調用,並且會調用以下功能:回到上一個應用程序(Swift ios)

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!如果用戶點擊ok按鈕,我想回到郵件應用程序。我怎樣才能做到這一點?我讀過,那個蘋果不允許你在應用程序之間切換。但是WhatsApp正在這樣做。

我取出的viewController試了一下:

viewController.view.removeFromSuperview() 

但後來我只得到一個黑色的屏幕。

+0

這不會幫助你回到郵件應用程序viewController.view.removeFromSuperview() – Hasya

+0

這不像你的答案幫助我:)。你有解決這個問題的辦法嗎? – ReasyEasyPeasy

回答

-1

我用下面的代碼做它:

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

那是不夠好,對我來說。

謝謝@Jeremy你的鏈接幫助了我!

1

啓動其他應用程序的唯一方法是使用它們的URL方案,打開郵件的唯一方法是使用mailto:方案,該方案將始終打開撰寫視圖。

let email = "[email protected]" 

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

編輯:此answer可以幫助你

+0

那麼Whatsapp是如何做的呢? – ReasyEasyPeasy

+0

我編輯我的答案,檢查鏈接 – Jeremy

-1

這不會幫助你回去郵件應用viewController.view.removeFromSuperview()

它只會刪除您viewcontroler和黑色window是顯示了意見。

即使您使用openURL,也無法回到郵件應用程序。

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

這將打開電子郵件作曲家,但實際上你將無法回到郵件應用程序。

斯威夫特

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

與發件人爲[email protected]這將打開郵件的作曲家。

如果你想附加文件並通過電子郵件作曲家發送,然後像下面的代碼。

進口的UIKit 進口MessageUI

class ViewController: UIViewController, MFMailComposeViewControllerDelegate { 

@IBAction func launchEmail(sender: AnyObject) { 

var emailTitle = "EmailTitle" 
var messageBody = "Email BodyFeature request or bug report?" 
var toRecipents = ["[email protected]"] 
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) 
} 

} 
+1

嗯,但WhatsApp是如何做到這一點? – ReasyEasyPeasy

+0

僅打開電子郵件編輯器。不會重定向回郵件應用程序。 http://stackoverflow.com/questions/8821934/launch-apple-mail-app-from-within-my-own-app – Hasya

+0

那不是真的。如果我使用WhatsApp從郵件應用程序打開文件,我可以將此文件發送給聯繫人。將此文件發送給WhatsApp關閉的聯繫人後,您會再次看到我之前打開的郵件(使用該文件)。不是電子郵件作曲家 – ReasyEasyPeasy

相關問題