Post

Replies

Boosts

Views

Activity

URLSession Token Authentication: What's the 'correct' way to do it?
I'm working on an API client for a REST service that uses a custom token-based authentiation scheme. The app hits a specificed authentication endpoint with a username and password, said endpoint returns a token that's good for X amount of time, and the app passes that token along with every subsequent request. When that token expires, we start over.Most literature out there tells me to manually set the Authorization header on my request, but official Apple documentation discourages this, as that header is meant to be 'owned' by the built-in HTTP loading system. That said, official documentation on the 'correct' way to do this is shockingly lacking, and the standard didReceiveChallenge callbacks seem better suited for non-custom Basic/Digest/etc authentication schemes. One thought I had was registering my own URLProtocol subclass to handle our custom flow. However, while I haven't had a chance to sit down and take a crack at that yet, my understanding from skimming these forums is that it's suffering from some bit-rot right now, so it 'might' (?) not be the best choice. That, and it's also not clear to me whether the rules around the Authorization header change when a custom URLProtocol is in play.So, community (paging eskimo in particular!), what's the correct way for me to go about this?
4
3
9.5k
Oct ’17
Adding Run Script build phase ouptut files to target
We have an Aggregate target with a custom Run Script build phase that consumes a JSON file in the project and outputs a .swift file and an .xcassets folder. However, getting our primary app target to see and include these output files has required some hacky measures (that I'd rather not delve into), and I'm wondering if there's something I'm missing. What's the defacto way of ensuring that Build Phase output files get included as sources file in an app build?
0
0
1k
Oct ’21
Localized CNPostalAddress field labels
How can I extract the localized field labels on per-CNPostalAddress instance? For example, if I the following: let address1 = CNMutablePostalAddress() address1.isoCountryCode = "US" let address2 = CNMutablePostalAddress() address2.isoCountryCode = "CA" How can I extract the State string from address1 and Province string from address2?
0
0
562
Apr ’23
[SwiftUI] SecureEntry Autofill in Dark Mode
When using New Password Autofill in Dark Mode, it appears that SecureEntry sets the background color to white and applies a yellow-ish overlay, but doesn't adapt the foreground text color accordingly. This gives the illusion that the SecureEntry field is empty, as we have white text on a white background. Is there a holistic and SwiftUI-native way of fixing this?
0
0
37
Apr ’25
`.task(id:_:)` starting in the `Task.isCancelled` state
I'm running into a weird SwiftUI concurrency issue that only seems to happen on a real device (iOS 18.5 in my case), and not in simulator. I have a NavigationSplitView where the Detail view uses .task(id:_:) to perform some async work upon appearance (in my real-world case, a map snapshot). When running this on a real device, the first execution of the task works as expected. However, any task subsequent executions, even ones for the same id, always start in the Task.isCancelled state. Is there something about .task(id:_:) that I'm misunderstanding, or have I stumbled upon a serious bug here? import SwiftUI struct ContentView: View { var body: some View { TaskBugSplitView() } } struct TestItem: Identifiable, Hashable { var id: Int var name: String } struct TaskBugSplitView: View { @State private var selectedItemIndex: [TestItem].Index? @State private var testItems: [TestItem] = [ TestItem(id: 1, name: "First Item"), TestItem(id: 2, name: "Second Item"), TestItem(id: 3, name: "Third Item"), TestItem(id: 4, name: "Fourth Item"), TestItem(id: 5, name: "Fifth Item") ] var body: some View { NavigationSplitView { List(testItems.indices, id: \.self, selection: $selectedItemIndex) { item in Text(testItems[item].name) } } detail: { if let selectedItemIndex { TaskBugDetailView(item: testItems[selectedItemIndex]) } else { Text("Select an item") .foregroundStyle(.secondary) } } } } struct TaskBugDetailView: View { @State var item: TestItem @State private var taskResult = "Not started" var body: some View { VStack(spacing: 20) { Text("Item: \(item.name)") .font(.title2) Text("Task Result:") .font(.headline) Text(taskResult) .foregroundStyle(taskResult.contains("CANCELLED") ? .red : .primary) Spacer() } .padding() .navigationTitle(item.name) .task(id: item.id) { // BUG: On real devices, Task.isCancelled is true immediately for all tasks // after the first successful one, even though the ID has changed if Task.isCancelled { taskResult = "CANCELLED at start" print("Task cancelled at start for item \(item.id)") return } taskResult = "SUCCESS" print("Task completed successfully for item \(item.id)") } } }
2
0
56
Aug ’25