iosswifttuya

NSException occures while setuping Tuya SDK on IOS


I am new on IOS development and I am trying to create lot app using Tuya Smart Life App SDK. I am facing problem when I try to start the app after adding this SDK configure, with appKey and appSecret:

ThingSmartSDK.sharedInstance()?.start(withAppKey: <#your_app_key#>, secretKey: <#your_secret_key#>)

According to the Fast Integration guide this should be placed inside of AppDelegate.m -file, but I have read that this file is not being added to IOS projects automatically any more. (I am using Xcode version '14.3.1')

I have added the AppDelegate class inside of AppNameApp.swift -file and it looks like this:

import SwiftUI
import ThingSmartBaseKit

@main
struct AppNameApp: App {
    
    @UIApplicationDelegateAdaptor(AppDelegate.self) var appDelegate
    
    var body: some Scene {
        WindowGroup {
            ContentView()
        }
    }
}

class AppDelegate: NSObject, UIApplicationDelegate {
    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool {
        ThingSmartSDK.sharedInstance().start(withAppKey: <#your_app_key#>, secretKey: <#your_secret_key#>)
        
        #if DEBUG
        ThingSmartSDK.sharedInstance().debugMode = true
        #else
        #endif
        
        
        return true
    }
}

Currently I am not seeing any problem messages coming from this in Xcode, but when I try to run the app on simulator it crashes due to uncaught exception of type NSException. Crash message:

2023-07-13 15:26:50.051841+0300 iosAppName[22835:261964] +[NSArray yy_modelArrayWithClass:json:]: unrecognized selector sent to class 0x10c377890
2023-07-13 15:26:50.086746+0300 iosAppName[22835:261964] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '+[NSArray yy_modelArrayWithClass:json:]: unrecognized selector sent to class 0x10c377890'
*** First throw call stack:
(
    0   CoreFoundation                      0x000000010bf7278b __exceptionPreprocess + 242
    1   libobjc.A.dylib                     0x000000010a1e2b73 objc_exception_throw + 48
    2   CoreFoundation                      0x000000010bf817dd __CFExceptionProem + 0
    3   CoreFoundation                      0x000000010bf76c66 ___forwarding___ + 1443
    4   CoreFoundation                      0x000000010bf78e08 _CF_forwarding_prep_0 + 120
    5   iosAppName                          0x000000010072ada3 +[ThingConfigLoader loadConfig] + 349
    6   iosAppName                          0x000000010073d969 -[ThingSmartSDK startWithAppKey:secretKey:] + 1878
    7   iosAppName                          0x00000001003f2869 $s10iosAppName11AppDelegateC11application_29didFinishLaunchingWithOptionsSbSo13UIApplicationC_SDySo0l6LaunchK3KeyaypGSgtF + 217
    8   iosAppName                          0x00000001003f29e7 $s10iosAppName11AppDelegateC11application_29didFinishLaunchingWithOptionsSbSo13UIApplicationC_SDySo0l6LaunchK3KeyaypGSgtFTo + 183
    9   SwiftUI                             0x00000001160aa7b2 block_destroy_helper.140 + 15626
    10  SwiftUI                             0x00000001160aa88c block_destroy_helper.140 + 15844
    11  UIKitCore                           0x00000001270a42c3 -[UIApplication _handleDelegateCallbacksWithOptions:isSuspended:restoreState:] + 271
    12  UIKitCore                           0x00000001270a6186 -[UIApplication _callInitializationDelegatesWithActions:forCanvas:payload:fromOriginatingProcess:] + 4288
    13  UIKitCore                           0x00000001270abed2 -[UIApplication _runWithMainScene:transitionContext:completion:] + 1236
    14  UIKitCore                           0x00000001264c5277 -[_UISceneLifecycleMultiplexer completeApplicationLaunchWithFBSScene:transitionContext:] + 179
    15  UIKitCore                           0x00000001270a83d0 -[UIApplication _compellApplicationLaunchToCompleteUnconditionally] + 59
    16  UIKitCore                           0x00000001270a879b -[UIApplication _run] + 958
    17  UIKitCore                           0x00000001270ad5de UIApplicationMain + 123
    18  SwiftUI                             0x0000000115db55ed __swift_memcpy93_8 + 12403
    19  SwiftUI                             0x0000000115db54a6 __swift_memcpy93_8 + 12076
    20  SwiftUI                             0x00000001154cb610 __swift_memcpy195_8 + 14237
    21  iosAppName                          0x00000001003f25a3 $s10iosAppName0abC3AppV5$mainyyFZ + 35
    22  iosAppName                          0x00000001003f2b19 main + 9
    23  dyld                                0x000000010959c384 start_sim + 10
    24  ???                                 0x00000002011b141f 0x0 + 8608486431
)
libc++abi: terminating due to uncaught exception of type NSException```

 

Solution

  • After fighting for some time over this I found the solution.

    The app needs to be run as Rosetta simulator to get it to run without crash.

    There was also a problem with ContentView canvas not showing a preview, but I manage to fix this problem by setting Automatically Refresh Canvas off (Editor -> Canvas -> Automatically Refresh Canvas (uncheck this if its checked)).