import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
self.view.backgroundColor = UIColor.lightGray
let myTextView = UITextView(frame: CGRect(x: 10, y: 50, width: self.view.frame.width - 20, height: 500))
myTextView.backgroundColor = UIColor(red: 0.9, green: 0.9, blue: 1, alpha: 1.0)
myTextView.layer.masksToBounds = true
myTextView.layer.cornerRadius = 20.0
myTextView.layer.borderWidth = 1
myTextView.layer.borderColor = UIColor.black.cgColor
myTextView.font = UIFont.systemFont(ofSize: CGFloat(20))
myTextView.textColor = UIColor.black
myTextView.textAlignment = NSTextAlignment.left
myTextView.dataDetectorTypes = UIDataDetectorTypes.all
myTextView.layer.shadowOpacity = 0.5
myTextView.layer.masksToBounds = false
myTextView.isEditable = false
self.view.addSubview(myTextView)
var json: NSData!
let myDict:NSMutableDictionary = NSMutableDictionary()
myDict.setObject("object1", forKey: "key1" as NSCopying)
myDict.setObject("object2", forKey: "key2" as NSCopying)
myDict.setObject("object3", forKey: "key3" as NSCopying)
myDict.setObject("object4", forKey: "key4" as NSCopying)
if JSONSerialization.isValidJSONObject(myDict){
do {
json = try JSONSerialization.data(withJSONObject: myDict, options: JSONSerialization.WritingOptions.prettyPrinted) as NSData
print(NSString(data: json as Data, encoding: String.Encoding.utf8.rawValue)!)
} catch {
print(error)
}
}
let config:URLSessionConfiguration = URLSessionConfiguration.default
let url:NSURL = NSURL(string: "http:/xxx/json_decode.php")!
let request:NSMutableURLRequest = NSMutableURLRequest(url: url as URL)
let session:URLSession = URLSession(configuration: config)
request.httpMethod = "POST"
let data:NSString = "json=\(NSString(data: json as Data, encoding: String.Encoding.utf8.rawValue)!)" as NSString
request.httpBody = data.data(using: String.Encoding.utf8.rawValue)
let task:URLSessionDataTask = session.dataTask(with: request as URLRequest, completionHandler: { (_data, response, err) -> Void in
DispatchQueue.main.async(execute: {
myTextView.text = NSString(data: _data!, encoding: String.Encoding.utf8.rawValue)! as String
})
})
task.resume()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
self.view.backgroundColor = UIColor.lightGray
let myTextView = UITextView(frame: CGRect(x: 10, y: 50, width: self.view.frame.width - 20, height: 500))
myTextView.backgroundColor = UIColor(red: 0.9, green: 0.9, blue: 1, alpha: 1.0)
myTextView.layer.masksToBounds = true
myTextView.layer.cornerRadius = 20.0
myTextView.layer.borderWidth = 1
myTextView.layer.borderColor = UIColor.black.cgColor
myTextView.font = UIFont.systemFont(ofSize: CGFloat(20))
myTextView.textColor = UIColor.black
myTextView.textAlignment = NSTextAlignment.left
myTextView.dataDetectorTypes = UIDataDetectorTypes.all
myTextView.layer.shadowOpacity = 0.5
myTextView.layer.masksToBounds = false
myTextView.isEditable = false
self.view.addSubview(myTextView)
var json: NSData!
let myDict:NSMutableDictionary = NSMutableDictionary()
myDict.setObject("object1", forKey: "key1")
myDict.setObject("object2", forKey: "key2")
myDict.setObject("object3", forKey: "key3")
myDict.setObject("object4", forKey: "key4")
if JSONSerialization.isValidJSONObject(myDict){
do {
json = try JSONSerialization.data(withJSONObject: myDict, options: JSONSerialization.WritingOptions.prettyPrinted)
print(NSString(data: json as Data, encoding: String.Encoding.utf8.rawValue)!)
} catch {
print(error)
}
}
let config:URLSessionConfiguration = URLSessionConfiguration.default
let url:NSURL = NSURL(string: "http:/xxx/json_decode.php")!
let request:NSMutableURLRequest = NSMutableURLRequest(url: url as URL)
let session:URLSession = URLSession(configuration: config)
request.httpMethod = "POST"
let data:NSString = "json=\(NSString(data: json as Data, encoding: String.Encoding.utf8.rawValue)!)"
request.httpBody = data.data(using: String.Encoding.utf8.rawValue)
let task:URLSessionDataTask = session.dataTask(with: request as URLRequest, completionHandler: { (_data, response, err) -> Void in
DispatchQueue.main.async(execute: {
myTextView.text = NSString(data: _data!, encoding: String.Encoding.utf8.rawValue)! as String
})
})
task.resume()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}
import UIKit
class ViewController: UIViewController{
override func viewDidLoad() {
super.viewDidLoad()
self.view.backgroundColor = UIColor.lightGrayColor()
let myTextView = UITextView(frame: CGRectMake(10, 50, self.view.frame.width - 20, 500))
myTextView.backgroundColor = UIColor(red: 0.9, green: 0.9, blue: 1, alpha: 1.0)
myTextView.layer.masksToBounds = true
myTextView.layer.cornerRadius = 20.0
myTextView.layer.borderWidth = 1
myTextView.layer.borderColor = UIColor.blackColor().CGColor
myTextView.font = UIFont.systemFontOfSize(CGFloat(20))
myTextView.textColor = UIColor.blackColor()
myTextView.textAlignment = NSTextAlignment.Left
myTextView.dataDetectorTypes = UIDataDetectorTypes.All
myTextView.layer.shadowOpacity = 0.5
myTextView.layer.masksToBounds = false
myTextView.editable = false
self.view.addSubview(myTextView)
var json: NSData!
let myDict:NSMutableDictionary = NSMutableDictionary()
myDict.setObject("object1", forKey: "key1")
myDict.setObject("object2", forKey: "key2")
myDict.setObject("object3", forKey: "key3")
myDict.setObject("object4", forKey: "key4")
if NSJSONSerialization.isValidJSONObject(myDict){
do {
json = try NSJSONSerialization.dataWithJSONObject(myDict, options: NSJSONWritingOptions.PrettyPrinted)
print(NSString(data: json, encoding: NSUTF8StringEncoding)!)
} catch {
print(error)
}
}
let config:NSURLSessionConfiguration = NSURLSessionConfiguration.defaultSessionConfiguration()
let url:NSURL = NSURL(string: "http:/xxx/json_decode.php")!
let request:NSMutableURLRequest = NSMutableURLRequest(URL: url)
let session:NSURLSession = NSURLSession(configuration: config)
request.HTTPMethod = "POST"
let data:NSString = "json=\(NSString(data: json, encoding: NSUTF8StringEncoding)!)"
request.HTTPBody = data.dataUsingEncoding(NSUTF8StringEncoding)
let task:NSURLSessionDataTask = session.dataTaskWithRequest(request, completionHandler: { (_data, response, err) -> Void in
dispatch_async(dispatch_get_main_queue(), {
myTextView.text = NSString(data: _data!, encoding: NSUTF8StringEncoding)! as String
})
})
task.resume()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}