I'm sharing a Core Data store with my action extension, which adds data to it. My app has a SwiftUI view that presents this data, fetched with a FetchRequest:
private struct VideosScrollView: View {
@Environment(\.managedObjectContext) private var viewContext
private var fetchRequest: FetchRequest<Video>
init(sortOrder: String, tagIds: String, showWatched: Bool) {
fetchRequest = Video.getFetchRequest(sortingBy: "addDate",
with: sortOrder,
showWatched: showWatched,
tagIds: tagIds)
}
var body: some View {
ScrollView {
if fetchRequest.wrappedValue.isEmpty {
ContentEmptyView()
} else {
ForEach(fetchRequest.wrappedValue) { item in
VideoCellView(video: item)
}
}
}
}
}
After adding data from the action extension and going back to the app, the view is not updated. Only after CloudKit finished a sync is that the view notices something changed and updates itself.
How can I force my SwiftUI view to update when data changes in the action extension?
Selecting any option will automatically load the page
Post
Replies
Boosts
Views
Activity
Using SwiftUI's new Table container, how can I add a context menu that appears when Control-clicking a row?
I can add the contextMenu modifier to the content of the TableColumn, but then I will have to add it to each individual column. And it only works above the specific text, not on the entire row:
I tried adding the modifier to the TableColumn itself, but it shows a compile error:
Value of type 'TableColumn<RowValue, Never, Text, Text>' has no member 'contextMenu'
Here's what I have in terms of source code, with the contextMenu modifier in the content of the TableColumn:
struct ContentView: View {
@Environment(\.managedObjectContext) private var viewContext
@FetchRequest(sortDescriptors: [NSSortDescriptor(keyPath: \Item.name, ascending: true)])
private var items: FetchedResults<Item>
@State
private var sortOrder = [KeyPathComparator(\Item.name)]
@State
private var selection = Set<Item.ID>()
var body: some View {
NavigationView {
Table(items, selection: $selection, sortOrder: $items.sortDescriptors) {
TableColumn("Column 1") {
Text("Item at \($0.name!)")
.contextMenu {
Button(action: {}) { Text("Action 1") }
Divider()
Button(action: {}) { Text("Action 2") }
Button(action: {}) { Text("Action 3") }
}
}
TableColumn("Column 2") {
Text($0.id.debugDescription)
}
}
.toolbar {
ToolbarItem {
Button(action: addItem) {
Label("Add Item", systemImage: "plus")
}
}
}
if selection.isEmpty {
Text("Select an item")
} else if selection.count == 1 {
Text("Selected \(items.first(where: { $0.id == selection.first! })!.id.debugDescription)")
} else {
Text("Selected \(selection.count)")
}
}
}
}
So, how can I add a context menu to the entire row inside the Table?
I have a macOS Share Extension invoked when users tap the Share button in Safari. I'm trying to get the page's URL from the -[NSExtensionItem attachments] attribute, but it comes as an NSSecureCoding object, and I'm unable to read the URL from it.
In the loadView method, I'm filtering and loading the attachments of type public.url:
override func loadView() {
super.loadView()
guard let inputItem = extensionContext?.inputItems.first as? NSExtensionItem else {
print("Didn't received input item from action.")
return
}
var itemProvider: NSItemProvider?
itemProvider = inputItem.attachments?.filter({ $0.registeredTypeIdentifiers.contains("public.url") }).first ?? inputItem.attachments?.filter({ $0.registeredTypeIdentifiers.contains("public.plain-text") }).first
guard let itemProvider = itemProvider else {
print("Didn't received attachments from input item.")
return
}
if itemProvider.canLoadObject(ofClass: URL.self) {
itemProvider.loadItem(forTypeIdentifier: "public.url", completionHandler: onLoadVideoURL)
} else if itemProvider.canLoadObject(ofClass: String.self) {
itemProvider.loadItem(forTypeIdentifier: "public.plain-text", completionHandler: onLoadVideoURL)
} else {
print("This action only supports URL and String.")
}
}
The itemProvider.loadItem method runs for the type identifier public.url, calling the completion handler bellow:
@objc private func onLoadVideoURL(dict: NSSecureCoding?, error: Error?) {
print("URL: \(dict.debugDescription)")
// ...
}
But the content that it prints to the console is:
URL: Optional(<68747470 733a2f2f 73746163 6b6f7665 72666c6f 772e636f 6d2f7175 65737469 6f6e732f 35323231 39373030 2f686f77 2d746f2d 63617374 2d6e7373 65637572 65636f64 696e672d 746f2d6d 6b6d6170 6974656d 2d696e2d 61637469 6f6e2d65 7874656e 73696f6e>)
The same code works as expected on iOS, printing the shared URL to the console.
Do I have to somehow convert this NSSecureCoding to URL or another object? Or should I do this in a completely different way on macOS? The goal is to access the page's URL from the Share Extension activated when the user selects it in the Share Menu.
WWDC21 Session Focus on iPad keyboard navigation says that we can use UIFocusHaloEffect to change the appearance of focused items.
On iPadOS, we can use this effect by assigning a UIFocusHaloEffect to the focusEffect property like so:
self.focusEffect = UIFocusHaloEffect()
What wasn't very clear is where should we put this code when working with UICollectionViewCell. I am doing it in the collectionView(_:canFocusItemAt:) method:
func collectionView(_ collectionView: UICollectionView, canFocusItemAt indexPath: IndexPath) -> Bool {
guard #available(iOS 15.0, *) else { return false }
guard let cell = collectionView.cellForItem(at: indexPath) as? FeedCollectionViewCell else { return false }
if cell.focusEffect == nil {
cell.focusEffect = UIFocusHaloEffect(roundedRect: cell.artworkImageView.frame,
cornerRadius: cell.cornerRadius,
curve: .continuous)
}
return true
}
Is this the best way to implement it?
I have a screen with two List side by side, inside a NavigationView. The layout is rendered correctly, and I can independently scroll both lists. The problem is that, when I scroll the first list, it goes behind the navigation bar without triggering the effect of applying a background color to it. Here's a gif showing what's going on:
And this is the code I'm using for this view:
struct ContentView: View {
var body: some View {
NavigationView {
HStack(spacing: 0) {
List {
Section(header: Text("Header left")) {
ForEach(0..<600) { integer in
Text("\(integer)")
}
}
}
.listStyle(InsetGroupedListStyle())
.frame(minWidth: 0, maxWidth: .infinity)
List {
Section(header: Text("Header right")) {
ForEach(0..<400) { integer in
Text("\(integer)")
}
}
}
.listStyle(InsetGroupedListStyle())
.frame(minWidth: 0, maxWidth: .infinity)
}
.navigationTitle("Example")
}
.navigationViewStyle(StackNavigationViewStyle())
}
}
Would this be a SwiftUI bug? If not, how can I make the first list correctly interact with the navigation bar when scrolling?
My app has an action extension that allow users to collect and save links throughout the system. It also has a widget to show these links in the Home Screen.
When adding links from the main app, I call WidgetCenter.shared.reloadAllTimelines() to reload all widgets, and everything works as expected.
But when I add links from the action extension, widgets are not reloaded, even after calling WidgetCenter.shared.reloadAllTimelines(). Only when I go to the main app is that widgets do reload.
How can I refresh my widgets for changes made via the Action Extension from the share sheet?
My app uses Core Data to store user data. It doesn't have login or profile mechanisms, because user data is tied to the user's Apple ID and synced via iCloud using CloudKit.
I've recently added the User Management capability, and checked Runs as Current User to allow each Apple TV user to have access to its own data. Everything works as expected: I press and hold the TV button to select a different user in Control Center; The system shows a switching user UI; And the app is relaunched using the new user's Apple ID and data from iCloud.
The problem is that switching users apparently causes the local Core Data managed database to be reset, removing all data from the previous user. This is inconvenient because every time we switch users, the newly selected user has to wait for CloudKit to download all its data back into the app. And it takes a while, especially for users that have more than a few hundred items in the database.
Ideally, tvOS should maintain the user data on-device, and only switch to a different database according to the active user. This way data would not need to be downloaded from scratch every time a user is switched.
Is there a setting I can configure or a checkbox I can check to enable this behavior, or is this something not possible yet on tvOS?
Since updating to macOS Sequoia, I see this dialog every time I launch my SwiftUI macOS app from Xcode:
Users who installed the app from the App Store don’t see it. And this didn’t happened in previous macOS versions.
Could launching it from Xcode be triggering some extra access requirement? How can I stop this dialog from appearing every time I launch my app? It’s very disruptive to the debugging process.