I have a php page that returns a JSON array, here is the php:
$sql ="SELECT * FROM SongTable";
if ($result = mysqli_query($con, $sql)) {
while ($row = mysqli_fetch_row($result)) {
$allarray[] = $row;
}
echo json_encode($allarray, JSON_UNESCAPED_SLASHES);
In my swift code I want to use datataskwithURL
to download the JSON array, then save that data to a new array to use later. Here is my swift code.
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
//Data Pull Section
let url_to_request = (string: "********")
let url:NSURL = NSURL(string: url_to_request)!
let session = URLSession.shared
let request = NSMutableURLRequest(url: url as URL)
//request data from URL
let task = session.dataTask(with: request as URLRequest) {
(
data, response, error) in
guard let _:NSData = data as NSData?, let _:URLResponse = response, error == nil else {
print("error")
return
}
//create data string
let dataString = NSString(data: data!, encoding: String.Encoding.utf8.rawValue)
print(dataString! as NSString)
}
} //close viewDidLoad
When I run the Simulator in Xcode I don't get any errors, But I don't know how to make sure its working properly or not. I also Need to save this data I am getting from the url to a new array in xcode. Thanks for the help!