I'm new on iOS development, I'm making an IoT app using the TuyaSDK (I've already done the Android version of the App), the problem comes when I pair the device, it uses a delegate to listen wether the device is or isn't successfully paired. I can pair the device but it seems that my code never reaches the protocol of my delegate. This is my code:
import SwiftUI
import TuyaSmartHomeKit
import TuyaSmartDeviceKit
import Toast
import TuyaSmartActivatorKit
struct HomeDevices: View{
@State var homeId: Int64 = 0
@State var token: String = ""
@State var devId: String = ""
@State var device: TuyaSmartDevice?
let homeManager: TuyaSmartHomeManager = TuyaSmartHomeManager()
var body: some View {
VStack{
Button("Crear casa"){
addHome()
}.font(.title2)
.fontWeight(.bold)
.frame(width:150, height:20)
.foregroundColor(.white)
.padding()
.background(Color("colorbotones"))
.cornerRadius(16)
.shadow(color: .gray, radius: 5, x: 0, y: 5)
.offset(y:-100)
Button("Encender"){
publishDps(estado : true)
}.font(.title2)
.fontWeight(.bold)
.frame(width:150, height:20)
.foregroundColor(.white)
.padding()
.background(Color("colorbotones"))
.cornerRadius(16)
.shadow(color: .gray, radius: 5, x: 0, y: 5)
.offset(y:30)
Button("Apagar"){
publishDps(estado : false)
}.font(.title2)
.fontWeight(.bold)
.frame(width:150, height:20)
.foregroundColor(.white)
.padding()
.background(Color("colorbotones"))
.cornerRadius(16)
.shadow(color: .gray, radius: 5, x: 0, y: 5)
.offset(y:90)
}
}
func addHome() {
homeManager.addHome(withName: "Home",
geoName: "Bolivia",
rooms: ["Quantum"],
latitude: 0,
longitude: 0,
success: { (homeId) in
// The value of `homeId` for the home.
print("add home success")
print(homeId)
getToken(homeId: homeId)
}) { (error) in
if let e = error {
print("add home failure: \(e)")
}
}
}
func getToken(homeId : Int64){
TuyaSmartActivator.sharedInstance()?.getTokenWithHomeId(homeId, success: { (token) in
print("getToken success: \(String(describing: token))")
startConfigWiFi(token: token!)
// TODO: startConfigWiFi
}, failure: { (error) in
if let e = error {
print(homeId)
print("getToken failure: \(e)")
}
})
}
func startConfigWiFi(token: String) {
let delegate = tuyaSmartActivatorDelegate.init()
delegate.StartSync(token: token)
}
func publishDps(estado : Bool) {
device = TuyaSmartDevice(deviceId: "eb153db66617bd33e5dvi1") //I'm manually passing the deviceID
let dps = ["1" : estado]
device?.publishDps(dps, success: {
print("publishDps success")
// The DP is sent and the callback of status reporting is implemented with the `deviceDpsUpdate` method.
}, failure: { (error) in
if let e = error {
print("publishDps failure: \(e)")
}
})
}
}
struct HomeDevices_Previews: PreviewProvider {
static var previews: some View {
HomeDevices()
}
}
class tuyaSmartActivatorDelegate: NSObject, TuyaSmartActivatorDelegate {
override init() {
super.init()
TuyaSmartActivator.sharedInstance().delegate = self
}
func StartSync(token:String){
TuyaSmartActivator.sharedInstance()?.startConfigWiFi(TYActivatorModeEZ, ssid: "QUANTUM", password: "almacenqnt22*", token: token, timeout: 1000)
}
//MARK: - TuyaSmartActivatorDelegate
//My code never reaches this protocol stub//
func activator(_ activator: TuyaSmartActivator!, didReceiveDevice deviceModel: TuyaSmartDeviceModel!, error: Swift.Error!) {
print("Anything")
}
}
I've tried calling the protocol manually like this: example on how I tried to force the code to enter the protocol of the delegate what I'm expecting is to get a response from the "func activator()" in order to know if the device was paired or not. Something like this:
func activator(_ activator: TuyaSmartActivator!, didReceiveDevice deviceModel: TuyaSmartDeviceModel!, error: Error!) {
if deviceModel != nil && error == nil {
// The device is paired.
//Do something
}
if let e = error {
// Failed to pair the device.
print("\(e)")
}
}
accordind to the samples provided by Tuya IoT platform, it should work without manually calling the "fun activator()"