উত্তর:
আপনি মেল অ্যাপটি খুলতে সাধারণ মেলটো: আইওএসের লিঙ্কগুলি ব্যবহার করতে পারেন।
let email = "foo@bar.com"
if let url = URL(string: "mailto:\(email)") {
if #available(iOS 10.0, *) {
UIApplication.shared.open(url)
} else {
UIApplication.shared.openURL(url)
}
}
অন্যান্য উত্তরগুলি সঠিক থাকলেও আপনি কখনই জানতে পারবেন না যে আপনার আইফোন / আইপ্যাড যে অ্যাপ্লিকেশনটি চালাচ্ছে অ্যাপলের মেল অ্যাপ্লিকেশনটি ইনস্টল আছে কি না তা ব্যবহারকারী দ্বারা মুছে ফেলা যায় কিনা।
একাধিক ইমেল ক্লায়েন্টকে সমর্থন করা ভাল। নিম্নলিখিত কোডটি আরও কৃপণভাবে ইমেল প্রেরণাকে পরিচালনা করে। কোডের প্রবাহটি হ'ল:
mailto:..ফ্যালব্যাক যা ব্যবহারকারীকে অ্যাপলের মেল অ্যাপ্লিকেশনটি ইনস্টল করতে অনুরোধ করে।কোড সুইফট 5 এ লিখিত হয়েছে :
import MessageUI
import UIKit
class SendEmailViewController: UIViewController, MFMailComposeViewControllerDelegate {
@IBAction func sendEmail(_ sender: UIButton) {
// Modify following variables with your text / recipient
let recipientEmail = "test@email.com"
let subject = "Multi client email support"
let body = "This code supports sending email via multiple different email apps on iOS! :)"
// Show default mail composer
if MFMailComposeViewController.canSendMail() {
let mail = MFMailComposeViewController()
mail.mailComposeDelegate = self
mail.setToRecipients([recipientEmail])
mail.setSubject(subject)
mail.setMessageBody(body, isHTML: false)
present(mail, animated: true)
// Show third party email composer if default Mail app is not present
} else if let emailUrl = createEmailUrl(to: recipientEmail, subject: subject, body: body) {
UIApplication.shared.open(emailUrl)
}
}
private func createEmailUrl(to: String, subject: String, body: String) -> URL? {
let subjectEncoded = subject.addingPercentEncoding(withAllowedCharacters: .urlHostAllowed)!
let bodyEncoded = body.addingPercentEncoding(withAllowedCharacters: .urlHostAllowed)!
let gmailUrl = URL(string: "googlegmail://co?to=\(to)&subject=\(subjectEncoded)&body=\(bodyEncoded)")
let outlookUrl = URL(string: "ms-outlook://compose?to=\(to)&subject=\(subjectEncoded)")
let yahooMail = URL(string: "ymail://mail/compose?to=\(to)&subject=\(subjectEncoded)&body=\(bodyEncoded)")
let sparkUrl = URL(string: "readdle-spark://compose?recipient=\(to)&subject=\(subjectEncoded)&body=\(bodyEncoded)")
let defaultUrl = URL(string: "mailto:\(to)?subject=\(subjectEncoded)&body=\(bodyEncoded)")
if let gmailUrl = gmailUrl, UIApplication.shared.canOpenURL(gmailUrl) {
return gmailUrl
} else if let outlookUrl = outlookUrl, UIApplication.shared.canOpenURL(outlookUrl) {
return outlookUrl
} else if let yahooMail = yahooMail, UIApplication.shared.canOpenURL(yahooMail) {
return yahooMail
} else if let sparkUrl = sparkUrl, UIApplication.shared.canOpenURL(sparkUrl) {
return sparkUrl
}
return defaultUrl
}
func mailComposeController(_ controller: MFMailComposeViewController, didFinishWith result: MFMailComposeResult, error: Error?) {
controller.dismiss(animated: true)
}
}
দয়া করে নোট করুন যে আমি ইচ্ছাকৃতভাবে আউটলুক অ্যাপ্লিকেশনটির জন্য শরীরটি হারিয়েছি, কারণ এটি এটি বিশ্লেষণ করতে সক্ষম নয়।
Info.plistইউআরএল কোয়েরি স্কিমগুলি ব্যবহার করা হয় এমন ফাইলগুলিতে ফাইল করার জন্য আপনাকে নীচের কোডটি যুক্ত করতে হবে।
<key>LSApplicationQueriesSchemes</key>
<array>
<string>googlegmail</string>
<string>ms-outlook</string>
<string>readdle-spark</string>
<string>ymail</string>
</array>
isHTMLউপরের কোডটি সত্য হিসাবে সেট করে আপনি ডিফল্ট মেল সুরকারের জন্য এইচটিএমএল সমর্থন করতে পারেন । অন্যান্য ক্লায়েন্টদের জন্য, এটি সম্ভব হতে জন্য আরো পড়ার দেখতে মনে হচ্ছে না stackoverflow.com/questions/5620324/mailto-link-with-html-body
আমি নিশ্চিত নই যে আপনি নিজেই মেল অ্যাপটিতে স্যুইচ করতে চান বা কেবল ইমেল খুলুন এবং প্রেরণ করতে চান। একটি বোতামের সাথে সংযুক্ত পরবর্তী বিকল্পগুলির জন্য আইবিএક્શન:
import UIKit
import MessageUI
class ViewController: UIViewController, MFMailComposeViewControllerDelegate {
@IBAction func launchEmail(sender: AnyObject) {
var emailTitle = "Feedback"
var messageBody = "Feature request or bug report?"
var toRecipents = ["friend@stackoverflow.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)
}
}
class myClass: UIViewController, MFMailComposeViewControllerDelegate {
'NSInvalidArgumentException', reason: 'Application tried to present a nil modal view controller on target। অ্যাপ্লিকেশন কিছু ডিভাইসে ক্র্যাশ করেছে (আইফোন 5, আইফোন 6 এবং আইপ্যাড মিনি)
সুইফ্ট 3-এ আপনি প্রোটোকলের import MessageUIসাথে সংযোগ স্থাপনের প্রয়োজনীয়তা নিশ্চিত করেছেন MFMailComposeViewControllerDelegate।
func sendEmail() {
if MFMailComposeViewController.canSendMail() {
let mail = MFMailComposeViewController()
mail.mailComposeDelegate = self
mail.setToRecipients(["ved.ios@yopmail.com"])
mail.setMessageBody("<p>You're so awesome!</p>", isHTML: true)
present(mail, animated: true)
} else {
// show failure alert
}
}
প্রোটোকল:
func mailComposeController(_ controller: MFMailComposeViewController, didFinishWith result: MFMailComposeResult, error: Error?) {
controller.dismiss(animated: true)
}
সুইফট 4.2+ এবং আইওএস 9+ এর জন্য
let appURL = URL(string: "mailto:TEST@EXAMPLE.COM")!
if #available(iOS 10.0, *) {
UIApplication.shared.open(appURL, options: [:], completionHandler: nil)
} else {
UIApplication.shared.openURL(appURL)
}
আপনার পছন্দসই ইমেল ঠিকানা দিয়ে TEST@EXAMPLE.COM প্রতিস্থাপন করুন।
প্রাপ্যতা যাচাই সহ সুইফট 2 :
import MessageUI
if MFMailComposeViewController.canSendMail() {
let mail = MFMailComposeViewController()
mail.mailComposeDelegate = self
mail.setToRecipients(["test@test.test"])
mail.setSubject("Bla")
mail.setMessageBody("<b>Blabla</b>", isHTML: true)
presentViewController(mail, animated: true, completion: nil)
} else {
print("Cannot send mail")
// give feedback to the user
}
// MARK: - MFMailComposeViewControllerDelegate
func mailComposeController(controller: MFMailComposeViewController, didFinishWithResult result: MFMailComposeResult, error: NSError?) {
switch result.rawValue {
case MFMailComposeResultCancelled.rawValue:
print("Cancelled")
case MFMailComposeResultSaved.rawValue:
print("Saved")
case MFMailComposeResultSent.rawValue:
print("Sent")
case MFMailComposeResultFailed.rawValue:
print("Error: \(error?.localizedDescription)")
default:
break
}
controller.dismissViewControllerAnimated(true, completion: nil)
}
সুইফট 4 এর জন্য এটি কীভাবে দেখায়:
import MessageUI
if MFMailComposeViewController.canSendMail() {
let mail = MFMailComposeViewController()
mail.mailComposeDelegate = self
mail.setToRecipients(["test@test.test"])
mail.setSubject("Bla")
mail.setMessageBody("<b>Blabla</b>", isHTML: true)
present(mail, animated: true, completion: nil)
} else {
print("Cannot send mail")
// give feedback to the user
}
func mailComposeController(_ controller: MFMailComposeViewController, didFinishWith result: MFMailComposeResult, error: Error?) {
switch result.rawValue {
case MFMailComposeResult.cancelled.rawValue:
print("Cancelled")
case MFMailComposeResult.saved.rawValue:
print("Saved")
case MFMailComposeResult.sent.rawValue:
print("Sent")
case MFMailComposeResult.failed.rawValue:
print("Error: \(String(describing: error?.localizedDescription))")
default:
break
}
controller.dismiss(animated: true, completion: nil)
}
সুইফট 3 এর জন্য স্টিফেন গুরুর থেকে উত্তর উত্তর answer
let email = "email@email.com"
let url = URL(string: "mailto:\(email)")
UIApplication.shared.openURL(url!)
আপনি যদি কেবল কোনও মেইলে ক্লায়েন্টকে এটির মাধ্যমে খুলতে চান তবে এখানে সুইফট 4-এর একটি আপডেট রয়েছে URL:
let email = "foo@bar.com"
if let url = URL(string: "mailto:\(email)") {
UIApplication.shared.open(url, options: [:], completionHandler: nil)
}
এটি আমার জন্য পুরোপুরি সূক্ষ্ম কাজ করেছে :)
এটি সুইফটে 3 টি ধাপের একটি সরাসরি সমাধান।
import MessageUI
প্রতিনিধি অনুসারে যুক্ত করুন
MFMailComposeViewControllerDelegate
এবং কেবল আপনার পদ্ধতিটি তৈরি করুন:
func sendEmail() {
if MFMailComposeViewController.canSendMail() {
let mail = MFMailComposeViewController()
mail.mailComposeDelegate = self
mail.setToRecipients(["support@mail.com"])
mail.setSubject("Support App")
mail.setMessageBody("<p>Send us your issue!</p>", isHTML: true)
presentViewController(mail, animated: true, completion: nil)
} else {
// show failure alert
}
}
func mailComposeController(controller: MFMailComposeViewController, didFinishWithResult result: MFMailComposeResult, error: NSError?) {
controller.dismissViewControllerAnimated(true, completion: nil)
}
আপনার অন্তর্নির্মিত মেল সুরকারের সাথে পাঠানোর চেষ্টা করা উচিত এবং যদি এটি ব্যর্থ হয় তবে ভাগ করে চেষ্টা করুন:
func contactUs() {
let email = "info@example.com" // insert your email here
let subject = "your subject goes here"
let bodyText = "your body text goes here"
// https://developer.apple.com/documentation/messageui/mfmailcomposeviewcontroller
if MFMailComposeViewController.canSendMail() {
let mailComposerVC = MFMailComposeViewController()
mailComposerVC.mailComposeDelegate = self as? MFMailComposeViewControllerDelegate
mailComposerVC.setToRecipients([email])
mailComposerVC.setSubject(subject)
mailComposerVC.setMessageBody(bodyText, isHTML: false)
self.present(mailComposerVC, animated: true, completion: nil)
} else {
print("Device not configured to send emails, trying with share ...")
let coded = "mailto:\(email)?subject=\(subject)&body=\(bodyText)".addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)
if let emailURL = URL(string: coded!) {
if #available(iOS 10.0, *) {
if UIApplication.shared.canOpenURL(emailURL) {
UIApplication.shared.open(emailURL, options: [:], completionHandler: { (result) in
if !result {
print("Unable to send email.")
}
})
}
}
else {
UIApplication.shared.openURL(emailURL as URL)
}
}
}
}
@IBAction func launchEmail(sender: AnyObject) {
if if MFMailComposeViewController.canSendMail() {
var emailTitle = "Feedback"
var messageBody = "Feature request or bug report?"
var toRecipents = ["friend@stackoverflow.com"]
var mc: MFMailComposeViewController = MFMailComposeViewController()
mc.mailComposeDelegate = self
mc.setSubject(emailTitle)
mc.setMessageBody(messageBody, isHTML: false)
mc.setToRecipients(toRecipents)
self.present(mc, animated: true, completion: nil)
} else {
// show failure alert
}
}
func mailComposeController(controller:MFMailComposeViewController, didFinishWithResult result:MFMailComposeResult, error:NSError) {
switch result {
case .cancelled:
print("Mail cancelled")
case .saved:
print("Mail saved")
case .sent:
print("Mail sent")
case .failed:
print("Mail sent failure: \(error?.localizedDescription)")
default:
break
}
self.dismiss(animated: true, completion: nil)
}
নোট করুন যে সমস্ত ব্যবহারকারীর ইমেল প্রেরণের জন্য তাদের ডিভাইস কনফিগার করে না, এজন্য প্রেরণের চেষ্টা করার আগে আমাদের ক্যানসেন্ডমেল () এর ফলাফল পরীক্ষা করা দরকার। এছাড়াও নোট করুন যে মেল উইন্ডোটি খারিজ করার জন্য আপনাকে ডিড ফিনিশওয়েথ কলব্যাকটি ধরতে হবে।
ভিউ কন্ট্রোলারে যেখান থেকে আপনি নিজের মেইল-অ্যাপ্লিকেশনটি ট্যাপটিতে খুলতে চান।
এই ফাংশনটি আপনার নিয়ামকের ভিতরে রাখুন।
func showMailComposer(){
guard MFMailComposeViewController.canSendMail() else {
return
}
let composer = MFMailComposeViewController()
composer.mailComposeDelegate = self
composer.setToRecipients(["abc@gmail.com"]) // email id of the recipient
composer.setSubject("testing!!!")
composer.setMessageBody("this is a test mail.", isHTML: false)
present(composer, animated: true, completion: nil)
}আপনার ভিউ কন্ট্রোলার প্রসারিত করুন এবং MFMailComposeViewControllerDelegate অনুসারে ।
আপনার মেইল প্রেরণে, এই পদ্ধতিটি রাখুন এবং ব্যর্থতাটি পরিচালনা করুন।
func mailComposeController(_ controller: MFMailComposeViewController, didFinishWith result: MFMailComposeResult, error: Error?) {
if let _ = error {
controller.dismiss(animated: true, completion: nil)
return
}
controller.dismiss(animated: true, completion: nil)
}উপরের দিকে সুইফট 4.2 এবং এর জন্য
let supportEmail = "abc@xyz.com"
if let emailURL = URL(string: "mailto:\(supportEmail)"), UIApplication.shared.canOpenURL(emailURL)
{
UIApplication.shared.open(emailURL, options: [:], completionHandler: nil)
}
ব্যবহারকারীকে ইমেল প্রেরণের জন্য অনেকগুলি মেল বিকল্প (যেমন আইক্লাউড, গুগল, ইয়াহু, আউটলুক ডটকম - কোনও মেল যদি তার ফোনে প্রাক-কনফিগার করা না থাকে) চয়ন করতে দিন।