Posts

Post not yet marked as solved
0 Replies
307 Views
I have an Objective-C app with a new iOS 14 Widget (a Swift target), and I want to use a few methods from an Objective-C class in my Swift target, but am having one issue with it. The class is called SharedUtils, and the methods are marked as class methods in the .h file. I don't create instances of this class, I just use the methods. One of the methods I want to use is: (NSString *)symbolForCategoryId:(NSNumber *)categoryId; This works fine in the Objective-C main app, i.e.: [SharedUtils symbolForCategoryId:myId]; In the Swift target, I start typing: SharedUtils.symbol ... and Xcode autocompletes to: SharedUtils.symbol(forCategoryId: NSNumber(value: myId)) If I don't set the bridging header in the widget target's build settings, or I remove the import from within the bridging-header.h file, Xcode cannot autocomplete the line, so I know that the Swift target can see the SharedUtils code. All good so far. However, when trying to build the code, Xcode errors with: Cannot find type 'SharedUtils' in scope Any idea how to get this to work? I've used Objective-C code in a Swift app before and not had an issue, but the code was all in the same target, so I'm thinking it's something to do with that?
Posted
by darkpaw.
Last updated
.
Post not yet marked as solved
1 Replies
683 Views
I've read the documentation Apple have provided on the "Creating a Widget Extension" page, but it doesn't seem to match what's happening. My app lets users create events, and the widget should let users choose an event to display. You can have multiple widgets, all displaying different events. When the user installs the app for the first time and launches it, there are no events, so trying to add a widget will display some preview data (a fake event) to give the user an idea of what the widget will look like. In the placeholder func I return the preview data, and it does show this fake event when you're in the preview screen (the panel with the "Add Widget" button on it). placeholder() is called twice because I'm using both systemSmall and Medium widget sizes. getSnapshot() is then called, and the context.isPreview is true or the 'configuration' parameter is nil so I return the preview data again. This works fine. Once the user adds the widget to the Home Screen, they get a different view that tells them to edit the widget (because no event has actually been chosen yet). This is set up in both getSnapshot() and getTimeline(). So, the user sees this widget telling them to edit it and select an event, or tap it to launch the app and create an event. The user tries to edit the widget and there are no events listed. Fine. All works well so far. The user taps the widget and adds an event in the app. They go back to the Home Screen and edit the widget. Their event is present, so they select it. getTimeline() is called, which calls a function to get the event the user selected. This works properly and returns the correct event object for the selected event, e.g. "Paris Trip". getTimeline() then confirms it has the event and sets up the timeline, five entries, 5 minutes apart. But the widget on the Home Screen still displays the preview data. I can wait five minutes, an hour, a day and it will not change. So, based on the above and the following code, how do I get the widget to actually work? func placeholder(in context: Context) -> EventEntry { return entryPreviewData } func getSnapshot(for configuration: DynamicEventSelectionIntent, in context: Context, completion: @escaping (EventEntry) -> Void) { var entry: EventEntry if(context.isPreview || configuration.event == nil) { entry = entryPreviewData } else { EventDetail.getUpdatedEventsFromDefaults() if(EventDetail.availableEvents .isEmpty) { entry = entryNoEventsData } else { let selectedEvent = event(for: configuration) if(selectedEvent.type == kEventTypeNoEvents) { entry = entryNoEventsData } else if(selectedEvent.type == kEventTypeError) { entry = entryErrorData } else { entry = EventEntry(date: Date(), relevance: nil, event: selectedEvent) } } } completion(entry) } func getTimeline(for configuration: DynamicEventSelectionIntent, in context: Context, completion: @escaping (Timeline<EventEntry>) -> Void) { var entries: [EventEntry] = [] var timeline: Timeline<EventEntry> if(context.isPreview || configuration.event == nil) { entries.append(entryNoEventsData) timeline = Timeline(entries: entries, policy: .after(Date().advanced(by: 15))) } else { let selectedEvent = event(for: configuration) if(selectedEvent.type == kEventTypeNoEvents) { /* There are no events, so return the no events data */ entries.append(entryNoEventsData) timeline = Timeline(entries: entries, policy: .after(Date().advanced(by: 15))) } else if(selectedEvent.type == kEventTypeError) { /* Received error data, so return the error data */ entries.append(entryErrorData) timeline = Timeline(entries: entries, policy: .after(Date().advanced(by: 15))) } else { var entryDate: Date = Date() for _ in 0 ..< 5 { entryDate = Calendar.current.date(byAdding: .minute, value: 5, to: entryDate)! entries.append(EventEntry(date: entryDate, relevance: relevance, event: selectedEvent)) } timeline = Timeline(entries: entries, policy: .atEnd) } } completion(timeline) }
Posted
by darkpaw.
Last updated
.
Post not yet marked as solved
3 Replies
554 Views
I just experienced this problem, and thought it might help someone else out later. User adds a widget to their home screen. The widget has dynamic options, so they edit the widget and select a value. In my case, these are events, so the user selects their "Paris Trip". The widget shows the Paris Trip data and all is well. The user then edits the event in the main app and renames it to "Paris Vacation". The widget on the home screen clears because the identifier that that widget was created for (i.e. the name of that event) no longer exists. Here's my fix... The default settings for a dynamic selection intent's Type is to have two properties: identifier and displayString. The settings for these two are greyed-out (because they shouldn't be changed). But, you can add a new property into there, so I added a unique identifier for the event, "uniqueId". Now, here's a gotcha. This doesn't show up anywhere until the supporting files for the intent are recreated in the background. The easiest way I found of forcing this was to quit and restart Xcode. Here's my provideEventOptionsCollection() func in IntentHandler.swift: func provideEventOptionsCollection(for intent: DynamicEventSelectionIntent, with completion: @escaping (INObjectCollection<Event>?, Error?) -> Void) { let events: [Event] = EventDetail.availableEvents.map { event in let returnEvent = Event( identifier: event.name, display: event.name ) returnEvent.uniqueId = event.uniqueId. /* <-- THIS IS THE UPDATE */ return returnEvent } let collection = INObjectCollection(items: events) completion(collection, nil) } As you can see, you can access the uniqueId property of the event object and add it to the event you're returning. In my IntentTimelineProvider the event func is now this: if let uniqueId = configuration.event?.uniqueId { if let event = EventDetail.eventFromUniqueId(uniqueId: uniqueId) { return event } } and the eventFromUniqueId() func in EventDetail: static func eventFromUniqueId(uniqueId: String) -> EventDetail? { return (availableEvents).first(where: { (event) -> Bool in return event.uniqueId == uniqueId }) } Hope this helps someone out there.
Posted
by darkpaw.
Last updated
.
Post not yet marked as solved
1 Replies
1.1k Views
I have a widget that shows one "event" at a time, so you can have as many copies of the widget as you like, each one showing a different event. This all works fine. I'm trying to get the main app to open onto a specific page when the user taps a specific widget. For example, the user taps the "Birthday" widget, and the app should show the Birthday page. In my widget view, I have this on the View object: .widgetURL(URL(string: "widget_PreviewEvent:" + event.name)) This sends the text "widget_PreviewEvent:Birthday" to the app. The App's delegate implements the method: application:openURL:options: I have it logging this out: Delegate: openURL: url = 'widget_PreviewEvent:Birthday' Main view: showScreen: 'widget_PreviewEvent:Birthday' Found eventName from url = 'Birthday' Right, so that works fine. I can tell the app to open the Birthday event, and it does. However, sometimes, and especially more so on the medium widget, this method is called instead: application:continueUserActivity:restorationHandler: and the userActivity object is this: userActivity = DynamicEventSelectionIntent, userActivity.userInfo = {     WGWidgetUserInfoKeyFamily = systemMedium;     WGWidgetUserInfoKeyKind = "com.company.product.widget"; /* example */ } "Birthday" isn't mentioned in that object anywhere, so I can't open the Birthday event. How do I fix this?
Posted
by darkpaw.
Last updated
.
Post marked as solved
2 Replies
1.4k Views
Does anyone have complete instructions - including ALL steps and information on everything that needs to be done - and example code that we can use to reload SwiftUI widgets from an Objective-C main app? It's all well and good telling us to look at a page that gives us generic info on importing Swift header files, but that doesn't really help; it just gives us yet another thing to go searching for actual info on. There are no classes in my widget code; just structs, extensions and some funcs to figure out dates, so there's nowhere to make anything public or to add "@objc" to. If it is possible to reload a SwiftUI widget from an Objective-C main app, then Apple should provide us with such code. That would be really simple for them to do. Yeah, widgets are new, which is why the source of such information should be Apple themselves, not from random pages you find on the internet. These forums don't have anywhere near as many responses from Apple engineers as are required. (I asked five days ago how to format a Text.init(theDate, style: .timer) - no response.) Yes, this may seem a little like a rant, but why doesn't Apple give us example code for their new features? Not every app has been written in Swift, and for many it would be too costly to convert.
Posted
by darkpaw.
Last updated
.
Post not yet marked as solved
0 Replies
207 Views
In this thread: https://developer.apple.com/forums/thread/650535 an Apple Developer Tools Engineer wrote: 66 You can use SwiftUI's new Text initializer to display the relative time to a certain timestamp. For more information you can checkout: https://developer.apple.com/documentation/swiftui/text/init(_:style:) This Text view updates automatically without you needing to provide new entries for every second. 99 This does work, but there's no way of formatting the result, so a date 24 days 8 hours into the future shows as "584:00:00" and counts down like "583:59:59", "583:59:58" etc. I'd like it to say something like "24 days, 08:00:00". Is there any way of doing that?
Posted
by darkpaw.
Last updated
.
Post marked as solved
2 Replies
515 Views
I have an app that currently uses MagicalRecord to store its data. As of 2017, MR is toast, so I've decided to remove it and go with standard Core Data (since it's been updated and is easier to use now). My problem is that I can't seem to get the new Core Data methods to see the data in the store. Here's how it's set up: Old version (MagicalRecord): [MagicalRecord setupCoreDataStackWithAutoMigratingSqliteStoreNamed:@"MyModel"]; New version (Core Data): self.persistentContainer = [[NSPersistentContainer alloc] initWithName:@"MyModel"]; [self.persistentContainer loadPersistentStoresWithCompletionHandler:^(NSPersistentStoreDescription *description, NSError *error) { if(error != nil) { NSLog(@"Failed to load Core Data stack: %@", error); abort(); } else { self.viewContext = self.persistentContainer.viewContext; } }]; If I load the old version into Xcode 12b6 and run it on the iOS 13.7 Simulator I can read, update and delete data in the store, and it all works properly as before. When I load the new code into Xcode 12b6 and run it in the same iOS 13.7 Simulator, the app launches but there is no data in the store. Any data in the old version is not seen in the new version, and vice versa. It's like they're two different stores of data. The new version can see the store because I change the name to, say, "MyModel99" it errors saying it can't find MyModel99. I can re-launch the old code into the Simulator and the data is there, so I think the data model is all fine, it's just that I can't seem to get the new code to read that data. Any ideas? I'm guessing there's something I'm missing when setting up the persistent container etc.? I guess the question is: how do you migrate data from MR to Core Data? If MR is basically a wrapper for CD, why can't I see the data in it? Thanks.
Posted
by darkpaw.
Last updated
.
Post not yet marked as solved
2 Replies
1.6k Views
Hi. I have an iOS app with a Watch app that works generally well, but I can't seem to get the Watch app to update with data from the iOS app after it's been suspended/killed by watchOS 4.The Watch app has complications, which are self-updating (the data is based on dates, so no transfer from iOS - Watch is necessary). The data in the Watch app is richer than that shown in the complications, and includes images etc.The moment watchOS kills my app when it hasn't been running for a while, it wipes the various bits of data it had in memory, so when I launch the app again, it says there's no data, then spends a few seconds retrieving data from the iOS app, which sometimes works and sometimes doesn't. This looks bad on the Watch, and it's missing any updates the user may have performed in the iOS app.I should be able to launch the Watch app and see the most up to date information.I'd like to avoid this situation by using a background task in the Watch app to wake up the iOS app and retrieve data at regular intervals, but all the examples I see use a URLSession within the scheduleBackgroundRefresh(withPreferredDate:userInfo:scheduledCompletion:)method. I don't get my data from a server; I get it from the iOS app, and it's all user-created, so there's no server involved anyway.https://developer.apple.com/documentation/watchkit/wkapplicationrefreshbackgroundtaskSo, how do I get the Watch app to wake up the iOS app at regular intervals, and retrieve its data so that the Watch app is up to date?Some code:In the init() method of the Watch app's ExtensionDelegate.m:// Schedule a background refresh task NSDateComponents *offsetComponents = [[NSDateComponents alloc] init]; [offsetComponents setHour:1]; NSDate *refreshDate = [[NSCalendar autoupdatingCurrentCalendar] dateByAddingComponents:offsetComponents toDate:[NSDate date] options:0]; [[WKExtension sharedExtension] scheduleBackgroundRefreshWithPreferredDate:refreshDate userInfo:@{} scheduledCompletion:^(NSError * _Nullable error) { if(error == nil) { // Successfully scheduled } else { NSLog(@"Error scheduling background refresh task for date: %@", refreshDate); } }];This should tell the Watch app to schedule a background refresh task for an hour from now.The handleBackgroundTasks() method in the Watch app's ExtensionDelegate.m:- (void)handleBackgroundTasks:(NSSet *)backgroundTasks { for(WKRefreshBackgroundTask *currentTask in backgroundTasks) { NSLog(@"currentTask class = '%@'", [currentTask class]); NSLog(@"currentTask.userInfo = %@", currentTask.userInfo); if([[WKExtension sharedExtension] applicationState] == WKApplicationStateBackground) { if([currentTask isKindOfClass:[WKApplicationRefreshBackgroundTask class]]) { // Is it here that I get the data from the iOS app? HOW? // Schedule another background refresh task NSDateComponents *offsetComponents = [[NSDateComponents alloc] init]; [offsetComponents setHour:1]; NSDate *refreshDate = [[NSCalendar autoupdatingCurrentCalendar] dateByAddingComponents:offsetComponents toDate:[NSDate date] options:0]; [[WKExtension sharedExtension] scheduleBackgroundRefreshWithPreferredDate:refreshDate userInfo:@{} scheduledCompletion:^(NSError * _Nullable error) { if(error == nil) { // Successfully scheduled } else { NSLog(@"Error scheduling background refresh task for date: %@", refreshDate); } }]; [currentTask setTaskCompletedWithSnapshot:false]; } [currentTask setTaskCompletedWithSnapshot:true]; } } }How do I get the data from the iOS app? The iOS app may be (is more often than not) in the background.Thanks.
Posted
by darkpaw.
Last updated
.