12

I'm trying to learn about working with APIs in Swift. As a good first test, I figured I'd hit the itunes API and just return some search results. I'm in playground using the following code. I don't get any errors, but my println isn't outputting anything. Anyone know what's wrong?

Thanks!

import UIKit
import XCPlayground

XCPSetExecutionShouldContinueIndefinitely()


let url = NSURL(string: "https://itunes.apple.com/lookup?id=909253")

let task = NSURLSession.sharedSession().dataTaskWithURL(url!) {(data, response, error) in
    println(NSString(data: data, encoding: NSUTF8StringEncoding))
}

task.resume()
user2874270
  • 1,312
  • 2
  • 18
  • 31
  • 1
    I'm not sure, but playground is a bit limited in concurrent functions. Try it in a simple appdelegate. – qwerty_so Feb 05 '15 at 20:12
  • As Nate points out below, using your code in a playground I get an error. Using `http` instead of `https` it all works as expected though. My guess is it has something to do with some failure in https in the playground, probably not enough things get automatically configured. – David Berry Feb 05 '15 at 20:23
  • @David I tried it with http but still no dice. I looked in the console and I'm seeing this: 2015-02-05 14:26:35.440 MyPlayground[16591:718022] Failed to obtain sandbox extension for path=/var/folders/zf/vnqxfh0d1mqfggy11rs3mc1sxvfpzn/T/com.apple.dt.Xcode.pg/containers/com.apple.dt.playground.stub.iOS_Simulator.MyPlayground-06DB641A-E22C-41A4-A4C6-9D32890615D0/Library/Caches/com.apple.dt.playground.stub.iOS_Simulator.MyPlayground-06DB641A-E22C-41A4-A4C6-9D32890615D0. Errno:1 2015-02-05 14:26:35.441 MyPlayground[16591:718022] – user2874270 Feb 05 '15 at 20:28
  • 1
    If I run it in the full simulator (with "Run In Full Simulator") checked, I get the log message(s) you show, but the request works and prints the appropriate results. If I uncheck run in full simulator, it works with no errors at all (both using http and not https) – David Berry Feb 05 '15 at 20:39
  • Actually, in the full simulator it works using https, although I get the sandbox extension message. – David Berry Feb 05 '15 at 20:40
  • This definitely seems to be a simulator setup issue. It comes and goes as I restart the simulator and change Run in Full Simulator. I'd just put it in an application and see what happens (it should work fine, there's not anything wrong with the code) – David Berry Feb 05 '15 at 20:42
  • 1
    Tried it outside the sandbox in a simple appdelegate as @ThomasKilian suggested and it worked. Not sure what the point of the playground is if i can't get stuff to work in it! Thanks everyone for the help – user2874270 Feb 05 '15 at 20:58
  • 1
    Well, the playground is not construction place for a high rise ;-) So it's a bit limited. But it works for quite a couple of use cases. – qwerty_so Feb 05 '15 at 21:24
  • 1
    I had exactly the same issue with the Playground, in my case the very same code that didn't do anything on playground worked perfect when applied it to simulator and real device. My Advice, take all of your code from playground to a Swift file – Jorge Vicente Mendoza Oct 14 '15 at 02:52

6 Answers6

26

Try adding this line of code:

URLCache.shared = URLCache(memoryCapacity: 0, diskCapacity: 0, diskPath: nil)
Paul Roub
  • 36,322
  • 27
  • 84
  • 93
rv1331
  • 261
  • 3
  • 2
  • 1
    I wish this was built in... also that I could read playground books on osx but that's neither here nor there. – Joe Susnick Feb 22 '17 at 20:09
21

For Swift 3/Xcode 8.2.1, combining a couple of the answers (rv1331, Wouter van Zuilen) gave the best results with no errors in Playground. I'm using this Swift 3 REST example:

http://mrgott.com/swift-programing/30-work-with-rest-api-in-swift-3-and-xcode-8-using-urlsession-and-jsonserialization

import PlaygroundSupport
import Foundation

PlaygroundPage.current.needsIndefiniteExecution = true
URLCache.shared = URLCache(memoryCapacity: 0, diskCapacity: 0, diskPath: nil)
pvanallen
  • 549
  • 4
  • 10
11

Swift 3 code:

<!-- language: lang-c -->

import PlaygroundSupport
PlaygroundPage.current.needsIndefiniteExecution = true
slfan
  • 8,950
  • 115
  • 65
  • 78
5

try following line and i guess it will work:

XCPlaygroundPage.currentPage.needsIndefiniteExecution = true
Jasmin
  • 794
  • 7
  • 18
0

Not sure why you aren't seeing an error—I get this:

NSURLConnection/CFURLConnection HTTP load failed (kCFStreamErrorDomainSSL, -9807)

You can test more effectively with httpbin.org—this works perfectly for me:

let url = NSURL(string: "http://httpbin.org/get")

let task = NSURLSession.sharedSession().dataTaskWithURL(url!) {(data, response, error) in
    println(NSString(data: data, encoding: NSUTF8StringEncoding))
}

task.resume()
Nate Cook
  • 92,417
  • 32
  • 217
  • 178
  • Thanks, I tried that, but now in console i see this error: 2015-02-05 14:26:35.440 MyPlayground[16591:718022] Failed to obtain sandbox extension for path=/var/folders/zf/vnqxfh0d1mqfggy11rs3mc1sxvfpzn/T/com.apple.dt.Xcode.pg/containers/com.apple.dt.playground.stub.iOS_Simulator.MyPlayground-06DB641A-E22C-41A4-A4C6-9D32890615D0/Library/Caches/com.apple.dt.playground.stub.iOS_Simulator.MyPlayground-06DB641A-E22C-41A4-A4C6-9D32890615D0. Errno:1 – user2874270 Feb 05 '15 at 20:28
0

My answer is not correct, it's really only the in the playgound indefinitite page execution. The session type does not matter.

Run into the same issue recently and found a solution.

The error is caused by using URLSession.shared() which apparently is not allowed in a playground sandbox.

Create and use your own ephemeral URL session instead:

let session = URLSession(configuration: URLSessionConfiguration.ephemeral)

jezovec
  • 1
  • 1
  • 1
    No, `URLSession.shared()` works very well in Playground, I use it all the time. You just have to set the Playground in asynchronous mode with `XCPlayground` or `PlaygroundSupport`. – Eric Aya Nov 18 '16 at 09:50
  • Strange. After testing it again, it works with the shared session for me, too. Go figure... I am almost sure it did not before. – jezovec Nov 19 '16 at 17:41
  • Playgrounds are very convenient but not very stable, maybe yours had an issue before. – Eric Aya Nov 19 '16 at 17:52
  • Dunno. Actually, using `ephemeral` session instead of a `shared` or `default` one seems to have one nice side—effect: it at least removes the _Failed to obtain sandbox extension_ error messages. – jezovec Nov 20 '16 at 08:02