ios – Detailed error from AsyncImage in SwiftUI?

ios – Detailed error from AsyncImage in SwiftUI?


I’ve been utilizing AsyncImage for my challenge which will get the pictures utilizing urls. I believed AsyncImage is the easiest way to go since SwiftUI supplies a prepared to make use of part for it.

Nevertheless, if a picture load fails, then I get a standard error and never an in depth error message of why the url request failed (eg: no permissions to entry URL or another cause).

The explanation I’m always seeing is The operation couldn’t be accomplished. (SwiftUI.(unknown context at $1d2600574).LoadingError error 1.)

Executable code:

import SwiftUI

class Foo {
    var title: String
    var url: String
    var picture: Picture?
    
    init(title: String, url: String, picture: Picture? = nil) {
        self.title = title
        self.url = url
        self.picture = picture
    }
}

struct ContentViewA: View {
    @State var showSheetA: Bool = false
    
    var physique: some View {
        VStack {
            Textual content("That is important view")
        }
        .onAppear{
            showSheetA = true
        }
        .sheet(isPresented: $showSheetA) {
            SheetViewA()
        }
    }
}

struct SheetViewA: View {
    @State var knowledge = [
        Foo(title: "Image E", url: "https://t3.ftcdn.net/jpg/10/08/34/50/360_F_1008345045_VOe4ziz83vb6d3E3X6KI00qHyLd32D4l.jpg123", image: nil)
    ]

    @State var panelDetent: PresentationDetent = .medium
    
    var physique: some View {
        NavigationStack {
            VStack {
                ScrollView(.horizontal, showsIndicators: false) {
                     HStack(alignment: .middle, spacing: 10) {
                         ForEach(knowledge, id: .title) { merchandise in
                             if let urlObject = URL(string: merchandise.url) {
                                 AsyncImage(url: urlObject,
                                            scale: 1.0,
                                            transaction: Transaction(animation: .spring(response: 0.5, dampingFraction: 0.65, blendDuration: 0.025)),
                                            content material: { renderPhoto(part: $0, merchandise: merchandise) })
                             } else {
                                 /// Observe: Exhibits placeholder view
                                 EmptyView()
                             }
                         }
                     }
                     .background(Shade.grey.opacity(0.2))
                     .padding(.main, 0)
                     .padding(.trailing, 16)
                     .body(maxWidth: .infinity, minHeight: 65, maxHeight: 65, alignment: .topLeading)
                }
            }
            .padding([.top, .bottom], 150.0)
            .presentationDetents([.medium, .large], choice: $panelDetent)
        }
    }
    
    @ViewBuilder
    personal func renderPhoto(part: AsyncImagePhase, merchandise: Foo) -> some View {
        change part {
            case .success(let picture):
                let _ = print("Success state")
            case .failure(let error):
                let _ = print("%%% detailed error is - (error.localizedDescription) %%%%")
            case .empty:
                let _ = print("Empty state")
            @unknown default:
                EmptyView()
        }
    }
}

Is there a technique to get detailed error message utilizing AsycnImage? Or will I’ve to get photos utilizing the outdated URLSession request technique? I must show detailed error message on the UI.

author avatar
roosho Senior Engineer (Technical Services)
I am Rakib Raihan RooSho, Jack of all IT Trades. You got it right. Good for nothing. I try a lot of things and fail more than that. That's how I learn. Whenever I succeed, I note that in my cookbook. Eventually, that became my blog. 
rooshohttps://www.roosho.com
I am Rakib Raihan RooSho, Jack of all IT Trades. You got it right. Good for nothing. I try a lot of things and fail more than that. That's how I learn. Whenever I succeed, I note that in my cookbook. Eventually, that became my blog. 

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here


Latest Articles

author avatar
roosho Senior Engineer (Technical Services)
I am Rakib Raihan RooSho, Jack of all IT Trades. You got it right. Good for nothing. I try a lot of things and fail more than that. That's how I learn. Whenever I succeed, I note that in my cookbook. Eventually, that became my blog.