WWDC22 SALE: Save 50% on all my Swift books and bundles! >>

URLSession.shared.data(from: url) fails in Swift Playgrounds for Mac

Forums > SwiftUI

I followed this post on receiving and sending data with UrlSession in Swift Playgrounds for iPad, and everything worked great.

I opened the same code in Swift Playgrounds for Mac, and the line:

let (data, _) = try await URLSession.shared.data(from: url)

silently fails everytime. I exported a small snippet to a playground in Mac, and found that adding:

import PlaygroundSupport
PlaygroundPage.current.needsIndefiniteExecution = true

At the beggining of the script fixes this in the playground. I am wondering if there is a way to move this code to the app version and get this up and running on the Mac? Or maybe this code was never designed to run on a Macbook?


The first line certainly should work in Xcode without the two playground lines.


Hacking with Swift is sponsored by Emerge

SPONSORED Optimize your app’s startup time, binary size, and overall performance using Emerge’s advanced app optimization and monitoring tools. Reliably measure app size, speed up your app's startup time with Emerge's Launch Booster, and much more. Emerge is actively used by many of the top mobile development teams in the world.

Find out more

Sponsor Hacking with Swift and reach the world's largest Swift community!

Reply to this topic…

You need to create an account or log in to reply.

All interactions here are governed by our code of conduct.

Unknown user

You are not logged in

Log in or create account

Link copied to your pasteboard.