LoginSignup
0
1

More than 5 years have passed since last update.

(初心者向け)Swift3.0で初アプリ - スマホからの文字列入力

Last updated at Posted at 2017-10-10

Swift3.0を使って簡単なiPhoneアプリを作ってみます
スマホからの文字列入力を扱ってみます

1. 文字列の入力

  • Xcodeで新しいSingleViewアプリケーションを用意します
  • 下記のソースを使用するためには、自分のアプリのMain.storyboardに自分であらかじめテキストフィールド(TextField)とボタン(Button)を用意する必要があります。TextFieldとButtonを用意し、下記のtf1button1へ接続します
  • ViewController.swiftに下記をコピペします
文字列の入力
import UIKit

class ViewController: UIViewController, UITextFieldDelegate {

    @IBOutlet weak var tf1: UITextField!
    @IBAction func button1(_ sender: UIButton) {
        print("print from button : \(tf1.text!)")
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        // Delegateを自身に設定するx
        tf1.delegate = self as UITextFieldDelegate
    }

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

     // UITextFieldが編集された直前に呼ばれる
    func textFieldDidBeginEditing(_ tf1: UITextField) {
        print("textFieldDidBeginEditing: \(tf1.text!)")
    }

    // UITextFieldが編集された直後に呼ばれる
    func textFieldDidEndEditing(_ tf1: UITextField) {
        print("textFieldDidEndEditing: \(tf1.text!)")
    }

    //  改行ボタンが押された際に呼ばれる
    func textFieldShouldReturn(_ tf1: UITextField) -> Bool {
        print("textFieldShouldReturn \(tf1.text!)")
        // 改行ボタンが押されたらKeyboardを閉じる処理.
        tf1.resignFirstResponder()
        return true
    }
}
  • TextFieldへ入力し、ボタンを押すとXcodeのログ出力領域へ出力されることを確認します

2. Node-REDへ送付します

  • (初心者向け)Swift3.0で初アプリ - iPhoneからMacbookへアクセスを参照し、iPhoneからMacbookのNode-REDへ接続します
  • 下記のソースを使用するためには、自分のアプリのMain.storyboardに自分であらかじめテキストフィールド(TextField)とボタン(Button)を用意する必要があります。TextFieldとButtonを用意し、下記のtf1button1へ接続します
  • 下記をコピペし実行してみます
文字列を入力しNode-REDへ送付
import UIKit
import Alamofire
import CoreMotion

class ViewController: UIViewController, UITextFieldDelegate {

    let url = "http://192.168.2.1:1880/test10"  //送付先を指定
    let headers: HTTPHeaders = [
        "name": "value"
    ]

    @IBOutlet weak var tf1: UITextField!
    @IBAction func button1(_ sender: UIButton) {
        sendJson()
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        // Delegateを自身に設定するx
        tf1.delegate = self as UITextFieldDelegate
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    // UITextFieldが編集された直前に呼ばれる
    func textFieldDidBeginEditing(_ tf1: UITextField) {
        print("textFieldDidBeginEditing: \(tf1.text!)")
    }

    // UITextFieldが編集された直後に呼ばれる
    func textFieldDidEndEditing(_ tf1: UITextField) {
        print("textFieldDidEndEditing: \(tf1.text!)")
    }

    //  改行ボタンが押された際に呼ばれる
    func textFieldShouldReturn(_ tf1: UITextField) -> Bool {
        print("textFieldShouldReturn \(tf1.text!)")
        // 改行ボタンが押されたらKeyboardを閉じる処理.
        tf1.resignFirstResponder()
        return true
    }

    // 送信処理
    //func sendJson(sender:UIButton){
    func sendJson(){
        let parameters: Parameters = [
            "moji_param": tf1.text,   // 文字パラメータの例
            "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)
        }
    }
}

  • MacbookのNode-REDへ入力された文字列データが送付されることを確認します

IMG_0658.jpg

スクリーンショット 2017-10-10 11.38.13.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