I use SwiftUi I want to call API when app is in background and send notifications to users
This is my code to call API. I found this way from Apple Dec - https://developer.apple.com/documentation/uikit/app_and_environment/scenes/preparing_your_ui_to_run_in_the_background/updating_your_app_with_background_app_refresh#2928664 for Background fetch But this for UIkit
}
.onAppear(perform: loadData)
}
func loadData() {
guard let url = URL(string: "My API") else {
print("Invalid URL")
return
}
let request = URLRequest(url: url)
URLSession.shared.dataTask(with: request) { data, response, error in
if let data = data {
if let decodedResponse = try? JSONDecoder().decode([Result].self, from: data) {
DispatchQueue.main.async {
self.results = decodedResponse
}
return
}
}
print("Fetch failed: \(error?.localizedDescription ?? "Unknown error")")
}.resume()
}
}