我有一些代码从文本框中获取文本并通过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)
}
}
当按下按钮时电子邮件会通过,但电子邮件中没有任何内容,它只是空白。是的,文本字段中有文本。你能看出我可能出错的地方吗?
感谢任何帮助, 感谢。
答案 0 :(得分:0)
您的文字可能不会出现的原因有多种:
- >您的故事板与ViewController之间的映射在某种程度上被切断了。尝试重新连接它们。
- >您可以尝试并专门使用" self"
var dataString = self.emailText.text;
- >您可以使用NSData提供的以下初始化程序:
NSData(base64String:String, options:NSDataBase64DecodingOptions)
- >放置一个调试点或编写一个简单的print语句,显示存在的数据的长度:
let requestBodyData = (dataString as NSString).dataUsingEncoding(NSUTF8StringEncoding)
//if this print says length is 0 then you dont seem to a valid link between the storyboard and the ViewController
print(requestBodyData.length)
request.HTTPBody = requestBodyData
答案 1 :(得分:0)
EDITED: 试试这样的事情
let postRequest = NSMutableURLRequest(URL: NSURL(string: "")!)
let postBody = NSMutableData()
postRequest.HTTPMethod = "POST"
// Boundary & Charset
let boundary = "blahblahblah"
let charset = "utf-8"
let contentType = "multipart/form-data; charset=\(charset); boundary=\(boundary)"
postRequest.addValue(contentType, forHTTPHeaderField: "Content-Type")
let stringData = NSMutableString()
stringData.appendFormat("--\(boundary)\r\n")
let endBound = "\r\n--\(boundary)\r\n"
let codeInfo = "Content-Disposition: form-data; name=\"code\"\r\n\r\n\(code!)"
stringData.appendString(codeInfo)
stringData.appendString(endBound)
postBody.appendData(stringData.dataUsingEncoding(NSUTF8StringEncoding)!)
postRequest.HTTPBody = postBody
let operationQueue = NSOperationQueue()
NSURLConnection.sendAsynchronousRequest(postRequest, queue: operationQueue) { (response, data, error) -> Void in
if data != nil {
let resp = String(data: data!, encoding: NSUTF8StringEncoding)
print("Response -> \(resp)")
}
}
// -----
尝试对代码进行调试,看看“dataString”中是否包含文本。 还要确保'requestBodyData'不是nil
如果它的nil或empty可能意味着你的故事板的emailText没有与代码'@IBOutlet'正确连接
// set data
var dataString = emailText.text
let requestBodyData = (dataString as NSString).dataUsingEncoding(NSUTF8StringEncoding)
或者可以像这样检查
let contentType = "multipart/form-data; charset=utf-8; boundary=\(boundary)"
request.addValue(contentType, forHTTPHeaderField: "Content-Type")