Can Safari App Extensions use URLSession objects?

Hi All,

I'm trying to develop a Safari App extension with a view to porting our legacy Safari extension to the new system however I have run into a few problems.


I'm new to MacOS and Swift development so please bear with me. I want to load a block of JSON data from our server as part of the required dataset for our extension and I'm using the following Swift code.


class JSON {

  static func get(from: String) {

    let source = URL(string: from)
    let request = URLRequest.init(url: source!)
        URLSession.shared.downloadTask(with: request, completionHandler: {(location, response, error) -> Void in
            print("DOWNLOAD TASK COMPLETE")
         
            if let err = error {
                print("ERROR OCCURRED \(err)")
            } else {
             
                print("RESPONSE \(response!)")
            }
         
         
         
        }).resume()
  }
}


The problem is that despite me knowing the server is online and the data is accessible I get the following error in the console when I try and run the Extension scheme (so that it runs within Safari and I can see the output in the console log).


2018-03-27 16:36:02.810738+0100 Safari[70551:12491320] could not create directory "" for future sandbox extension, error Error Domain=NSCocoaErrorDomain Code=512 "The file “” couldn’t be saved." UserInfo={NSFilePath=}
2018-03-27 16:36:03.110644+0100 Donation Reminder[70565:12492045] dnssd_clientstub ConnectToServer: connect()-> No of tries: 3
2018-03-27 16:36:04.115774+0100 Donation Reminder[70565:12492045] dnssd_clientstub ConnectToServer: connect() failed path:/var/run/mDNSResponder Socket:5 Err:-1 Errno:1 Operation not permitted
2018-03-27 16:36:04.116170+0100 Donation Reminder[70565:12492045] [] nw_resolver_create_dns_service_locked DNSServiceCreateConnection failed: ServiceNotRunning(-65563)


I can't see anywhere in the docs where you specify the destination location for the temporary file that the downloadTask uses but I'm guessing from looking at the error is that the Safari app extension is running in it's own sandbox and doesn't have access to the system temporary folder - is this correct because I can't find any info about what permissions the extension has or what restrictions it runs under.


Does anyone know what I'm doing wrong (or advise me on how to download a file from the extensions controller?


Any help gratefully received.