SwitchWatch/Switch WatchKit Extension/Switch.swift

38 lines
1.0 KiB
Swift
Raw Permalink Normal View History

2019-09-21 21:00:13 +00:00
//
// Switch.swift
// Switch WatchKit Extension
//
// Created by Within on 2019-09-21.
// Copyright © 2019 Within. All rights reserved.
//
import Foundation
2019-09-22 14:58:03 +00:00
func getFront(_ callback: @escaping (String) -> Void) {
let task = URLSession.shared.dataTask(with: url) { data, resp, error in
guard data != nil else { return }
let frontString = String(decoding: data!, as: UTF8.self)
callback(frontString)
}
task.resume()
}
2019-09-21 21:00:13 +00:00
func getFront() -> String {
let data = try! Data(contentsOf: url)
return String(decoding: data, as: UTF8.self)
}
2019-09-22 14:58:03 +00:00
func setFront(_ to: String, _ callback: @escaping (String) -> Void) {
2019-09-21 21:00:13 +00:00
var request = URLRequest(url: url)
request.httpMethod = "POST"
request.httpBody = to.data(using: .utf8)
let task = URLSession.shared.dataTask(with: request) { data, resp, error in
guard data != nil else { return }
2019-09-22 14:58:03 +00:00
let frontString = String(decoding: data!, as: UTF8.self)
callback(frontString)
2019-09-21 21:00:13 +00:00
}
task.resume()
}