Post

Replies

Boosts

Views

Activity

Background App Refresh
Hi, I have a couple questions about background app refresh. First, is the function RefreshAppContentsOperation() where to implement code that needs to be run in the background? Second, despite importing BackgroundTasks, I am getting the error "cannot find operationQueue in scope". What can I do to resolve that? Thank you. func scheduleAppRefresh() { let request = BGAppRefreshTaskRequest(identifier: "peaceofmindmentalhealth.RoutineRefresh") // Fetch no earlier than 15 minutes from now. request.earliestBeginDate = Date(timeIntervalSinceNow: 15 * 60) do { try BGTaskScheduler.shared.submit(request) } catch { print("Could not schedule app refresh: \(error)") } } func handleAppRefresh(task: BGAppRefreshTask) { // Schedule a new refresh task. scheduleAppRefresh() // Create an operation that performs the main part of the background task. let operation = RefreshAppContentsOperation() // Provide the background task with an expiration handler that cancels the operation. task.expirationHandler = { operation.cancel() } // Inform the system that the background task is complete // when the operation completes. operation.completionBlock = { task.setTaskCompleted(success: !operation.isCancelled) } // Start the operation. operationQueue.addOperation(operation) } func RefreshAppContentsOperation() -> Operation { }
23
0
515
Oct ’25
@State variable returns empty despite being set in .onAppear function
With the code below, JSON data is parsed and is stored in the variable data in the .onAppear function, however an empty set of data is passed to the Content view. How can that be fixed so that the JSON data passes to the DataView? struct ContentView: View { @State var data: [Data] @State var index: Int = 0 var body: some View { VStack { DataView(data: data[index]) } .onAppear { let filePath = Bundle.main.path(forResource: "data", ofType: "json") let url = URL(fileURLWithPath: filePath!) data = getData(url: url) } } func getData(url: URL) -> [Data] { do { let data = try Data(contentsOf: url) let jsonDecoded = try JSONDecoder().decode([Data].self, from: data) return jsonDecoded } catch let error as NSError { print("Fail: \(error.localizedDescription)") } catch { print("Fail: \(error)") } return [] } }
17
1
523
Aug ’25
Testing In-App Purchases
Hi, I have a couple of questions in regards to testing in-app purchases. I tested the subscription on a device but I'm not sure how to reset so I can test again. I didn't see the subscription in device settings or in Debug -> StoreKit -> Manage Subscriptions window. Additionally, I was wondering if there was a way to detect the subscription being made. I implemented this, but I'm not sure if that will work: .onChange(of: Product.SubscriptionInfo.RenewalState.subscribed) { if Product.SubscriptionInfo.RenewalState.subscribed == .subscribed { } }
13
0
314
Jul ’25
Incorrect JSON Format with JSONEncoder
Hi, I have a struct that I'd like to encode with JSON and send in the body of an HTTP request: struct AccountCreationData: Codable, Hashable { var name: String var email: String var phone: String var password: String private enum CodingKeys: String, CodingKey { case name case email case phone case password } } I encode it with this code and set the HTTP request body to the encoded data: guard let encoded = try? JSONEncoder().encode(account) else { print("Failed to encode request") return } request.httpBody = encoded But in my backend, when I receive the data from the HTTP request, the JSON data is formatted as such: '{"phone":"0000000000", "password":"password", "email":"email",", "name":"name"}': ''} Instead of: { "phone": "0000000000", "password":"password" "email":"email" "name":"name" } So the JSON parser in my backend isn't properly parsing the data because of the format that the JSON is in. I was wondering if this is an issue with the way I'm encoding on the front end (Swift) or my backend. I have similar projects using the same code to encode the data so I'm unsure as to what the issue is. Any help would be greatly appreciated. Thank you.
6
0
664
Jul ’24
.onAppear and .task code not running
Hi, I have the following code, which for some reason is not working as expected. I have an .onAppear and a .task function that isn't running, which I can see isn't running because nothing is printing. Any guidance would be greatly appreciated. Thank you. struct ContentView: View { var body: some View { ZStack { switch view { case .view1: View1() case .view2: View2() case .view3: View3() case .view4: View4() case .view5: View5() default: SubscriptionStoreView(groupID: "") } } .onAppear() { view = .view6 print("test 1") } .task { print("test") await refreshPurchasedProducts() } } func refreshPurchasedProducts() async { // Iterate through the user's purchased products. for await verificationResult in Transaction.currentEntitlements { switch verificationResult { case .verified(let transaction): print("verified") case .unverified(let unverifiedTransaction, let verificationError): print("unverified") default: print("default") } } } }
Topic: Design SubTopic: General Tags:
6
0
154
Apr ’25
Subscriptions Not Displaying
I have a subscription group with two individual subscriptions configured but when trying to load the SubscriptionStoreView I get the error: "Subscription Unavailable: The subscription is unavailable in the current storefront." When I try to load the ProductView, it appears to be stuck in a loading screen. I am running the app on a device that is signed into a sandbox account. Any guidance would be greatly appreciated. Thank you.
Topic: Design SubTopic: General Tags:
5
0
870
Feb ’25
Using Swift UI Canvas
Hello, I am trying to use the SwiftUI canvas and I'm having a couple of issues. One is that I'd like to be able to fit a canvas view in a ScrollView, but when the canvas is inside a ScrolLView, nothing draws. Additionally, I'm trying to draw lines on the canvas on Appear, but I can't seem to get that to work either. Below is a simplified version of the code I have: struct Line: Codable, Hashable { var points: [CGPoint] var colorName: String? var rgb: [Double]? private enum CodingKeys: String, CodingKey { case points case colorName case rgb } } extension CGPoint: Hashable { public func hash(into hasher: inout Hasher) { hasher.combine(x) hasher.combine(y) } } struct CanvasExample: View { @State var lines: [Line] = [] @State var selectedColor: Color = Color.black var body: some View { Canvas { ctx, size in for line in lines { var path = Path() path.addLines(line.points) ctx.stroke(path, with: .color(line.color), style: StrokeStyle(lineWidth: 5, lineCap: .round, lineJoin: .round)) } }.gesture(DragGesture(minimumDistance: 0, coordinateSpace: .local) .onChanged({ value in let position = value.location if value.translation == .zero { lines.append(Line(points: [position], color: selectedColor)) } else { guard let lastIndex = lines.indices.last else { return } lines[lastIndex].points.append(position) } }) ) .onAppear { lines.append(Line(points: [position], color: .black)) } } } Any help would be greatly appreciated. Thank you.
4
0
1.3k
Jan ’24
Download Rendered PDF
I need to render a PDF and then make it downloadable to the mobile device. I can generate the PDF but I'm unsure how to configure the download aspect. I have the following code: let renderer = UIGraphicsPDFRenderer(bounds: CGRect(x: 0, y: 0, width: 612, height: 792)) let pdf = renderer.pdfData { (context) in context.beginPage() let text = "Test" as NSString text.draw(in: CGRect(x: 0, y: 0, width: 100, height: 25))
4
0
755
Jan ’25
Cannot convert value of type '() -> ()' to expected argument type 'String' SwiftUI Button
Hello, I'm trying to build a simple button. For some reason, I'm getting the following errors: Cannot convert value of type '() -> ()' to expected argument type 'String' Incorrect argument label in call (have 'action:_:', expected 'value:selected:') Button(action: login) { ZStack { RoundedRectangle(cornerRadius: 5) .fill(Color.black) .frame(width: UIScreen.main.bounds.width - 50, height: 50) Text("Sign in") .foregroundColor(.white) } } All of my buttons in this particular project suddenly have this error. Any help would be greatly appreciated. Thank you
3
0
1.4k
Dec ’23
Using the SwiftUI Canvas
Hello, I was following a tutorial on how to use the SwiftUI Canvas and I can't seem to get it to work. Below is the code that I have. struct Line { var points: [CGPoint] var color: Color } struct CanvasView: View { @State var lines: [Line] = [] @State var selectedColor: Color = Color.black var body: some View { ScrollView([.horizontal, .vertical]) { Canvas { ctx, size in for line in lines { var path = Path() path.addLines(line.points) ctx.stroke(path, with: .color(line.color), style: StrokeStyle(lineWidth: 5, lineCap: .round, lineJoin: .round)) } }.gesture(DragGesture(minimumDistance: 0, coordinateSpace: .local) .onChanged({ value in let position = value.location if value.translation == .zero { lines.append(Line(points: [position], color: selectedColor)) } else { guard let lastIndex = lines.indices.last else { return } lines[lastIndex].points.append(position) } }) ) } } } I'm pretty sure I followed the tutorial exactly so I'm not sure why it isn't drawing. Any help would be greatly appreciated. Thank you.
3
0
2.3k
Feb ’24
Efficiently accounting for different iOS versions
I one project, using .onChange(), I get an error: 'onChange(of:initial:_:)' is only available in iOS 17.0 or newer The solution provided is to wrap the code in if #available(iOS 17.0, *) { }, but I'm only able to wrap the whole view inside of it, like so: if #available(iOS 17.0, *) { VStack () { // view with .onChange } } else { VStack () { // view with older solution } } The view is quite lengthy so it would be very redundant to have the same view twice, (the same view for each component of the if/else statement). Is there a way to implement an if statement so that I would only need the code for the view once (something like the code below)? VStack { } if #available(iOS 17.0, *) { .onChange() { } } else { // older solution } Additionally, in a newer project, I don't have to include the if #available(iOS 17.0, *) { }, so I'm guessing the build is made for a newer iOS. If that's the case, would it not be compatible with older iOS versions? Any help would be greatly appreciated.
Topic: Design SubTopic: General Tags:
3
0
1.1k
Jul ’24
Issues with UserDefault boolean
Hi, I have a view that should do the following: Set up and confirm a passcode upon account creation. Verify passcode if signing in. Reset passcode if prompted. With the code below, functions 1 and 2 are working. However, I'm having an issue with function 3. I am able to declare the reset UserDefault on a previous view so that the proper logic occurs, which is to read in the input, and then confirm it. However, it is not working as intended. In this code here: else if reset { UserDefaults.standard.set(passcode, forKey: "new-passcode") UserDefaults.standard.set(false, forKey: "reset-passcode") passcode = "" } I store the new passcode, set reset to false, and clear the passcode so it can be entered again to confirm. The code does not run as intended however. The title does not change and I'm unsure if it is actually storing the passcode. And, when re-entering, it does not change the view as it should by setting view = .someView. I'm assuming there is just flaw in my logic but I'm not sure how to resolve this. Below is the full code. Please let me know if any further clarification is needed. struct EnterPasscode: View { @State var title = "" @State var passcode = "" @State var message = "" @State var buttonState = false @Binding var view: Views var body: some View { Group { Spacer() .frame(height: 50) Text(title) .font(.system(size: 36)) .multilineTextAlignment(.center) .frame(height: 50) Spacer() if passcode != "" { Text("\(passcode)") .font(.system(size: 24)) .frame(height: 25) } else { Spacer() .frame(height: 25) } Spacer() .frame(height: 50) PasscodeKeypad(passcode: $passcode) Spacer() if message != "" { Text(message) .frame(height: 25) .foregroundStyle(Color.red) } else { Spacer() .frame(height: 25) } Spacer() WideButton(text: "Continue", buttonFunction: .functional, openView: .enterPasscode, view: .constant(.enterPasscode), buttonState: $buttonState) .onChange(of: buttonState) { oldState, newState in if buttonState { let passcodeSet = UserDefaults.standard.bool(forKey: "passcode-set") let storedPasscode = UserDefaults.standard.string(forKey: "passcode") let reset = UserDefaults.standard.bool(forKey: "passcode-reset") let newPasscode = UserDefaults.standard.string(forKey: "new-passcode") print(reset) if passcode.count == 4 { if storedPasscode == nil { if newPasscode == nil { UserDefaults.standard.set(passcode, forKey: "new-passcode") passcode = "" } else if passcode == newPasscode { UserDefaults.standard.set(passcode, forKey: "passcode") UserDefaults.standard.set(true, forKey: "passcode-set") view = .someView } } else if reset { UserDefaults.standard.set(passcode, forKey: "new-passcode") UserDefaults.standard.set(false, forKey: "reset-passcode") passcode = "" } else if newPasscode != nil { if passcode == newPasscode { UserDefaults.standard.set(passcode, forKey: "passcode") view = .someView } } } checkPasscodeStatus() buttonState = false } } Spacer() if !UserDefaults.standard.bool(forKey: "passcode-reset") && UserDefaults.standard.bool(forKey: "passcode-set") { Button(action: { view = .verifyPhone }) { Text("Forgot passcode?") .foregroundStyle(.black) } } Spacer() .frame(height: 25) } .onAppear() { checkPasscodeStatus() } .frame(width: UIScreen.main.bounds.width - 50) } func checkPasscodeStatus() { let passcodeSet = UserDefaults.standard.bool(forKey: "passcode-set") let storedPasscode = UserDefaults.standard.string(forKey: "passcode") let reset = UserDefaults.standard.bool(forKey: "passcode-reset") if reset { title = "Enter new passcode" } else if passcodeSet { title = "Enter Passcode" } else if storedPasscode != nil && storedPasscode != "" { title = "Confirm Passcode" } else { title = "Select a 4 Digit Passcode" } } } struct WideButton: View { @State var text: String @State var buttonFunction: ButtonType @State var openView: Views? @Binding var view: Views @Binding var buttonState: Bool var body: some View { Button(action: { buttonPressed() }, label: { ZStack { RoundedRectangle(cornerRadius: 5) .fill(Color.black) .frame(width: UIScreen.main.bounds.width - 50, height: 50) Text(text) .foregroundColor(.white) } }) } func buttonPressed() { switch buttonFunction { case .openView: view = openView! case .functional: buttonState = true } } } enum ButtonType { case openView case functional }
Topic: Design SubTopic: General Tags:
3
0
761
Nov ’24
Rendering Multi-Page PDF
I have the following code for generating a one page PDF: @MainActor func render() -> URL { let renderer = ImageRenderer(content: pdfView)) let url = URL.documentsDirectory.appending(path: "output.pdf") renderer.render { size, context in var document = CGRect(x: 0, y: 0, width: 2550, height: 3300) guard let pdf = CGContext(url as CFURL, mediaBox: &document, nil) else { return } pdf.beginPDFPage(nil) context(pdf) pdf.endPDFPage() pdf.closePDF() } return url } I'm trying to write code to create a multi-page PDF if there is multiple ImageRenderers. I tried something shown below but I'm not sure how to properly implement. @MainActor func render() -> URL { let renderer = ImageRenderer(content: pdfView) let url = URL.documentsDirectory.appending(path: "output.pdf") for image in renderer { image.render { size, context in var page = generatePage(image: image) } } return url } func generatePage(image: ImageRenderer<<#Content: View#>>) -> CGContext { var view = CGRect(x: 0, y: 0, width: 2550, height: 3300) guard let pdf = CGContext(url as CFURL, mediaBox: &view, nil) else { return } pdf.beginPDFPage(nil) context(pdf) pdf.endPDFPage() pdf.closePDF() return pdf } Any guidance would be greatly appreciated. Thank you.
3
0
364
Feb ’25