Ios 在swift 4中按下按钮时,如何将json结果发送到电子邮件?
我遇到了一个问题,我有一个json问题列表,当用户填写一般问题时,我会显示要提交的a按钮,我要做的就是使用提交按钮将json结果发送到我的电子邮件。我的代码可以吗?非常感谢你!顺便说一下,我正在使用SurveyNative创建调查Ios 在swift 4中按下按钮时,如何将json结果发送到电子邮件?,ios,json,swift,Ios,Json,Swift,我遇到了一个问题,我有一个json问题列表,当用户填写一般问题时,我会显示要提交的a按钮,我要做的就是使用提交按钮将json结果发送到我的电子邮件。我的代码可以吗?非常感谢你!顺便说一下,我正在使用SurveyNative创建调查 import UIKit import SurveyNative class MyViewController: SurveyViewController, SurveyAnswerDelegate, CustomConditionDelegate, Validat
import UIKit
import SurveyNative
class MyViewController: SurveyViewController, SurveyAnswerDelegate, CustomConditionDelegate, ValidationFailedDelegate {
override func viewDidLoad() {
super.viewDidLoad()
self.setSurveyAnswerDelegate(self)
self.setCustomConditionDelegate(self)
self.setValidationFailedDelegate(self)
}
override func surveyJsonFile() -> String {
return "GeneralQuestions"
}
override func surveyTitle() -> String {
return "General Questions"
}
func question(for id: String, answer: Any) {
print("Question: \(id) has answer (maybe is complete): \(answer)")
if (surveyQuestions!.isQuestionFullyAnswered(id)) {
print("Question: \(id) is complete")
}
}
func isConditionMet(answers: [String: Any], extra: [String: Any]?) -> Bool {
let id = extra!["id"] as! String
if id == "check_age" {
if let birthYearStr = answers["birthyear"] as? String, let ageStr = answers["age"] as? String {
let birthYear = Int(birthYearStr)
let age = Int(ageStr)
let wiggleRoom = extra!["wiggle_room"] as? Int
let date = Date()
let calendar = Calendar.current
let components = calendar.dateComponents([.year], from: date)
let currentYear = components.year
return abs(birthYear! + age! - currentYear!) > wiggleRoom!
} else {
return false
}
} else {
Logger.log("Unknown custom condition check: \(id)")
return false
}
}
func validationFailed(message: String) {
let alert = UIAlertController(title: nil, message: message, preferredStyle: UIAlertController.Style.alert)
alert.addAction(UIAlertAction(title: "OK", style: UIAlertAction.Style.default, handler: nil))
self.present(alert, animated: true, completion: nil)
}
}
我的Json:
{
"questions": [
{
"id": "ice_cream",
"header": "Question 1",
"question": "Did you enjoy the discussion topic?",
"question_type": "single_select",
"options": [
"YES",
"NO",
"I DONT KNOW",
{
"title": "Other",
"type": "freeform"
}
]
},
{
"id": "perfect_day",
"header": "Question 2",
"question": "Did you enjoy the presenter? If yes, why?",
"question_type": "single_select",
"options": [
"YES",
"NO",
"I DONT KNOW",
{
"title": "Other",
"type": "freeform"
}
]
},
{
"id": "ice_cream",
"header": "Question 3",
"question": "Did you feel you engaged or connected to the topic discussed?",
"question_type": "single_select",
"options": [
"YES",
"NO",
{
"title": "Other",
"type": "freeform"
}
]
},
{
"id": "perfect_day",
"header": "Question 4",
"question": "Do you have any topics you would like to discuss?",
"question_type": "single_select",
"options": [
"YES",
"NO",
"I DONT KNOW",
]
},
{
"id": "felt_like_adult",
"header": "Question 5",
"question": "Do you feel we can change anything?",
"question_type": "single_select",
"options": [
"YES",
"NO",
"I DONT KNOW",
]
},
{
"id": "pets",
"header": "Question 6",
"question": "Do you prefer a specific presenter? If yes, why?",
"question_type": "single_select",
"options": [
"YES",
"NO",
"I DONT KNOW",
{
"title": "Other",
"type": "freeform"
}
]
},
],
"submit": {
"button_title": "Submit Answers",
"url": "https://www.example.com"
},
"auto_focus_text": true
}
我能知道在电子邮件中获取JSON结果的目的吗?以下是发送电子邮件的几个选项 选择1 您可以使用MFMailComposeViewController从应用程序发送电子邮件。将JSON设置为消息体。 下面介绍如何使用MFMailComposeViewController发送电子邮件 选择2 使用API服务将电子邮件发送回您的电子邮件。例如:Gmail API。你可以看一看 选择3 创建自己的API,将JSON结果发布到服务器,并使用自己的邮件服务器接收消息