基本上,我需要在mySQL数据库和Swift应用程序之间建立安全连接。理想情况下,我需要使表格数据出现在快速表格视图中。
虽然我已经使用PHP脚本在mySQL数据库和Swift应用程序之间建立了连接,但是当转到正确的地址(www.example.com/service.php)时,我不能只在网站上显示此信息。
重要的是,PHP脚本只能为我的移动应用程序加载,而不能为任何其他人加载。
当前,这是我在Swift App和PHP脚本中用于访问数据库的代码。
我可以对代码进行哪些更改?欢迎任何建议,我非常感谢。
Swift应用程序:
FirstViewController:
private boolean areFourConnected(){
for (int j = 0; j<rows-3 ; j++ ){
for (int i = 0; i<columns; i++){
if (this.cells[i][j].getPlayer() == cells[i][j].getPlayer() && this.cells[i][j+1].getPlayer() == cells[i][j].getPlayer() && this.cells[i][j+2].getPlayer() == cells[i][j].getPlayer() && this.cells[i][j+3].getPlayer() == cells[i][j].getPlayer()){
return true;
}
}
}
for (int i = 0; i<columns-3 ; i++ ){
for (int j = 0; j<this.rows; j++){
if (this.cells[i][j].getPlayer() == cells[i][j].getPlayer() && this.cells[i+1][j].getPlayer() == cells[i][j].getPlayer() && this.cells[i+2][j].getPlayer() == cells[i][j].getPlayer() && this.cells[i+3][j].getPlayer() == cells[i][j].getPlayer()){
return true;
}
}
}
for (int i=3; i<columns; i++){
for (int j=0; j<rows-3; j++){
if (this.cells[i][j].getPlayer() == cells[i][j].getPlayer() && this.cells[i-1][j+1].getPlayer() == cells[i][j].getPlayer() && this.cells[i-2][j+2].getPlayer() == cells[i][j].getPlayer() && this.cells[i-3][j+3].getPlayer() == cells[i][j].getPlayer())
return true;
}
}
for (int i=3; i<columns; i++){
for (int j=3; j<rows; j++){
if (this.cells[i][j].getPlayer() == cells[i][j].getPlayer() && this.cells[i-1][j-1].getPlayer() == cells[i][j].getPlayer() && this.cells[i-2][j-2].getPlayer() == cells[i][j].getPlayer() && this.cells[i-3][j-3].getPlayer() == cells[i][j].getPlayer())
return true;
}
}
return false;
}
FeedModel.swift:
import UIKit
class FirstViewController: UIViewController, UITableViewDataSource, UITableViewDelegate, FeedModelProtocol {
var feedItems: NSArray = NSArray()
var selectedStock : StockModel = StockModel()
@IBOutlet weak var listTableView: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
//set delegates and initialize FeedModel
self.listTableView.delegate = self
self.listTableView.dataSource = self
let feedModel = FeedModel()
feedModel.delegate = self
feedModel.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 = "stockCell"
let myCell: UITableViewCell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier)!
// Get the stock to be shown
let item: StockModel = feedItems[indexPath.row] as! StockModel
// Configure our cell title made up of name and price
let titleStr: String = item.manufacturer! + ": " + item.model!
print(titleStr)
// Get references to labels of cell
myCell.textLabel!.text = titleStr
return myCell
}
}
StockModel:
import Foundation
protocol FeedModelProtocol: class {
func itemsDownloaded(items: NSArray)
}
class FeedModel: NSObject, URLSessionDataDelegate {
weak var delegate: FeedModelProtocol!
let urlPath = "https://www.example.com/service.php"
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("Error")
}else {
print("stocks 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 stocks = NSMutableArray()
for i in 0 ..< jsonResult.count
{
jsonElement = jsonResult[i] as! NSDictionary
let stock = StockModel()
//the following insures none of the JsonElement values are nil through optional binding
if let name = jsonElement["manufacturer"] as? String,
let price = jsonElement["model"] as? String
{
print(name)
print(price)
stock.manufacturer = name
stock.model = price
}
stocks.add(stock)
}
DispatchQueue.main.async(execute: { () -> Void in
self.delegate.itemsDownloaded(items: stocks)
})
}
}
PHP脚本:
import UIKit
class StockModel: NSObject {
//properties of a stock
var manufacturer: String?
var model: String?
//empty constructor
override init()
{
}
//construct with @name and @price parameters
init(manufacturer: String, model: String) {
self.manufacturer = manufacturer
self.model = model
}
//prints a stock's name and price
override var description: String {
return "Name: \(String(describing: manufacturer)), Address: \(String(describing: model))"
}
}