【发布时间】:2015-12-16 04:38:09
【问题描述】:
我有一些代码从文本框中获取文本并通过 POST 消息将其发送到服务器,然后服务器将其发送到电子邮件地址。
class ViewController: UIViewController {
@IBOutlet weak var emailText: UITextView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func sendEmail() {
let url = NSURL(string:"http://www.fakewebsite.com/email.php")
let cachePolicy = NSURLRequestCachePolicy.ReloadIgnoringLocalCacheData
var request = NSMutableURLRequest(URL: url!, cachePolicy: cachePolicy, timeoutInterval: 2.0)
request.HTTPMethod = "POST"
// set Content-Type in HTTP header
let boundaryConstant = "----------V2ymHFg03esomerandomstuffhbqgZCaKO6jy";
let contentType = "multipart/form-data; boundary=" + boundaryConstant
NSURLProtocol.setProperty(contentType, forKey: "Content-Type", inRequest: request)
// set data
var dataString = emailText.text
let requestBodyData = (dataString as NSString).dataUsingEncoding(NSUTF8StringEncoding)
request.HTTPBody = requestBodyData
// set content length
//NSURLProtocol.setProperty(requestBodyData.length, forKey: "Content-Length", inRequest: request)
var response: NSURLResponse? = nil
var error: NSError? = nil
let reply = NSURLConnection.sendSynchronousRequest(request, returningResponse:&response, error:&error)
let results = NSString(data:reply!, encoding:NSUTF8StringEncoding)
println("API Response: \(results)")
}
@IBAction func sendButton(sender: AnyObject) {
sendEmail()
println(emailText.text)
}
}
按下按钮时电子邮件会通过,但电子邮件中没有任何内容,只是空白。是的,文本字段中有文本。你能看出我哪里出错了吗?
任何帮助表示赞赏, 谢谢。
【问题讨论】:
-
我不知道它是否会有所帮助,因为我觉得即使它是可选的,文本仍然会被发送,但请尝试在文本框中展开文本,因为这在技术上是一个可选属性跨度>
标签: ios swift email post message