WatchOS6 handleActiveWorkoutRecovery not being called after workout crashes - swift

I'm creating a workout app and I'm trying to implement the workoutRecovery logic but the handleActiveWorkoutRecovery function is never being called when an app crashes or is forced closed.
Are there any special circumstances that must be met so that the handleActiveWorkoutRecovery?
func handleActiveWorkoutRecovery() {
NSLog("log: handleActiveWorkoutRecovery");
self.healthInfo.recoverWorkout()
self.healthInfo.workoutRecovery = true
}
Moreover, in Content.swift I have the following code so that in case the login is not working I could see visual feedback that the handleActiveWorkoutRecovery function is being called:
#EnvironmentObject var healthInfo: HealthInfo
var body: some View {
VStack{
if(healthInfo.workoutRecovery){
Text("RECOVERY")
}else{
TestView()
}
}
}
When the app relaunches it never goes to The Text("RECOVERY") view.
Deployment target is 6.1 and I'm using an Apple Watch 4 running 6.1 beta 3
Ive also tested the native apple workout app and that seems to recover as it should

Related

SwiftUI: Error message when using .onMove on List built with forEach

I have an array containing some data that I want to present in a View. It should be possible to rearrange the data by holding and dragging it to its new position. I have used the .onMove modifier on a List generated using ForEach to iterate over every element in the list. My code works exactly as expected, but throws the following error/warning:
2022-10-12 18:48:52.833139+0200 Playground[1212:207280]
[EventDispatcher] Found no UIEvent for backing event of type: 11;
contextId: 0xFEEA6094
This only happens when using an actual device (iPhone 12 Pro Max on iOS 16.0.2), not when using the simulator (Xcode Version 14.0.1 (14A400), build in simulator of any device that runs iOS 16).
The code (the whole project is way bigger, but this is enough to trigger the error/warning):
struct ContentView: View {
#State var stuffList = ["Beer", "Chips", "Boardgames"]
var body: some View {
NavigationStack {
List {
ForEach(stuffList, id: \.self) { stuff in
Text(stuff)
}
.onMove { stuffList.move(fromOffsets: $0, toOffset: $1) }
}
}
}
}
Edit:
I have now tried different code snippets from online tutorials, e.g. this one (Hacking in Swift) and I still get this message no matter what I try. As my code seems to work as intended, I will continue my project and treat this as "log noise". If someone knows more about this than I do or simply can confirm having the same issue, it would be highly appreciated.

How can I get permanent access to files in macOS?

I am working in app that tells the count of folders and files exist on Desktop in macOS, for making things easier to my issue get solved I made a simple and small project that re create the problem. At first time of lunching the app I am asking the user to select the Desktop, after I am using the selected path for later use case when the app get killed, I do not want ask user each time to select the Desktop each time, it would be very bothering! currently sometimes macOS allows my app to get access to Desktop without showing the permission dialog but sometimes it goes ask each single time, I am trying to make my app authorized for such permission via user and use that permission for later use case in app. For example as you can see Terminal has such permission in the picture:
So here is my code with disabling sandbox:
struct ContentView: View {
#State private var contents: [String]? = nil
var body: some View {
VStack(spacing: 10.0) {
Text("Count of contents of Desktop is:")
Text(contents != nil ? String(contents!.count) : "Unknown").bold()
}
.frame(width: 200.0, height: 200.0)
.padding()
.onAppear {
contents = directoryContentsReader(folderLocation: "/Users/myNameHere/Desktop")
}
}
}
func directoryContentsReader(folderLocation: String) -> [String]? {
do {
return try FileManager.default.contentsOfDirectory(atPath: folderLocation)
}
catch(let error) {
print(error.localizedDescription)
return nil
}
}
And as said before most of the time and randomly I get this below dialog from macOS, I want make this kind permission done one time and for ever when the user use my app, and not bothering user with this dialog any more. How can I get this permission for my app?

Perform action after coming from a closed state - SwiftUI

I currently have this:
.onReceive(NotificationCenter.default.publisher(for: UIApplication.willEnterForegroundNotification)) { _ in
...
}
and that works when I don't quit the app, it simply is in the background, but when I quit it, the code inside there that is suppoused to check for a time, doesn't work.
I also tried putting it in
.onAppear() {
but that doesn't workout either :/
How can I once the app has been closed, then opened, as soon as it opens run an action?
you could try adding the following code to perform an action when your app starts:
.onReceive(NotificationCenter.default.publisher(for: UIApplication.didBecomeActiveNotification)) { _ in
// do your action here
....
}

SwiftUI view sometimes creates Thread 1: EXC_BAD_ACCESS (code=1, address=0x6002005b8368), when I put array as a parameter

SwiftUI View file
struct NoteListView: View {
#EnvironmentObject var presenter: NoteListPresenter
var body: some View {
NavigationView{
AddNoteButton()
Text(presenter.noteViewModels[0].title)
}
}
}
struct NoteListView_Previews: PreviewProvider {
static var previews: some View {
NoteListView()
}
}
I first got this error when I finished writing the logic and started to writing Views. After add my custom button AddNoteButton() to main view, it gave the following error, although it worked before I added this button. I tried to rewrite my views, but although it works at the beginning at some random point of time it throws this error again(by other words sometimes it works, but when i add some irrelevant code it stops working and discarding changes doesn't solve the problem). It always highlights line, where I use noteViewModels array inside of the NoteListView.
Also I added method which prints array to presenter and it always prints everything.
func printAll(){
for element in self.noteViewModels{
print(element.title)
}
}
I can not add other files to question, because there is too many of them. But i hope that I gave enough information, If you think that there is not enough information you are free to ask me or you can check out github:
https://github.com/ShynggysSaparbek/ViperNotesWithRealm/
Xcode version 11.3
Mac OS version Catalina 10.15.2

addUIInterruptionMonitor is not getting called on macOS

I want to test my macOS application. It uses your Macbook's camera, and want to handle this in my UITest. However I cannot get it working. Here is my NOT working code. This code triggers to notification, and I'm presented an alert to allow access to my camera, but the closure is not getting called. Thanks fo any help.
There are many solutions for iOS, but I need it on macOS.
let alertHandler = addUIInterruptionMonitor(withDescription: "Camera Permission Alert") { (alert) -> Bool in
if alert.buttons.matching(identifier: "OK").count > 0 {
alert.buttons["OK"].click()
self.app.click()
return true
} else {
return false
}
}
XCTAssertTrue(startButton.waitForExistence(timeout: 1.0))
startButton.click()
XCTAssertTrue(recordButton.waitForExistence(timeout: 20.0))
recordButton.click()
wait(for: 8)
recordButton.click()
removeUIInterruptionMonitor(alertHandler)
}
I managed to make interruption monitor work on macOS by adding an extra interaction after the interaction that triggers the system dialog (be it camera access or else). So in your example I would add an action after startButton.click() (if that is what triggers the camera access dialog).
Example:
func testCamera() {
let alertHandler = addUIInterruptionMonitor(withDescription: "Camera Permission Alert") { (alert) -> Bool in
if alert.buttons.matching(identifier: "OK").count > 0 {
alert.buttons["OK"].click()
self.app.click()
return true
} else {
return false
}
}
useCameraButton.click()
// try to interact with the app by clicking on the app's window
app.windows.first().click()
// at this point the handler should intercept the system interruption
// and blocks further execution until handler does return
// try to use the camera again
useCameraButton.click()
removeUIInterruptionMonitor(alertHandler)
}
Hint about this behaviour in Apple's documentation:
When an alert or other modal UI is an expected part of the
test workflow, don't write a UI interruption monitor. The test won’t
use the monitor because the modal UI isn’t blocking the test. A UI
test only tries its UI interruption monitors if the elements it needs
to interact with to complete the test are blocked by an interruption
from an unrelated UI.
https://developer.apple.com/documentation/xctest/xctestcase/handling_ui_interruptions