Use Your Loaf Blog
4 FOLLOWERS
Read regular articles about iOS development, Xcode, Objective-C, Swift, Auto Layout, WWDC, and more. Hi, I am Keith Harrison, I have worked in the software and I.T. services industry for many years. This is the place where I have been writing about my experiences learning and developing for iOS since 2010.
Use Your Loaf Blog
2w ago
Using the OSLog framework for logging in your Apps? How do you retrieve those logs at runtime to show them in your App? Here’s what worked for me.
Swift Logging - A Recap
I’ve written about using the OSLog unified logging framework to write log messages from Swift. Here’s a quick recap:
Import the OSLog framework:
import OSLog
Create a Logger object for each distinct area of your App you want to create logs for:
private let logger = Logger(subsystem: "com.useyourloaf.LogMaker",
category: "AppModel")
Create log entries at the appropriate level:
logger.debug("Initialising AppMode ..read more
Use Your Loaf Blog
1M ago
How do you change a SwiftUI binding to a boolean to give the inverted value?
Suppressing A Confirmation Dialog
Here’s my situation. When a user deletes items, I present a SwiftUI confirmation dialog before proceeding with the operation. A confirmation dialog has a few extra options on macOS compared to iOS. For example, you can include a checkbox to allow the user to opt-out of getting future confirmations:
.confirmationDialog("Delete all items?", isPresented: $isConfirmingDelete) {
Button("Delete", role: .destructive) { ... }
}
.dialogSuppressionToggle(isSuppressed: $isSuppressed)
The ..read more
Use Your Loaf Blog
2M ago
If you’re still using print statements to debug your App it’s time you moved to the unified OS logging framework.
Why Use Unified Logging?
Apple introduced the unified logging framework for Swift in iOS 14 but it took until Xcode 15 for it to become integrated into the debug console. Log entries include metadata to classify each entry by subsystem, category, and type, the process, and the source call site. The log type determines whether the system archives the log to persistent storage.
As Quinn explains in this Apple Developer Forums post this has some advantages:
It’s fast so you can leave ..read more
Use Your Loaf Blog
2M ago
How do you stop the Core Data CloudKit sync logging from filling the Xcode console?
Core Data CloudKit Logging
If you’re using CloudKit to sync Core Data, or Swift Data, you’ll be familiar with the noise it creates in the Xcode console:
CoreData: CloudKit: CoreData+CloudKit: -[NSCloudKitMirroringDelegate
executeMirroringRequest:error:](969): <NSCloudKitMirroringDelegate:
0x600003d143c0>: Asked to execute request: <NSCloudKitMirroringExportRequest:
0x600002119680> FAA2E23B-D458-4527-BAF6-679945DB3934
CoreData: CloudKit: CoreData+CloudKit: -[NSCloudKitMirroringDelegate
_enqueueReq ..read more
Use Your Loaf Blog
3M ago
Apple added the App Intents framework in iOS 16 giving us a way to programmatically make an app’s content and actions available to the Shortcuts app, Siri, Widgets, and Spotlight. Here’s my quick guide to get started.
App Intents
I don’t think I ever understood the old system of creating custom intents using Xcode Intent Definition files. Fortunately App Intents, a Swift-only framework introduced in iOS 16, replaces all of that.
App Intents show up in a lot of places. You use them to add support for App Shortcuts to Siri, the App Shortcuts App, the iPhone Action button, Spotlight and also in i ..read more
Use Your Loaf Blog
3M ago
Each time I need to style a SwiftUI button I find myself struggling to remember which view modifier I need. Here’s a quick recap of the most common options.
Creating A Button
You create a button with a label, an optional role, and an action to run when the user clicks or taps on the button. There are many ways to create the button depending on what you use for the label view. First the full initializer:
Button(role: .destructive, action: deleteAll, label: {
Label("Delete All", systemImage: "trash")
})
The possible roles are .cancel, .destructive, or .none, which is the default if you omi ..read more
Use Your Loaf Blog
4M ago
When you fetch data using Core Data it includes pending changes by default. SwiftData, in theory, works the same way let’s see how it works in practise.
Fetching Pending Changes
Here’s a SwiftData fetch request with a predicate that returns all visited country model objects, sorted by name:
let descriptor = FetchDescriptor<Country>(
predicate: #Predicate { $0.visited },
sortBy: [SortDescriptor(\.name,
comparator: .localizedStandard)])
let countries = try context.fetch(descriptor)
The Core Data SQLDebug flag also works for SwiftData so we can see the SELECT s ..read more
Use Your Loaf Blog
4M ago
There are at least three ways to delete data from a SwiftData store. Here’s a quick recap.
Delete A Single Item
If you only want to delete a single object, call the delete method on a model context passing the model object you want to delete:
context.delete(country)
The method doesn’t throw or return a value. If you’re not using autosave the SwiftData documentation recommends you save the context after deletion. A few observations that I’ve not found well documented:
After the delete operation completes, but before you save (or autosave), the isDeleted property is true for the deleted mode ..read more
Use Your Loaf Blog
5M ago
In iOS 17, Apple added control over which column shows when a Split View collapses to a single column.
NavigationSplitView
Apple added NavigationSplitView to SwiftUI in iOS 16 for creating two or three column layouts. The Split View configuration available in iOS 16 includes controls for column visibility, width, and style. It was missing API for controlling which column the split view shows when collapsing to a single column for compact widths. Apple fixed that gap in iOS 17 (and macOS 14).
Preferred Compact Column
When a split view collapses to a single column the default behaviour is for Sw ..read more
Use Your Loaf Blog
6M ago
Starting with iOS 17 you can add your own custom traits to the UIKit trait environment and have them interoperate with the SwiftUI environment.
Trait Changes in iOS 17
Apple added the UITraitEnvironment protocol way back in iOS 8. It adds a traitCollection property and traitCollectionDidChange method to conforming types including UIScreen, UIWindowScene, UIView and UIViewController.
In iOS 17, Apple made significant changes to the handling of traits and deprecated some earlier trait related methods. See registering for trait changes for the new way to observe trait changes that deprecates trai ..read more