Why not login to Qiita and try out its useful features?

We'll deliver articles that match you.

You can read useful information later.

1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Xcode + Swift でiPhone用 地図アプリ。(23日目) 完成?

Last updated at Posted at 2022-01-14

今日やった事

・ストーリーボードでボタンを3つ作成(種別)
・ボタンのクリックイベント作成
・ボタンクリック後、MySQLサーバーへデータ送信
・データ送信結果をダイアログで表示(OK or ERROR)

コード

ViewController.swift
    //クリックイベント(3個)
    @IBAction func Trash01(_ sender: Any) {
        print("[DBG] : TRASH 01" )       
        HttpGET(data:"0")
    }
    @IBAction func Trash02(_ sender: Any) {
        print("[DBG] : TRASH 02" )
        HttpGET(data:"1")
    }
    @IBAction func Trash03(_ sender: Any) {
        print("[DBG] : TRASH 03" )
        HttpGET(data:"2")
    }

...
    // Http
    func HttpGET(data:String){
        let data:String = DateTime() + ","+data+"," + longitude + "," + latitude
        // BASIC AUTH
        let username = "(ID)"
        let password = "(PASS)"
        let session = URLSession.shared
        guard let url = URL(string: "(URL)/hoge.php?huga="+data) else { return }
        var request = URLRequest(url: url)
        request.httpMethod = "GET"
        guard let credentialData = "\(username):\(password)".data(using: String.Encoding.utf8) else { return }
        let credential = credentialData.base64EncodedString(options: [])
        let basicData = "Basic \(credential)"
        request.setValue(basicData, forHTTPHeaderField: "Authorization")
        session.dataTask(with: request) { data, urlresponse, error in
            if let error = error {
                self.ErrorDialog(data:"送信 ERROR")
                return
            }        }.resume()
        self.ErrorDialog(data:"送信 成功")
        
    }

    //確認ダイアログ
    func ErrorDialog(data:String){
        let alert: UIAlertController = UIAlertController(title: "確認", message:  data, preferredStyle:  UIAlertController.Style.alert)
        let confirmAction: UIAlertAction = UIAlertAction(title: "OK", style: UIAlertAction.Style.default, handler:{
            (action: UIAlertAction!) -> Void in
        })
        alert.addAction(confirmAction)
        present(alert, animated: true, completion: nil)
        
    }

完成

イメージ7569.jpg
作成したボタンをタップする事で、WEB上のMySQLサーバへデータの記録が出来ることを確認。
失敗した場合は、ダイアログで確認できるようにしています。

最小限の機能のみですが、一応、完成。

Kotlinと比較すると、swiftの方は、初日の環境作成を含めて3日程で、比較的楽に作る事が出来ました。

後は、ボタンに画像を表示したり、設定画面を作るなど…。

これまでの流れで、[androidアプリ]、[iPhoneアプリ]、[MySQLサーバ]、[WEBサイト]と一通り全部作り終えたので、一旦、これで閉めるかもしれません。

参考サイト

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up

Qiita Conference 2025 will be held!: 4/23(wed) - 4/25(Fri)

Qiita Conference is the largest tech conference in Qiita!

Keynote Speaker

ymrl、Masanobu Naruse, Takeshi Kano, Junichi Ito, uhyo, Hiroshi Tokumaru, MinoDriven, Minorun, Hiroyuki Sakuraba, tenntenn, drken, konifar

View event details
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?