如何通过PHP将iOS应用连接到数据库?

时间:2019-03-10 09:13:28

标签: php ios iphone xcode mobile

我正在尝试通过我的网站将iOS应用连接到sql数据库。我已经使用.php文件将数据库连接到我的网站。我正在遵循codewithchris.com上的教程来完成此任务(https://codewithchris.com/iphone-app-connect-to-mysql-database/),但是在尝试在Xcode上构建和运行应用程序时出现错误。

我的错误状态:“线程1:致命错误:在展开可选值时意外发现nil”。如果值为零,则我的程序可能无法正确访问数据库中的信息以获取值。有没有办法修复我的代码以从数据库中获取数据?

Xcode控制台指出:“ 2019-03-10 03:06:29.820695-0500 7655 Clock-It Fix-It [39747:2060656] libMobileGestalt MobileGestalt.c:890:此平台不支持MGIsDeviceOneOfType。 数据已下载”

这是我的代码:

    //
//  TableViewController.swift
//  7655 Clock-It Fix-It
//
//  Created by Kevin Terwelp on 3/9/19.
//  Copyright © 2019 Kevin Terwelp. All rights reserved.
//

import UIKit

class TableViewController: UIViewController, UITableViewDataSource, UITableViewDelegate, HomeModelProtocol {

    //Properties

    var feedItems: NSArray = NSArray()
    var selectedJob : JobsModel = JobsModel()
    @IBOutlet weak var listTableView: UITableView!

    override func viewDidLoad() {
        super.viewDidLoad()

        // set delegates and initialize homeModel

        self.listTableView.delegate = self
        self.listTableView.dataSource = self

        let homeModel = HomeModel()
        homeModel.delegate = self
        homeModel.downloadItems()
    }

    func itemsDownloaded(items: NSArray) {

        feedItems = items
        self.listTableView.reloadData()
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        //Return the number of feed items
        return feedItems.count
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

        //Retrieve cell
        let cellIdentifier: String = "BasicCell"
        let myCell: UITableViewCell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier)!
        //Get the job to be shown
        let item: JobsModel = feedItems[indexPath.row] as! JobsModel
        //Get references to labels of cell
        myCell.textLabel!.text = item.date
        myCell.textLabel!.text = item.time
        myCell.textLabel!.text = item.unit
        myCell.textLabel!.text = item.jobType

        return myCell
    }
    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destination.
        // Pass the selected object to the new view controller.
    }
    */

}

    //
//  HomeModel.swift
//  7655 Clock-It Fix-It
//
//  Created by Kevin Terwelp on 3/9/19.
//  Copyright © 2019 Kevin Terwelp. All rights reserved.
//

import UIKit
import Foundation

protocol HomeModelProtocol: class {
    func itemsDownloaded(items: NSArray)
}

class HomeModel: NSObject, URLSessionDataDelegate {

    //properties

    weak var delegate: HomeModelProtocol!

    var data = Data()

    let urlPath: String = "https://devbykevt.com/service.php" //this will be changed to the path where service.php lives

    func downloadItems() {

        let url: URL = URL(string: urlPath)!
        let defaultSession = Foundation.URLSession(configuration: URLSessionConfiguration.default)

        let task = defaultSession.dataTask(with: url) { (data, response, error)  in

            if error != nil {
                print("Failed to download data")
            } else {
                print("Data downloaded")
                self.parseJSON(data!)
            }
        }

        task.resume()
    }

    func parseJSON(_ data:Data) {

        var jsonResult = NSArray()

        do{
            jsonResult = try JSONSerialization.jsonObject(with: data, options: JSONSerialization.ReadingOptions.allowFragments) as! NSArray

        } catch let error as NSError {
            print(error)
        }

        var jsonElement = NSDictionary()
        let jobs = NSMutableArray()

        for i in 0 ..< jsonResult.count
        {

            jsonElement = jsonResult[i] as! NSDictionary

            let job = JobsModel()

            //the following insures none of the JsonElement values are nil through optional binding
            if let name = jsonElement["Name"] as? String,
                let date = jsonElement["Date"] as? String,
                let time = jsonElement["Time"] as? String,
                let unit = jsonElement["Unit"] as? String,
                let jobType = jsonElement["Job Type"] as? String,
                let address = jsonElement["Address"] as? String
            {

                job.name = name
                job.date = date
                job.time = time
                job.unit = unit
                job.jobType = jobType
                job.address = address

            }

            jobs.add(job)
        }

        DispatchQueue.main.async(execute: { () -> Void in

            self.delegate.itemsDownloaded(items: jobs)
        })
    }

}


    //
//  JobsModel.swift
//  7655 Clock-It Fix-It
//
//  Created by Kevin Terwelp on 3/9/19.
//  Copyright © 2019 Kevin Terwelp. All rights reserved.
//

import UIKit
import Foundation

class JobsModel: NSObject {

    //properties

    var name: String?
    var date: String?
    var time: String?
    var unit: String?
    var jobType: String?
    var address: String?

    //empty constructor

    override init()
    {

    }

    //construct with @name, @date, @time, @unit, @jobType, and @address parameters

    init(name: String, date: String, time: String, unit: String, jobType: String, address: String) {

        self.name = name
        self.date = date
        self.time = time
        self.unit = unit
        self.jobType = jobType
        self.address = address

    }

    //prints object's current state

    override var description: String {
        return "Name: \(name), Date: \(date), Time: \(time), Unit: \(unit), Job Type: \(jobType), Address: \(address)"
    }

}

感谢所有可以帮助我的人!如果需要更多信息,请告诉我。

0 个答案:

没有答案