Post

Replies

Boosts

Views

Activity

Calling app code in the background when interacting with iOS 14 widgets
My goal is simple: have an app "run in the background" (like the game example in the widgets code along sequence, where rangers have their health refill in the background based on a timer), and have my widget show this status. So far so good, however I would like to achieve this functionality : when tapping on one of the widget's interactable areas, I would like some code in my app to run without launching the app in full screen. Example : the rangers are regenerating their health. When their health is full, the widget displays a "go to battle" button, and when pressed, this starts a "battle" timer (similar to the "healing" timer). At the end of the battle timer, rangers go back to their healing state. All of this without explicitly opening the game. So in my use case, I want to be able to press that "to battle!" button and start my Swift code that will update my rangers status, send them to battle and setup new timers. Right now, with my current knowledge, I know I can call an app URL through a widget tap to open my app in a specific location, but that would open it in fullscreen and that's what I want to avoid (I don't want the app to be launched "explicitely" for the end user). Is there any way for me to achieve this result? Some iOS native way to run an app's code portion in the background? Or to open the app ever-so-briefly and immediately close it (not ideal but could work)?
1
0
934
Jul ’20
Widgets refreshing continuously, non-stop, not respecting timeline
Hey, I'm giving a first go at the brand new iOS 14 Widgets. I tried setting up a very simple widget (directly taken from both the documentation examples and the default generated project). Right now my goal is to have a simple widget that : Refreshes every 10 seconds Displays a random number on every refresh Here's my code : struct Provider: TimelineProvider {   public typealias Entry = SimpleEntry   public func snapshot(with context: Context, completion: @escaping (SimpleEntry) -> ()) {     let entry = SimpleEntry(status: "Populating", date: Date())     completion(entry)   }   public func timeline(with context: Context, completion: @escaping (Timeline&lt;Entry&gt;) -> ()) {     print("Going through timeline.")     let date = Date()     let entry = SimpleEntry(status: String(Int.random(in: 0 ..< 10)), date: date)           let nextUpdateDate = Calendar.current.date(byAdding: .minute, value: 15, to: date)!           let timeline = Timeline(       entries: [entry],       policy: .after(nextUpdateDate)     )           completion(timeline)   } } struct SimpleEntry: TimelineEntry {   public let status: String   public let date: Date } struct PlaceholderView : View {   var body: some View {     Text("Placeholder View")   } } struct WidgetAdventureWidgetExtensionEntryView : View {   var entry: Provider.Entry   var body: some View {     Text(entry.status)   } } In this example I'm trying to have a refresh every 15 minutes, but when ran on simulator, the widget seems to update continuously and random numbers keep getting re-drawn. Like almost 8-9 per second. My print line gets displayed non stop too. Any idea what I'm doing wrong ?
9
0
7.6k
Jun ’20