[:en]The most challenging part of uploading data to the server by using Swift is the space between the texts:
Here is the solution: replace all empty spaces with “+” to solve the uploading and downloading problems as the server ignores “+” symbol.
@IBAction func GoAction(sender: AnyObject) {
// Replace all empty spaces with "+" symbols; else, can't upload the data
let studentNoSpace = studentnamelabel.text!.stringByReplacingOccurrencesOfString(" ", withString: "+")
let courseNoSpace = coursenamelabel.text!.stringByReplacingOccurrencesOfString(" ", withString: "+")
let remarkNoSpace = remarklabel.text!.stringByReplacingOccurrencesOfString(" ", withString: "+")
let phoneNoSpace = phonelabel.text!.stringByReplacingOccurrencesOfString(" ", withString: "+")
if company != nil && useruploaded != nil {
let myUrl = NSString(format:"http://sample.com/adduserphp?FirstName=%@&dbname=%@&LastName=%@&Street=%@&City=%@&State=%@&Zip=%@&Email=%@&inc=%@&score=%@&Phone=%@&Recorder=%@&version=%@&station=%@&announce2=%@", Dummy, company!, Dummy, studentNoSpace, "ID", courseNoSpace, remarkNoSpace,"FreeTrial", Dummy, dateLabel.titleLabel!.text!,Dummy, Dummy, Dummy, phoneNoSpace, Playstore) as String;
let request = NSMutableURLRequest(URL: NSURL(string: myUrl as String)!)
request.HTTPMethod = "POST";
ActivityIndicator.hidden = false
ActivityIndicator.hidesWhenStopped = false
ActivityIndicator.startAnimating();
let task = NSURLSession.sharedSession().dataTaskWithRequest(request) {
data, response, error in
if error != nil {
print("error=\(error)")
return
}
// Print out reponse body
let todaystudentlist = NSString(data: data!, encoding: NSUTF8StringEncoding)
self.defaults.setValue(todaystudentlist!, forKey: "teacher")
print("response:\(todaystudentlist!)")
// Important to include all the actions here upon completion transmission to server
dispatch_async(dispatch_get_main_queue(),{
self.tableView1.hidden = false
self.myDatePicker.hidden = true
self.gobTnDisplay.hidden = false
self.ActivityIndicator.stopAnimating()
self.ActivityIndicator.hidesWhenStopped = true
self.downloadfreetrial()
});
}
task.resume()
}
}
[:]
