2016-11-30 8 views
29

मैं ईमेल विकल्प भेजने के साथ एक ऐप स्थापित करने की कोशिश कर रहा हूं।तेजी से एक ईमेल भेजना 3

import Foundation 
import MessageUI 
import UIKit 

class emailClass: UIViewController, MFMailComposeViewControllerDelegate { 
    override func viewDidLoad() { 
     super.viewDidLoad() 

     if !MFMailComposeViewController.canSendMail() { 
      print("Mail services are not available") 
      return 
     }   
     sendEmail() 
    } 

    func sendEmail() {  
     let composeVC = MFMailComposeViewController() 
     composeVC.mailComposeDelegate = self 
     // Configure the fields of the interface. 
     composeVC.setToRecipients(["[email protected]"]) 
     composeVC.setSubject("Hello!") 
     composeVC.setMessageBody("Hello this is my message body!", isHTML: false) 
     // Present the view controller modally. 
     self.present(composeVC, animated: true, completion: nil) 
    } 

    func mailComposeController(controller: MFMailComposeViewController, 
          didFinishWithResult result: MFMailComposeResult, error: NSError?) { 
     // Check the result or perform other tasks. 
     // Dismiss the mail compose view controller. 
     controller.dismiss(animated: true, completion: nil) 
    } 

    override func didReceiveMemoryWarning() { 
     super.didReceiveMemoryWarning() 
     // Dispose of any resources that can be recreated. 
    } 
} 

तो मैं यह संदेश प्राप्त: "मेल सेवाओं उपलब्ध नहीं हैं"

मैं इस कोड है। अब मैंने iCloud में सिम्युलेटर डिवाइस में लॉग इन किया है ... तो मुझे लगता है कि इसे करना चाहिए लेकिन यह नहीं है। यह क्यों काम नहीं कर रहा है? क्या आप मुझे बता सकते हैं कि क्या गलत है और मैं आगे कैसे बढ़ सकता हूं?

+0

आपको एक ईमेल पते के साथ अपने डिवाइस को कॉन्फ़िगर किया? यदि नहीं, तो यह करें ... और यह आपकी समस्या का समाधान कर सकता है। –

+0

मैं सिम्युलेटर का उपयोग कर रहा हूँ। मैं डिवाइस को ईमेल पते से कैसे कॉन्फ़िगर कर सकता हूं? जब मैं सेटिंग्स पर जाता हूं तो मुझे विकल्प "ईमेल" नहीं दिख रहा है .... –

+0

मैं बात करता हूं, आपने इस समस्या को हल कर लिया है .. इस बीच यहां आईओएस डिवाइस के लिए एक ईमेल कॉन्फ़िगर करने के लिए लिंक है ... https: // support। apple.com/en-in/HT201320 –

उत्तर

29

यह सिम्युलेटर के साथ काम नहीं करेगा। कृपया आईफोन डिवाइस पर इसका परीक्षण करें। आप उल्लेख कर सकते हैं Apple Developer Portal - MFMailComposeViewController

+0

हां। मैं 100% निश्चित हूँ।मैं एक ही प्रकार के ऐप पर काम कर रहा हूं और इसे पाया। –

+0

ठीक है धन्यवाद :) लेकिन मैंने जो कोड ऊपर लिखा है। ठीक है न? –

+0

हां। कोड ठीक लगता है और काम करना चाहिए। क्या आपके पास कोई आईफोन नहीं है? –

4

कोड अच्छा प्रतीत हो रहा है और ठीक काम करता है, तो अनुप्रयोग एक असली डिवाइस

MFMailComposeViewController.canSendMail() // returns false for simulators. 

आप सिम्युलेटर पर यह परीक्षण नहीं कर सकते में चल रहा है, आप बुनियादी बातें परीक्षण करने के लिए सक्षम हो जाएगा यूआई की तरह, रद्द/भेजें बटन क्लिक पर चीजें कैसे हो रही हैं।

परीक्षण करने के लिए, आपको डिवाइस का उपयोग करना होगा, डिवाइस में मेल एप्लिकेशन को कुछ मेल (उदा: [email protected]) के साथ कॉन्फ़िगर किया जाना चाहिए।

उम्मीद है कि मदद करता है।

-1

अपने कोड के साथ समस्या यह है, कि

// Present the view controller modally. self.present(composeVC, animated: true, completion: nil)

अपने आप में ;-)

में प्रस्तुत करता है जिससे आप वर्तमान दृश्य नियंत्रक पता नहीं है, तो बस RootViewController, यानी पर प्रदर्शित

UIApplication.shared.keyWindow? .rootViewController? .present (...

9

यहां बताया गया है मैंने किया। ऐसा लगता है कि आप documentati पीछा बहुत अच्छी तरह से, मैंने सोचा कि अगर मैं किसी और की मदद करता हूं तो मैं अपनी विविधता जोड़ूंगा। इसके अलावा, यह वर्तमान (अगस्त 2017) वाक्यविन्यास के लिए थोड़ा और अद्यतन है।

MFMailComposeViewControllerDelegate प्रोटोकॉल के अनुरूप, और जांचें कि डिवाइस मेल भेज सकता है या नहीं।

import Foundation 
import UIKit 
import MessageUI 

class WelcomeViewController: UIViewController, MFMailComposeViewControllerDelegate { 


override func viewDidLoad() { 
    super.viewDidLoad() 

    if !MFMailComposeViewController.canSendMail() { 
     print("Mail services are not available") 
     return 
    } 
} 

मेरा ऐप मेल संरचना शुरू करने के लिए आईबीएक्शन का उपयोग करता है।

@IBAction func sendFeedbackButtonTapped(_ sender: Any) { 

    let composeVC = MFMailComposeViewController() 
    composeVC.mailComposeDelegate = self 

    // Configure the fields of the interface. 
    composeVC.setToRecipients(["[email protected]"]) 
    composeVC.setSubject("StoryBook Feedback") 
    composeVC.setMessageBody("Hey Josh! Here's my feedback.", isHTML: false) 

    // Present the view controller modally. 
    self.present(composeVC, animated: true, completion: nil) 

} 

बारे में निम्नलिखित mailComposeController समारोह, प्रलेखन कहते हैं

The mail compose view controller is not dismissed automatically. When the user taps the buttons to send the email or cancel the interface, the mail compose view controller calls the mailComposeController(_:didFinishWith:error:) method of its delegate. Your implementation of that method must dismiss the view controller explicitly, as shown in Listing 3. You can also use this method to check the result of the operation.

func mailComposeController(_ controller: MFMailComposeViewController, 
          didFinishWith result: MFMailComposeResult, error: Error?) { 
    // Check the result or perform other tasks. 

    // Dismiss the mail compose view controller. 
    controller.dismiss(animated: true, completion: nil) 
    } 
} 

स्रोत Apple Documentation: MFMailComposeViewController

संबंधित मुद्दे