0
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 5 years have passed since last update.

(初心者向け)Swift3.0で初アプリ - スマホで押したボタンの情報を送付

Last updated at Posted at 2017-10-13

Swift3.0を使って簡単なiPhoneアプリを作ってみます
スマホで押したボタンの情報をサーバーへ送付します

#1. ボタン押下情報の送付

  • Xcodeで新しいSingleViewアプリケーションを用意します
  • Buttonを3個用意し、下記のbutton1button2button3へ接続します
  • ViewController.swiftに下記をコピペします
文字列の入力
import UIKit
import Alamofire

class ViewController: UIViewController {

    let url = "http://192.168.2.1:1880/test10"  //送付先を指定
    let headers: HTTPHeaders = [
        "name": "value"
    ]
    var whichButton:String = ""
    
    @IBAction func button1(_ sender: UIButton) {
        whichButton = "ボタン1"
        sendJson()
    }
    
    @IBAction func button2(_ sender: UIButton) {
        whichButton = "ボタン2"
        sendJson()
    }
    
    @IBAction func button3(_ sender: UIButton) {
        whichButton = "ボタン3"
        sendJson()
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    // 送信処理
    //func sendJson(sender:UIButton){
    func sendJson(){
        let parameters: Parameters = [
            "moji_param": whichButton,   // 文字パラメータの例
            "suji_param": 123,          // 数値パラメータの例
            "nested": [
                "nested_param": "ごきげんよう" // ネストしたパラメータの例
            ]
        ]
        // Alamofireでの送信処理
        Alamofire.request(url,
                          method: .post,    // POSTメソッド
            parameters: parameters,
            encoding: JSONEncoding.default,
            headers: headers)
            .authenticate(user: "user", password: "password")
            .responseJSON { response in
                if
                    let json = response.result.value as? [String: Any],
                    let prop1 = json["prop1"] as? String
                {
                    debugPrint(prop1)
                }
                debugPrint(response)
        }
    }
}

#2. Node-REDで受け取ります

  • 下記のNode-REDフローをコピペして動かします
スマホからのボタン情報を受信
[{"id":"662f2b11.b3d67c","type":"http in","z":"6daf1ea.58ba66","name":"","url":"/test10","method":"post","upload":false,"swaggerDoc":"","x":170,"y":60,"wires":[["386498ec.918b48","b89e76f5.b8f22","a5db31ef.24402","d69ff1de.1f35c"]]},{"id":"f814e629.f0152","type":"http response","z":"6daf1ea.58ba66","name":"","statusCode":"","headers":{},"x":790,"y":60,"wires":[]},{"id":"1cc428b9.c7b2ff","type":"template","z":"6daf1ea.58ba66","name":"htmlを返す","field":"payload","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"<html>\n    <body>\n        <h1>\n        祝!開通!!\n        </h1>\n    </body>\n</html>","output":"str","x":610,"y":60,"wires":[["f814e629.f0152"]]},{"id":"386498ec.918b48","type":"debug","z":"6daf1ea.58ba66","name":"","active":true,"console":"false","complete":"payload.moji_param","x":310,"y":200,"wires":[]},{"id":"b89e76f5.b8f22","type":"function","z":"6daf1ea.58ba66","name":"","func":"msg.hgt = msg.payload.hgt;\nreturn msg;","outputs":1,"noerr":0,"x":430,"y":60,"wires":[["1cc428b9.c7b2ff"]]},{"id":"a5db31ef.24402","type":"template","z":"6daf1ea.58ba66","name":"ボタンを抽出","field":"payload","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"{{payload.moji_param}}が押されました","output":"str","x":280,"y":260,"wires":[["a135aa.73a59258"]]},{"id":"a135aa.73a59258","type":"debug","z":"6daf1ea.58ba66","name":"","active":true,"console":"false","complete":"false","x":490,"y":260,"wires":[]},{"id":"d69ff1de.1f35c","type":"debug","z":"6daf1ea.58ba66","name":"","active":true,"console":"false","complete":"false","x":270,"y":140,"wires":[]}]


  • MacbookのNode-REDへ入力された文字列データが送付されることを確認します
スクリーンショット 2017-10-13 15.05.03.png
0
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
0
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?