law
law

Reputation: 21

Can't use Researchkit with SwiftUI

I am using researchkit within a SwiftUI app for a study I'm conducting. Everytime I call on a researchkit Task the app crashes with the following error:

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[SwiftUI.AppDelegate window]: unrecognized selector sent to instance 0x600000c511c0' terminating with uncaught exception of type NSException

I'm having a bit of a time debugging at present. The code is below:

struct ActivityView: UIViewControllerRepresentable {
var activity: Activity
var onCompleted: (Bool) -> Void

func makeUIViewController(context: Context) -> ORKTaskViewController {
    let result: ORKTaskViewController
    switch activity {
    case .survey:
        result = ORKTaskViewController(task: StudyTasks.surveyTask, taskRun: NSUUID() as UUID)
    
    case .microphone:
        result = ORKTaskViewController(task: StudyTasks.microphoneTask, taskRun: NSUUID() as UUID)
        
        do {
            let defaultFileManager = FileManager.default
            
            // Identify the documents directory.
            let documentsDirectory = try defaultFileManager.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: false)
            
            // Create a directory based on the `taskRunUUID` to store output from the task.
            let outputDirectory = documentsDirectory.appendingPathComponent(result.taskRunUUID.uuidString)
            try defaultFileManager.createDirectory(at: outputDirectory, withIntermediateDirectories: true, attributes: nil)
            
            result.outputDirectory = outputDirectory
        } catch let error as NSError {
            fatalError("The output directory for the task with UUID: \(result.taskRunUUID.uuidString) could not be created. Error: \(error.localizedDescription)")
        }
        
    case .tapping:
        result = ORKTaskViewController(task: StudyTasks.tappingTask, taskRun: NSUUID() as UUID)
    
    case .trailmaking:
        result = ORKTaskViewController(task: StudyTasks.trailmakingTask, taskRun: NSUUID() as UUID)
    }
    result.view.window?.tintColor = UIColor(named: "AccentColor")
    return result
}

func updateUIViewController(_ uiViewController: ORKTaskViewController, context: Context) {
    uiViewController.delegate = context.coordinator
}

func makeCoordinator() -> Coordinator {
    Coordinator(onCompleted: onCompleted)
}

class Coordinator: NSObject, ORKTaskViewControllerDelegate {
    var onCompleted: (Bool) -> Void

    init(onCompleted: @escaping (Bool) -> Void) {
        self.onCompleted = onCompleted
    }

    public func taskViewController(_ taskViewController: ORKTaskViewController, didFinishWith reason: ORKTaskViewControllerFinishReason, error: Error?) {
        self.onCompleted(reason == .completed)
    }
    
    func taskViewController(_ taskViewController: ORKTaskViewController, viewControllerFor step: ORKStep) -> ORKStepViewController? {
        return nil
    }
}

}

Upvotes: 0

Views: 289

Answers (1)

law
law

Reputation: 21

I think there's a bug in ResearchKit. In the ORKNavigationContainerView.m

Change

_appTintColor = [[UIApplication sharedApplication].delegate window].tintColor;

to

if (@available(iOS 13.0, *)) {
_appTintColor = [self window] .tintColor;
// not sure if it's this or [[self window].windowScene windows].firstObject .tintColor;} else {
_appTintColor = [[UIApplication sharedApplication].delegate window].tintColor;}

Upvotes: 1

Related Questions