0

This is the guide I'm trying to follow: https://www.youtube.com/watch?v=MBCX1atOvdA&t=878s

This is my code (Exact same as his):

import SwiftUI

struct Response: Codable {
    var results: [Result]
}

struct Result: Codable {
    var trackId: Int
    var trackName: String
    var collectionName: String
}

struct ContentView: View {
    @State private var results = [Result]()

    var body: some View {
        List(results, id: \.trackId) { item in
            VStack(alignment: .leading) {
                Text(item.trackName)
                    .font(.headline)
                Text(item.collectionName)
            }
            }.task {
                await loadData()
        }
    }
    
    func loadData() async {

        guard let url = URL(string: "https://itunes.apple.com/search?term=taylor+swift&entity") else {
            print("Invalid URL")
            return
        }
        do {
            let (data, _) = try await URLSession.shared.data(from: url)
            
            if let decodedResponse = try? JSONDecoder().decode(Response.self, from: data) {
                results = decodedResponse.results
            }
        } catch {
            print("Invalid data")
        }
        
    }
    
}

The issue is I get this error:

2022-01-19 21:34:45.807870-0500 pulldata[46561:4433324] [logging] volume does not support data protection, stripping SQLITE_OPEN_FILEPROTECTION_* flags 2022-01-19 21:34:45.813838-0500 pulldata[46561:4433324] [logging] volume does not support data protection, stripping SQLITE_OPEN_FILEPROTECTION_* flags 2022-01-19 21:34:45.841534-0500 pulldata[46561:4433324] [] nw_resolver_can_use_dns_xpc_block_invoke Sandbox does not allow access to com.apple.dnssd.service 2022-01-19 21:34:45.842830-0500 pulldata[46561:4433324] dnssd_clientstub ConnectToServer: connect() failed path:/var/run/mDNSResponder Socket:11 Err:-1 Errno:1 Operation not permitted 2022-01-19 21:34:45.842911-0500 pulldata[46561:4433324] [connection] nw_resolver_create_dns_service_locked [C1] DNSServiceCreateDelegateConnection failed: ServiceNotRunning(-65563) 2022-01-19 21:34:45.843380-0500 pulldata[46561:4433324] Connection 1: received failure notification 2022-01-19 21:34:45.844386-0500 pulldata[46561:4433324] Connection 1: failed to connect 10:-72000, reason -1 2022-01-19 21:34:45.844448-0500 pulldata[46561:4433324] Connection 1: encountered error(10:-72000) 2022-01-19 21:34:45.845461-0500 pulldata[46561:4433327] Task <3E5E1143-19D2-4141-B5CE-F646B32F892F>.<1> HTTP load failed, 0/0 bytes (error code: -1003 [10:-72000]) 2022-01-19 21:34:45.846729-0500 pulldata[46561:4433327] Task <3E5E1143-19D2-4141-B5CE-F646B32F892F>.<1> finished with error [-1003] Error Domain=NSURLErrorDomain Code=-1003 "A server with the specified hostname could not be found." UserInfo={_kCFStreamErrorCodeKey=-72000, NSUnderlyingError=0x6000024aff90 {Error Domain=kCFErrorDomainCFNetwork Code=-1003 "(null)" UserInfo={_NSURLErrorNWPathKey=satisfied (Path is satisfied), interface: en0, ipv4, dns, _kCFStreamErrorCodeKey=-72000, _kCFStreamErrorDomainKey=10}}, _NSURLErrorFailingURLSessionTaskErrorKey=LocalDataTask <3E5E1143-19D2-4141-B5CE-F646B32F892F>.<1>, _NSURLErrorRelatedURLSessionTaskErrorKey=( "LocalDataTask <3E5E1143-19D2-4141-B5CE-F646B32F892F>.<1>" ), NSLocalizedDescription=A server with the specified hostname could not be found., NSErrorFailingURLStringKey=https://itunes.apple.com/search?term=taylor+swift&entity, NSErrorFailingURLKey=https://itunes.apple.com/search?term=taylor+swift&entity, _kCFStreamErrorDomainKey=10} Invalid data 2022-01-19 21:34:45.883310-0500 pulldata[46561:4433298] [com.what.pulldata] copy_read_only: vm_copy failed: status 1.

Yes I know that the site doesn't exist anymore, but the code doesn't work with any site and I feel it has something to do with this part of the error

Sandbox does not allow access to com.apple.dnssd.service

Any other videos showing me how to get data from websites using swift, or any help with this guide would be greatly appreciated!

Ken White
  • 123,280
  • 14
  • 225
  • 444
  • Not sure but this one might help: https://stackoverflow.com/questions/65279008/swiftui-macos-fetch-json-error-logging-volume-does-not-support-data-protection – Shawn Frank Jan 20 '22 at 03:43

1 Answers1

0

try something like this with optionals, works for me:

import SwiftUI

struct Response: Codable {
    let results: [Result]
}

struct Result: Codable {
    var trackId: Int?        // <--- here optional
    var trackName: String?    // <--- here optional
    var collectionName: String? // <--- here optional
}

struct ContentView: View {
    @State private var results = [Result]()
    
    var body: some View {
        List(results, id: \.trackId) { item in
            VStack(alignment: .leading) {
                Text(item.trackName ?? "").font(.headline) // <--- here optional
                Text(item.collectionName ?? "") // <--- here optional
            }
        }.task {
            await loadData()
        }
    }
    
    func loadData() async {
        guard let url = URL(string: "https://itunes.apple.com/search?term=taylor+swift&entity") else {
            print("Invalid URL")
            return
        }
        do {
            let (data, _) = try await URLSession.shared.data(from: url)
            if let decodedResponse = try? JSONDecoder().decode(Response.self, from: data) {
                results = decodedResponse.results
            }
        } catch {
            print("Invalid data")
        }
    }
  
}
  • I got an error from your code. I did add the import swift ui, I just got another long error code, I'm unsure what I am doing wrong. I also did get this a part of the error as well: ```Sandbox does not allow access to com.apple.dnssd.service``` Maybe that has something to do with it? – Cat needs logic Jan 20 '22 at 04:44
  • ok, what is your environment/system and targets. Try to delete "App Sandbox" in the "Signing & Capabilities". The code in my answer works well for me on macos 12.2, xcode 13.2, targets ios 15 and mac catalyst. On macos, simply tick the box "Outgoing Connections" in the App Sandbox. – workingdog support Ukraine Jan 20 '22 at 05:05
  • correction, you can leave the "App SandBox", but ensure that the `Network` -> `Outgoing Connections (client)` is ticked. This works for me on ios 15, macos 12.2, and mac catalyst. Tested on real devices, not Preview. – workingdog support Ukraine Jan 20 '22 at 05:14
  • Very strange, there seems to be a bug in Xcode. When I create a new `macos` project, copy and paste the code into it, I get the error that `nw_resolver_can_use_dns_xpc_block_invoke Sandbox does not allow access to com.apple.dnssd.service`, but if I tick the `Network` -> `Outgoing Connections (client)` in in the "App SandBox", all works well. However if I now un-tick the `Outgoing Connections (client)`, it still works. – workingdog support Ukraine Jan 20 '22 at 05:56
  • It works now, I had to tick that box for it to work, thank you so much! – Cat needs logic Jan 20 '22 at 12:40