SwitchWatch/Switch WatchKit Extension/Switch.swift

38 lines
1.0 KiB
Swift

//
// Switch.swift
// Switch WatchKit Extension
//
// Created by Within on 2019-09-21.
// Copyright © 2019 Within. All rights reserved.
//
import Foundation
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()
}
func getFront() -> String {
let data = try! Data(contentsOf: url)
return String(decoding: data, as: UTF8.self)
}
func setFront(_ to: String, _ callback: @escaping (String) -> Void) {
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 }
let frontString = String(decoding: data!, as: UTF8.self)
callback(frontString)
}
task.resume()
}