func requestLiveInfo(){
print("requestLiveInfo")
let url = URL(string:"https://release.hihealth.com.tw/api/MoveV/live-info")
var req = URLRequest(url: url!)
req.httpMethod = "GET"
let task = URLSession.shared.dataTask(with: req) { data, response, error in
guard let data = data, error == nil else {
print("request fail")
print(error?.localizedDescription ?? "No data")
DispatchQueue.main.async {
showAlert(target: self, title: "Error", msg: "Please confirm your internet connection status", cancelString: nil, confirmString: "reorganize", callback: { (btnindex) in
if btnindex == 1{
print("Please confirm your internet connection status")
self.requestLiveInfo()
}
});
}
return
}
if let httpResponse = response as? HTTPURLResponse {
print("request success")
print(httpResponse.statusCode)
if (httpResponse.statusCode == 200){
do{
let responseString = String(bytes: data, encoding: String.Encoding.utf8)
print(responseString)
if let responseJSON = try JSONSerialization.jsonObject(with: data, options: []) as? [String:String]{
let dateFormatter = DateFormatter()
dateFormatter.timeZone = TimeZone(identifier: "Asia/Taipei")
dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
if let startTimeString = responseJSON["started_at"],
let endTimeString = responseJSON["ended_at"],
let nameString = responseJSON["name"]{
if let start = dateFormatter.date(from: startTimeString),
let end = dateFormatter.date(from: endTimeString){
let now = Date()
if(now >= start && now <= end){
DispatchQueue.main.async {
self.liveBellImg.image = UIImage(named: "live-bell-on")
self.liveButtonImg.image = UIImage(named: "live-join-on")
self.liveLabelSetUp() //Use here
self.liveLabel.text = "Live:\(nameString)"
}
}
else{
DispatchQueue.main.async {
dateFormatter.dateFormat = "MM/dd HH:mm"
self.liveBellImg.image = UIImage(named: "live-bell-off")
self.liveButtonImg.image = UIImage(named: "live-join-off")
self.liveLabelSetUp() //Use here
self.liveLabel.text = "\(dateFormatter.string(from: start)) Live:\(nameString)"
}
}
DispatchQueue.main.async {
let uiTap = UITapGestureRecognizer(target: self, action: #selector(self.goToLive))
self.liveButtonImg.addGestureRecognizer(uiTap)
}
}
else{
DispatchQueue.main.async {
showAlert(target: self, title: "Error", msg: "Format is wrong", cancelString: nil, confirmString: "confirm", callback: { (btnindex) in
if btnindex == 1{
}
});
}
}
}
else{
DispatchQueue.main.async {
showAlert(target: self, title: "Error", msg: "Live information data is missing", cancelString: nil, confirmString: "confirm", callback: { (btnindex) in
if btnindex == 1{
}
});
}
}
}
}
catch{
DispatchQueue.main.async {
showAlert(target: self, title: "Error", msg: "The live broadcast information data format is incorrect", cancelString: nil, confirmString: "confirm", callback: { (btnindex) in
if btnindex == 1{
}
});
}
}
}
else
{
DispatchQueue.main.async {
let responseString = String(bytes: data, encoding: String.Encoding.utf8)
showAlert(target: self, title: "Error", msg: responseString ?? "Unknown", cancelString: nil, confirmString: "confirm", callback: { (btnindex) in
if btnindex == 1{
}
});
}
}
}
}
task.resume()
}
crash exactly
func liveLabelSetUp() {
// Continuous, with tap to pause
liveLabel.type = .continuousReverse //crash
liveLabel.speed = .duration(10)
liveLabel.fadeLength = 10.0
liveLabel.leadingBuffer = 40.0
liveLabel.trailingBuffer = 30.0
liveLabel.isUserInteractionEnabled = true
let tapRecognizer = UITapGestureRecognizer(target: self, action: #selector(self.pauseTap))
tapRecognizer.numberOfTapsRequired = 1
tapRecognizer.numberOfTouchesRequired = 1
liveLabel.addGestureRecognizer(tapRecognizer)
}