I've been relishing Develop in Swift: Fundamentals as an introduction to Swift. It's wonderful!
Having limited time, and ultimately targeting visionOS development, I've reached a point where any UIKit content feels like a distraction from my goals. Hence I'm switching to
SwiftUI Bootcamp by Swiftful Thinking on YouTube, SwiftUI Concepts Tutorials, and SwiftUI Tutorials by Apple.
Is there any official word on updates to the Develop in Swift series? Failing that, any other recommendations on how to bridge between Fundamentals and visionOS development?
Post
Replies
Boosts
Views
Activity
Hi all,
I have a problem with trying to use UIApplication.shared.canOpenURL to open a specific web extension in the safari settings.
When executing the code below the safari extension menu is shown but not the settings for the specific extension:
if let url = URL(string: "App-prefs:SAFARI&path=WEB_EXTENSIONS/NAME_OF_EXTENSION") {
if UIApplication.shared.canOpenURL(url) {
UIApplication.shared.open(url, options: [:], completionHandler: nil)
}
}
However, the weird thing is that when executing the above I can see some kind of an event that looks like it tries to click the specific extension. Furthermore, if I keep the settings open and tries to execute the code again it actually works, and the specific web extension's settings is open.
Hope someone can help.
I recently created a project, originally in xCode 13.3 if i am not miistaken. Then i update it to 13.4 then update to mac os 15.6 as well.
Previously the project worked fine, but then i notice if i activate breakpoints the projects stopped when i run it in xCode but it worked fine like before, without breakpoints activated.
Also, when i build a .app of the project, the .app crashed exactly when the breakpoints previously stopped.
I am very confused on how to continue, would like to get help from anyone regarding the issue. From what i can gather from the breakpoints and crash report, it's got something about UUIID registration? AV foundation?
I am so confused. Please Help!
Thanks.
I am trying to count a database table from inside some of my classes.
I am tying to do this below **(My problem is that count1 is working, but count2 is not working.)
**
class AppState{
private(set) var context: ModelContext?
....
func setModelContext(_ context: ModelContext) {
self.context = context
}
@MainActor
func count()async{
let container1 = try ModelContainer(for: Item.self)
let descriptor = FetchDescriptor<Item>()
let count1 = try container1.mainContext.fetchCount(descriptor)
let count2 = try context.fetchCount(descriptor)
print("WORKING COUNT: \(count1)")
print("NOTWORKING COUNT: \(count2) -> always 0")
}
I am passing the context like:
...
@main
@MainActor
struct myApp: App {
@State private var appState = AppState()
@Environment(\.modelContext) private var modelContext
WindowGroup {
ItemView(appState: appState)
.task {
appState.setModelContext(modelContext)
}
}
.windowStyle(.plain)
.windowResizability(.contentSize)
.modelContainer(for: [Item.self, Category.self]) { result in
...
}
Can I get some guidance on why this is happening?
Which one is better to use?
If I should use count2, how can I fix it?
Is this the correct way to search inside an application using SwiftData ?
I don't wanna search using the View like @Query because this operation is gonna happen on the background of the app.
Hi everyone,
I'm currently working with the Screen Time API, specifically trying to figure out the best way to pause an active, repeating schedule without disrupting future schedules. Here's the scenario:
I have a repeating schedule set from 10:00 AM to 8:00 PM daily. If I need to pause the schedule temporarily, my current approach involves stopping monitoring, clearing all restrictions, and then setting a new schedule for the remaining time after the pause. However, this method cancels the repeating schedule entirely, which causes issues when the schedule is supposed to restart the next day at 10:00 AM. Instead, it starts after the pause time, which isn’t what I want.
Challenges I'm Facing:
Maintaining Repeating Schedules:
How can I pause the schedule in a way that ensures it resumes correctly the next day at the intended time (10:00 AM)?
DeviceActivityMonitor Logic:
Is there a way to deactivate and reactivate the schedule through the DeviceActivityMonitor without fully stopping the monitoring? Ideally, I'd like to retain the original schedule, pause it, and then resume it seamlessly after the pause.
What I’ve Tried So Far:
I’ve attempted to store the necessary data in the App Groups shared container as a local file. In the DeviceActivityMonitor, I used the schedule's name to identify and retrieve the saved object, planning to use this data to reapply the shielding after the pause. Unfortunately, this approach exceeds the 6MB memory limit of the extension, which is another roadblock.
Given these issues, I’m unsure how to move forward. Are there any best practices or alternative approaches that could help manage this situation more effectively? Any insights or suggestions would be greatly appreciated!
Thanks in advance for your help.
Good Morning/Afternoon/Evening.
I'm trying to build a code with a background bar (grey) and above it, we have a ProgressiveBar that starts full and empties as time passes.
To be able to stack the elements one above the other I'm trying to use the Zstack function, as shown on the code below, but when simulating the code the progressiveBar is always, on the iPhone screen, showing on top of the background bar, and not above as desired.
Could you please review the code below and let me know what I'm missing?
`var body: some View {
ZStack(alignment: .leading) {
// Background bar
RoundedRectangle(cornerRadius: 10)
.fill(Color.gray.opacity(0.3))
.frame(height: barHeight) // Set height to the specified barHeight
// Progress bar
GeometryReader { geometry in
RoundedRectangle(cornerRadius: 10)
.fill(barColor)
.frame(width: CGFloat(progress) * geometry.size.width, height: barHeight)
.animation(.linear(duration: 1), value: progress)
}
.cornerRadius(10)
// Icon and label
HStack {
Image(systemName: icon)
.foregroundColor(.black)
.font(.system(size: 28)) // Increased size
.padding(.leading, 8)
Spacer()
Text(timeString(from: duration * Double(progress)))
.foregroundColor(.black)
.bold()
.font(.system(size: 24)) // Increased size
.padding(.trailing, 8)
}
.frame(width: UIScreen.main.bounds.width * 0.9, height: barHeight)
.zIndex(1) // Ensure the HStack is on top
}
.padding(.horizontal)
After updating to Xcode 16.0 beta 5 (16A5221g) on macOS Sonoma 14.6 I am getting lots of errors like:
Main actor-isolated property '[property name]' can not be mutated from a nonisolated context
in classes that are marked as @MainActor at the class level, e.g.:
@MainActor final class MyClass: AnotherClass { ... }
The project uses Swift 6 and there are no errors in Xcode 16 beta 4.
I tried restarting, clearing Derived Data, cleaning build folder...
Anyone else encountering this issue with beta 5?
Dear all,
I'm going out of mind with the following issue. I have a view where I'm selecting a date through a datepicker. Then I'm inserting some other data and then I'm trying to save, see private func saveTraining(). But the date which is used to save the training is always one day before the selected date and more than this, I'm not able to save it without time. As I have then a calendar where the saved trainings need to be displayed, I'm not able to match it.
Did anybody already face this issue? How can I solve it?
I'm reporting here the code of the view where you can also find the checks I put to verify values/ dates:
import SwiftData
import AppKit
struct AddTrainingView: View {
@Environment(\.modelContext) private var context
@Binding var isAddingTraining: Bool
@ObservedObject var season: Season
@Binding var selectedDate: Date
@State private var trainingStartTime = Date()
@State private var trainingEndTime = Date()
@State private var trainingConditionalGoal = ""
@State private var trainingTacticalGoal = ""
@State private var trainingExercises: [TrainingExercise] = []
@State private var showExercisePicker = false
@State private var currentTraining: Training?
init(isAddingTraining: Binding<Bool>, selectedDate: Binding<Date>, season: Season, currentTraining: Training? = nil) {
self._isAddingTraining = isAddingTraining
self._selectedDate = selectedDate
self.season = season
self._currentTraining = State(initialValue: currentTraining)
if let training = currentTraining {
self._trainingStartTime = State(initialValue: training.startTime)
self._trainingEndTime = State(initialValue: training.endTime)
self._trainingConditionalGoal = State(initialValue: training.conditionalGoal)
self._trainingTacticalGoal = State(initialValue: training.tacticalGoal)
self._trainingExercises = State(initialValue: training.trainingExercises)
}
}
var body: some View {
VStack(alignment: .leading, spacing: 16) {
Text("Aggiungi Allenamento")
.font(.title)
.bold()
.padding(.top)
VStack(alignment: .leading) {
Text("Data allenamento:")
DatePicker("", selection: $selectedDate, displayedComponents: .date)
.datePickerStyle(.field)
}
.padding(.bottom, 10)
...
Button("Salva") {
saveTraining()
isAddingTraining = false
dismiss()
}
.buttonStyle(.borderedProminent)
.tint(.blue)
Button("Visualizza PDF") {
createPDF()
}
.buttonStyle(.borderedProminent)
.tint(.blue)
Spacer()
}
.padding(.bottom)
}
.padding()
.frame(maxWidth: .infinity, maxHeight: .infinity)
}
private func saveTraining() {
// Creiamo un'istanza del calendario corrente
var calendar = Calendar.current
calendar.timeZone = TimeZone.current // Assicuriamoci di utilizzare il fuso orario locale
// Estrarre solo i componenti di data dalla data selezionata
let components = calendar.dateComponents([.year, .month, .day], from: selectedDate)
// Creiamo una nuova data con i soli componenti di anno, mese e giorno
guard let truncatedDate = calendar.date(from: components) else {
print("Errore nella creazione della data troncata")
return
}
// Stampa di debug per verificare la data selezionata e quella troncata
print("Data selezionata per l'allenamento: \(selectedDate)")
print("Data troncata che verrà salvata: \(truncatedDate)")
let newTraining = Training(
date: truncatedDate,
startTime: trainingStartTime,
endTime: trainingEndTime,
conditionalGoal: trainingConditionalGoal,
tacticalGoal: trainingTacticalGoal,
season: season
)
// Verifica che la data sia correttamente impostata
print("Data che verrà salvata: \(newTraining.date)")
newTraining.trainingExercises = trainingExercises
context.insert(newTraining)
do {
try context.save()
print("Allenamento salvato correttamente.")
} catch {
print("Errore nel salvataggio: \(error.localizedDescription)")
}
}
private var dateFormatter: DateFormatter {
let formatter = DateFormatter()
formatter.dateStyle = .short
return formatter
}
private var timeFormatter: DateFormatter {
let formatter = DateFormatter()
formatter.timeStyle = .short
return formatter
}
}
}
Example: when I'm selecting 2023-08-21, the debugger retrieves me following data:
Data selezionata per l'allenamento: 2023-08-20 22:00:00 +0000
Data troncata che verrà salvata: 2023-08-20 22:00:00 +0000
Data che verrà salvata: 2023-08-20 22:00:00 +0000
Allenamento salvato correttamente.
Hi,
I had a SwiftData model and it was working fine, I had few records in it, but when I added to its Module file few extra properties I started to get the below error ! how to fix this ?
"Thread 1: Fatal error: failed to find a currently active container for Patient"
Hi,
I have a file with a name like this: file name.filetype
I need to include it in an xcconfig file.
I have read that spaces are used as delimiters in xcconfig files, and I need to correctly quote it. But this is for the framework paths, so not sure if the same applies to config files.
This might be a really silly question, but what's the correct quotation format we need to follow?
I've tried all the usual stuff:
#include "../../../file\ name.filetype"
#include "../../../file%20name.filetype"
#include "../../../file name.filetype"
#include "../../../file"+" "+"name.filetype"
But neither is working.
Am I having a major brain meltdown moment or is there a specific way to do it?
Thanks a lot.
When building my iOS framework, I encountered a (fatal) module 'TrustKit' not found issue. I've marked the necessary classes as public (which is inherited from other library classes) for client applications, but I'm unsure how to resolve this error.
Example Code Snippet
import Foundation
import TrustKit
@objc
public class InheritanceTruskitFrameworkProjectClass: TrustKit {
func extraFunctionality() {
print("extraFunctionality executing...")
}
}
Error while building the framework in the auto-generated Swift header file (ProjectName-Swift.h)
#if !defined(SWIFT_IMPORT_STDLIB_SYMBOL)
# define SWIFT_IMPORT_STDLIB_SYMBOL
#endif
#endif
#if defined(__OBJC__)
#if __has_feature(objc_modules)
#if __has_warning("-Watimport-in-framework-header")
#pragma clang diagnostic ignored "-Watimport-in-framework-header"
#endif
@import Foundation;
@import TrustKit; # -> error here `(fatal) module 'TrustKit' not found`
#endif
#endif
#pragma clang diagnostic ignored "-Wproperty-attribute-mismatch"
#pragma clang diagnostic ignored "-Wduplicate-method-arg"
#if __has_warning("-Wpragma-clang-attribute")
# pragma clang diagnostic ignored "-Wpragma-clang-attribute"
#endif
#pragma clang diagnostic ignored "-Wunknown-pragmas"
#pragma clang diagnostic ignored "-Wnullability"
#pragma clang diagnostic ignored "-Wdollar-in-identifier-extension"
Please check this image to understand the error
Sample GitHub project https://github.com/vinaykumar0339/InheritanceTruskitFrameworkProject
I want a way to use inheritance for the dependencies project. Is this supported?
Looks Like it won't work with any modules like cocopod frameworks, internal module map etc?
What I have Tried
I have tried to install TrustKit with SPM also the same error throwing (fatal) module 'TrustKit' not found
Hi everyone,
I'm working on an app that uses the Screen Time API, and I'm encountering an issue with Live Activities.
The app runs a timer (e.g., 10 minutes), after which the intervalDidEnd method in DeviceActivityMonitor is triggered. To improve the user experience, I've implemented Live Activities to display the remaining time.
However, I'm having trouble stopping the Live Activity when the timer expires and intervalDidEnd is called. It seems that the Screen Time
extensions cannot detect or interact with active Live Activities, even though
both share the same App Group.
My Question:
Since the DeviceActivityMonitor extension does not seem to be able to detect Live Activities, does anyone know if there’s a way to end a Live Activity when the timer expires without relying on a server? Using Apple’s Push Notification service feels excessive given the lightweight nature of the app, which doesn’t use server-side components.
Any insights or suggestions would be greatly appreciated!
Thanks!
new to Swift plz share some sources to proceed
Anytime I try to embed my xcframework (which has another xcframework embedded in it) in an iOS app and run it on a device I get this error in the console :
dyld[5028]: Library not loaded: @rpath/Calculator.framework/Calculator
Referenced from:...
It runs fine on the simulator however. I've tried several suggested solutions; modifying Runpath Search Paths, Framework Search Paths but to no avail.
Is there something I'm missing or you can't have nested frameworks for iOS as per this question? If so why does it run on the simulator though.
I have a very simple set of lines of code. It doesn't matter whether you run it under UIKit or SwiftUI. In SwiftUI, I have the following.
import SwiftUI
struct ContentView: View {
var body: some View {
VStack {
Button("Click on me") {
let tabLine = "1\tAnthony James\t139.9"
var item = ""
let tabs = tabLine.components(separatedBy: "\t")
for tab in tabs {
item += "'\(tab)'"
}
print("\(item)")
}
}
}
}
So I have tab-separated values. And I want to separate them and quote each value either with an apostrophe or a double quotation mark.
In the case above, I get the following print.
'1''Anthony James''139.9'
That's exactly what I want. Now, I have an array of three of those guys like the following.
import SwiftUI
struct ContentView: View {
var body: some View {
VStack {
Button("Click on me") {
let tabLine0 = "1\tAnthony James\t139.9"
let tabLine1 = "2\tKim Harbaugh\t181.4"
let tabLine2 = "3\tAnthony James\t212.4"
let tabTextLines = [tabLine0, tabLine1, tabLine2]
var strings = [String]()
for tabLine in tabTextLines {
var item = ""
let tabs = tabLine.components(separatedBy: "\t")
for tab in tabs {
item += "'\(tab)'"
}
strings.append(item)
}
print("\(strings)")
}
}
.frame(width: 360, height: 240)
}
}
And I get the following print.
This is a nightmare situation. Each value is quoted with an escaped apostrophe. I can't even remove the escapees with replacingOccurrences(of:with:). How does that happen when you have an array of strings? If I try quoting the values with a unicode character, things are the same. Is there a workaround? Muchos thankos.
With the release of iOS 18 Developer Beta 5 and Public Beta 3, my team received reports from customers that all negative values in our app were showing as positives, which led to major confusion.
We narrowed down the issue to a change to the initializer for Decimal: Decimal(sign:exponent:significand). Prior to Beta 5, the sign passed into the initializer would be the sign of the decimal. Passing plus would result in a positive decimal, and passing minus would result in a negative decimal. This is regardless of the sign of the significant. In Beta 5, it seems that the sign passed into the init, and the sign of the significand are now multiplied together. This means that passing .minus for sign and a negative Decimal for significand results in an unexpectedly positive Decimal value in Beta 5 alone. This behavior does not seem to be explicitly documented.
I created a quick playground to illustrate the issue. Here's the code:
// Expected Value: 20
// Actual Value: 20
let positiveDecimal = Decimal(sign: .plus, exponent: 1, significand: 2)
// Expected Value: 20
// Actual Value (15.4.0, 16.0 Beta 4): 20
// Actual Value (16.0 Beta 5): -20
let positiveDecimalWithNegativeSignificand = Decimal(sign: .plus, exponent: 1, significand: -2)
// Expected Value: -20
// Actual Value: -20
let negativeDecimal = Decimal(sign: .minus, exponent: 1, significand: 2)
// Expected Value: -20
// Actual Value (15.4.0, 16.0 Beta 4): -20
// Actual Value (16.0 Beta 5): 20
let negativeDecimalWithNegativeSignificand = Decimal(sign: .minus, exponent: 1, significand: -2)
Here is the result of running the playground in Xcode 16.0 Beta 4 (and 15.4.0):
And here is the result of running it in Xcode 16.0 Beta 5:
I've tracked down the issue to a PR in the swift-foundation repo from 3 weeks ago, which seems to pull a commit from
the swift-corelibs-foundation repo.
For anyone else who finds this thread looking for a solution, in the mean time you can wrap the significant in abs and this should ensure consistent behavior between iOS 18 Beta 5 and prior iOS versions.
class A: Codable {
var x: String?
}
class B: A {
var y: String?
}
When I try to decode class B, the property "y" remains nil even though there is a value for it in the JSON data. If I change it to B: Codable then it works.
When I try to search for a solution, all I can find is people overriding init methods and doing decoding manually in some way. I am having a hard time believeing that this is the right answer.
Surely, coding works across class hierarchies?
I am trying to create a simple app that "blocks" other apps if a certain condition is not met. I am currently using the IOS shortcuts and have set up an automation that opens my app A whenever another app B opens.
If the condition is not met i imagine the flow to look like:
Open app A.
My app B opens instead.
I check a box in my app B.
I navigate back to app A and it works as expected.
If the condition already is met the app A would work as expected from the beginning.
What is have tried so far
My first attempt involved using an AppIntent and changing the openAppWhenRun programmatically based on the condition. I did however learn pretty quickly that changing the value of openAppWhenRun does not change if the AppIntent actually opens my app. The code for this looked like this where the value of openAppWhenRun is changed in another function.
struct BlockerIntent: AppIntent {
static let title: LocalizedStringResource = "Blocker App"
static let description: LocalizedStringResource = "Blocks an app until condition is met"
static var openAppWhenRun: Bool = false
@MainActor
func perform() async throws -> some IntentResult {
return .result()
}
}
Another attempt involved setting openAppWhenRun to false in an outer AppIntent and opening another inner AppIntent if the condition is met. If the condition in my app is met openAppWhenRun is set to true and instead of opening the inner AppIntent an Error is thrown. This functions as expected but there is an error notification showing every time I open the "blocked" app.
struct BlockerIntent: AppIntent {
static let title: LocalizedStringResource = "Blocker App"
static let description: LocalizedStringResource = "Blocks an app until condition is met"
static var openAppWhenRun: Bool = false
func perform() async throws -> some IntentResult & OpensIntent {
if (BlockerIntent.openAppWhenRun) {
throw Error.notFound
}
return .result(opensIntent: OpenBlockerApp())
}
enum Error: Swift.Error, CustomLocalizedStringResourceConvertible {
case notFound
var localizedStringResource: LocalizedStringResource {
switch self {
case .notFound: return "Ignore this message"
}
}
}
}
struct OpenBlockerApp: AppIntent {
static let title: LocalizedStringResource = "Open Blocker App"
static let description: LocalizedStringResource = "Opens Blocker App"
static var openAppWhenRun: Bool = true
@MainActor
func perform() async throws -> some IntentResult {
return .result()
}
}
My third attempt look similar to the previous one but instead I used two different inner AppIntents. The only difference between the two were that on had openAppWhenRun = false and the other had openAppWhenRun = true.
struct BlockerIntent: AppIntent {
static let title: LocalizedStringResource = "Blocker App"
static let description: LocalizedStringResource = "Blacks an app until condition is met"
static var openAppWhenRun: Bool = false
func perform() async throws -> some IntentResult & OpensIntent {
if (BlockerIntent.openAppWhenRun) {
return .result(opensIntent: DoNotOpenBlockerApp())
} else {
return .result(opensIntent: OpenBlockerApp())
}
}
}
Trying this gives me this error:
Function declares an opaque return type 'some IntentResult & OpensIntent', but the return statements in its body do not have matching underlying types
I have also tried opening the app with a URL link with little to no success often ending up in an infinity loop, I did try the ForegroundContinuableIntent but it did not function as expected since it relies on the users input.
Is there any way to do what I am trying to accomplish? I have seen other apps using a similar concept so I feel like this should be possible.
Many thanks!
Posting here to see if folks have workarounds or if I have a misunderstanding of SwiftData supported types.
In adopting SwiftData, I have swiftData properties of collection type (Array or Set - both have this issue). E.g:
@Model
final class Item {
var timestamp: Date
var strings = ["aa", "bb"]
var display: String {
strings.joined(separator: " ")
}
init(timestamp: Date) {
self.timestamp = timestamp
}
}
So far in development I haven't had issues on iOS 17, but on the iOS 18 betas 4-5 the app logs show the following error:
"fault: Could not materialize Objective-C class named "Array" from declared attribute value type "Array<String>" of attribute named strings"
It happens immediately in my app when creating an object with a collection attribute.
In a minimal test example, the error log appears only after a few minutes and doesn't seem to affect the template app's basic functionality.
Anyone else running into this?
Was filed as FB14397250
I am trying to create identity from certificate and private key which are in base64 format.
I am getting error - Unable to create identity
one time I get the error - Failed to add certificate and private key to keychain: -26276
My Xcode is 15.3 and macOS is Sonoma 14.5
func loadIdentity(certificate: String, privateKey: String) -> SecIdentity? {
guard let certData = Data(base64Encoded: certificate) else {
print("Unable to encode certificate base64")
return nil
}
guard let cert = SecCertificateCreateWithData(nil, certData as CFData) else {
print("Unable to create certificate")
return nil
}
let certAddQuery: [NSString: Any] = [
kSecClass: kSecClassCertificate,
kSecValueRef: cert,
kSecAttrLabel: "myCertificate"
]
var status = SecItemAdd(certAddQuery as CFDictionary, nil)
if status != errSecSuccess && status != errSecDuplicateItem {
print("Failed to add certificate to keychain: \(status)")
return nil
}
guard let keyData = Data(base64Encoded: privateKey) else {
print("Unable to encode private key base64")
return nil
}
let keyDict: [NSString: Any] = [
kSecAttrKeyType: kSecAttrKeyTypeRSA,
kSecAttrKeyClass: kSecAttrKeyClassPrivate,
kSecAttrKeySizeInBits: 2048,
kSecReturnPersistentRef: true
]
var error: Unmanaged<CFError>?
guard let privateKeyData = SecKeyCreateWithData(keyData as CFData, keyDict as CFDictionary, &error) else {
print("Unable to create private key")
return nil
}
let keyAddQuery: [NSString: Any] = [
kSecClass: kSecClassKey,
kSecValueRef: privateKeyData,
kSecAttrLabel: "myKey",
kSecAttrAccessible: kSecAttrAccessibleWhenUnlocked
]
status = SecItemAdd(keyAddQuery as CFDictionary, nil)
if status != errSecSuccess && status != errSecDuplicateItem {
print("Failed to add private key to keychain: \(status)")
return nil
}
let identityQuery: [NSString: Any] = [
kSecClass: kSecClassIdentity,
kSecReturnRef: true,
kSecAttrLabel: "myCertificate",
kSecMatchItemList: [cert, privateKeyData]
]
var identity: CFTypeRef?
status = SecItemCopyMatching(identityQuery as CFDictionary, &identity)
guard status == errSecSuccess else {
print("Unable to create identity")
return nil
}
return (identity as! SecIdentity)
}