Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Crashes when used with SwiftUI #1465

Open
ablaw123 opened this issue Jul 25, 2021 · 4 comments
Open

Crashes when used with SwiftUI #1465

ablaw123 opened this issue Jul 25, 2021 · 4 comments

Comments

@ablaw123
Copy link

When I call the researchkit from a swiftui button it crashes with the error "unrecognized selector sent to instance". Specifically, if I have a task that requires more than one screen.

Below is a sample of my code:

import SwiftUI
import UIKit
import ResearchKit

struct SurveyView: UIViewControllerRepresentable {
    @Environment(\.presentationMode) var presentationMode
    
    func makeCoordinator() -> Coordinator {
        Coordinator()
    }
    
    typealias UIViewControllerType = ORKTaskViewController
    func makeUIViewController(context: Context) -> ORKTaskViewController {

        let taskViewController = ORKTaskViewController(task: ConsentTask, taskRun: nil)
        taskViewController.view.tintColor = UIColor(red:0.64, green:0.15, blue:0.11, alpha:1.00)
        taskViewController.delegate = context.coordinator

        return taskViewController
    }
    
    func updateUIViewController(_ taskViewController: ORKTaskViewController, context: Context) {

    }
    
    class Coordinator: NSObject, ORKTaskViewControllerDelegate {
        func taskViewController(_ taskViewController: ORKTaskViewController, didFinishWith reason: 
            ORKTaskViewControllerFinishReason, error: Error?) {
            taskViewController.dismiss(animated: true, completion: nil)
        }
    }
}
@priyanka16
Copy link

Hi @ablaw123,
Did you find the solution for the problem you mentioned above. I'm trying to achieve the same behavior like you mentioned above.

But getting this crash:
[SwiftUI.AppDelegate window]: unrecognized selector sent to instance 0x600002aa8870
Xcode Version: 13.2.1
ResearchKit Version: 2.0.0

Thanks.

@raafaelima
Copy link

Hey Folks,

Due to the adoption of scenes and the scene delegate, the app delegate no longer has a window property by default. You must add a property var window: UIWindow? to your app delegate file.

import SwiftUI
import HealthKit

@main
struct MyApp: App {

    @UIApplicationDelegateAdaptor(AppDelegate.self) var delegate

    var body: some Scene {
        WindowGroup {
            ContentView()
        }
    }
}

final class AppDelegate: NSObject, UIApplicationDelegate {
    
    //Just need to add this
    var window: UIWindow?

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]? = nil) -> Bool {
        print("Application did finish launching with options")
        return true
    }
}

However, this "workaround" seems to make the modal have a transparent title. That you can solve this by setting the background color of the navigation bar to white (or the color of your preference) as @xinsight said in it answer. Also, you can use the same logic, and apply it to the ORKTaskViewController by setting the same properties, before presenting it.

let taskViewController = ORKTaskViewController(task: withdrawTask(), taskRun: nil)
taskViewController.delegate = self
taskViewController.navigationBar.backgroundColor = .white
// This step is optional
taskViewController.navigationBar.prefersLargeTitles = false
present(taskViewController, animated: true, completion: nil)

@raafaelima
Copy link

Also, if you are using a subclass of ORKTaskViewController for some reason, it will be at the center of the SwiftUI view and not be on the entire screen as you expect. This is a thing that I still trying to solve, but have no idea how yet.

Simulator Screen Shot - iPhone 14 Pro - 2022-11-28 at 10 48 57

@cbaker6
Copy link
Contributor

cbaker6 commented Nov 28, 2022

@raafaelima #1469 should have solved the clear nav bar issue for you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants