Post

Replies

Boosts

Views

Activity

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.
5
0
337
1w
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
190
2w
Calling Async Functions in SwiftUI
Hi, I'd like to call an Async function upon a state change or onAppear() but I'm not sure how to do so. Below is my code: .onAppear() { if !subscribed { await Subscriptions().checkSubscriptionStatus() } } class Subscriptions { var subscribed = UserDefaults.standard.bool(forKey: "subscribed") func checkSubscriptionStatus() async { if !subscribed { await loadProducts() } } func loadProducts() async { for await purchaseIntent in PurchaseIntent.intents { // Complete the purchase workflow. await purchaseProduct(purchaseIntent.product) } } func purchaseProduct(_ product: Product) async { // Complete the purchase workflow. do { try await product.purchase() } catch { // Add your error handling here. } // Add your remaining purchase workflow here. } }
1
0
229
Jan ’25
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
479
Jan ’25
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 }
3
0
551
Nov ’24
Thread Error with @Model Class
I have a @Model class that is comprised of a String and a custom Enum. It was working until I added raw String values for the enum cases, and afterwards this error and code displays when opening a view that uses the class: { @storageRestrictions(accesses: _$backingData, initializes: _type) init(initialValue) { _$backingData.setValue(forKey: \.type, to: initialValue) _type = _SwiftDataNoType() } get { _$observationRegistrar.access(self, keyPath: \.type) return self.getValue(forKey: \.type) } set { _$observationRegistrar.withMutation(of: self, keyPath: \.type) { self.setValue(forKey: \.type, to: newValue) } } } Thread 1: EXC_BREAKPOINT (code=1, subcode=0x1cc165d0c) I removed the String raw values but the error persists. Any guidance would be greatly appreciated. Below is replicated code: @Model class CopingSkillEntry { var stringText: String var case: CaseType init(stringText: String, case: CaseType) { self.stringText = stringText self.case = case } } enum CaseType: Codable, Hashable { case case1 case case1 case case3 }
2
0
363
Oct ’24
Implementing multiple Model Containers
Hi, When using just one model container, I have the following code: let modelContainer: ModelContainer init() { do { entriesModel = try ModelContainer(for: Model.self) } catch { fatalError("Could not initialize ModelContainer") } } I am attempting to implement two like so: let model1: ModelContainer let model2: ModelContainer init() { do { model1 = try ModelContainer(for: Model1.self) model2 = try ModelContainer(for: Model2.self) } catch { fatalError("Could not initialize ModelContainer") } } var body: some Scene { WindowGroup { ContentView() .modelContainer(model1) .modelContainer(model2) } } However, when in the Views that I'd like to implement I don't know how to declare the models. I tried this: @Environment(\.model1) private var model1 @Query private var data: [Model1] But I get the error: Cannot infer key path type from context; consider explicitly specifying a root type How do I implement multiple model containers in multiple different views?
2
0
990
Oct ’24
AppStorage extension for Bool Arrays
Hi, As AppStorage does not support arrays, I found an extension online that allows for handling arrays of strings with AppStorage. However, in my use case, I need to handle arrays of boolean values. Below is the code. Any help would be greatly appreciated. extension Array: RawRepresentable where Element: Codable { public init?(rawValue: String) { guard let data = rawValue.data(using: .utf8), let result = try? JSONDecoder().decode([Element].self, from: data) else { return nil } self = result } public var rawValue: String { guard let data = try? JSONEncoder().encode(self), let result = String(data: data, encoding: .utf8) else { return "[]" } return result } }
1
0
516
Aug ’24
Condensing HTTP request functions
Hi, For HTTP requests, I have the following function: func createAccount(account: Account, completion: @escaping (Response) -> Void) { guard let url = URL(string: "http://localhost:4300/account") else { return } var request = URLRequest(url: url) request.httpMethod = "POST" request.setValue("application/json", forHTTPHeaderField: "Content-Type") guard let encoded = try? JSONEncoder().encode(account) else { print("Failed to encode request") return } request.httpBody = encoded URLSession.shared.dataTask(with: request) { (data, response, error) in guard let data = data else { return } do { let resData = try JSONDecoder().decode(Response, from: data) DispatchQueue.main.async { completion(resData) } } catch let jsonError as NSError { print("JSON decode failed: \(jsonError)") } }.resume() } Because various HTTP requests use different body structs and response structs, I have a HTTP request function for each individual HTTP request. How can I condense this into one function, where the passed struct and return struct are varying, so I can perhaps pass the struct types in addition to the data to the function? Any guidance would be greatly appreciated.
0
0
333
Aug ’24
.onAppear print not working
Hi, I have the following code: struct Loading: View { var body: some View { Text("Loading...") .onAppear { print("test") } } } The view is on the screen, however the print statement doesn't run. Why would this be? Any help would be greatly appreciated.
1
0
422
Aug ’24
Undefined symbol and linker command failed errors
Hi, I was searching and replacing code where all of the sudden I got the following errors: Undefined symbol: AppName.Client.init(id: Swift.Int, name: Swift.String, StructVariable: Swift.Int?, structVariable: [Struct]?) -> AppNameStruct Linker command failed with exit code 1 (use -v to see invocation) I replaced the names in the first error message for privacy reasons. I tried quitting Xcode and restarting my computer but the error persisted. Any help on resolving this would be greatly appreciated.
1
0
627
Jul ’24