Question

How do I import 3rd part frameworks into Xcode Playground?

Swift Playground obviously has a framework import mechanism because we can import Cocoa, SpriteKit, and in an OSX Playground, XCPlayground (XCPlayground seems missing from iOS, oddly)

What I'd really like to do is hide code from my playground and be able to show a minimal example. Any thoughts on how to load a Framework into Swift playground?

See also:

(This question is different because the request is to use a framework in Playground rather than simply regular swift files)

Was it helpful?

Solution

Edited: As of Beta5 this is now supported when the playground is part of the workspace that builds the framework as a target. There are more details on the Apple dev forums site, but hopefully @Rick Ballard will add them to his answer here and that should becoke the definitive answer for this question.

Don't do the below anymore!


For the short term, while there's no supported solution, if you're producing a Module/Framework you can copy it into the SDKs System/Library/Frameworks directory and then just import <#Module#> the same way as you import system Frameworks.

For an OS X Playground: /Applications/Xcode6-Beta.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.10.sdk/System/Library/Frameworks

And for iOS: /Applications/Xcode6-Beta.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator8.0.sdk/System/Library/Frameworks/

But before you do that... go file a radar as @Rick says in his answer.

OTHER TIPS

There is currently no supported way to import your own framework or app code into a playground, short of pasting it into the playground editor. We're aware that this is very desirable functionality, but as always we encourage people to "vote with bugreporter" at bugreport.apple.com

Right now, if you're just trying to hide code for showing in an example, code folding in the editor might do the trick.

It is now possible to import your own frameworks into a playground. This provides a way to share code between your applications and playgrounds, which can both import your frameworks. To do this, your playground must be in the same workspace as the project that produces your framework. You must have already built your framework. If it is an iOS framework, it must be built for a 64-bit run destination (e.g. iPhone 5s). You must have an active scheme which builds at least one target (that target's build location will be used in the framework search path for the playground). Your "Build Location" preference (in advanced "Locations" settings) should not be set to "Legacy". If your framework is not a Swift framework the "Defines Module" build setting must be set to "Yes". Once all these conditions are fulfilled, importing your framework will work in a playground.

I've written a tutorial covering import of custom frameworks in Beta 6

In short, if you've got your playground and framework either in the same project or workspace, and your framework is built for 64 bits (even for iOS), they play very well together.

source: http://qiita.com/ryokosuge/items/2551cd4faa9dca324342

If there is anyone who still had difficulty understanding how to go about it after reading the accepted answer, this may help.

It's not in English though so I will explain here:

Using Carthage:

1: Setup your Cartfile in the root of the project, add your libraries and run:

carthage update --platform iOS --use-submodules

2: Make sure your project is saved as a Workspace. From the Xcode menu bar: File > Save As Workspace. Shutdown Xcode and reopen from the new .xcworkspace file.

3: Add your library's .xcodeproj file in your workspace: File > Add files to "your_workspace_name". Then find it in: ${SRCROOT}/Carthage/Checkouts/your_library_name/your_library_name.xcodeproj

4: Find the Embedded Binaries and Linked Frameworks and Libraries section in your project's general settings. Add your library's .framework file to both.

5: Clean(⇧+⌘+K) and build(⌘+B).

6: Import your library in the playground.

I solved it by copying the built frameworks to the Built Products Directory, where Playgrounds in the workspace also searches for frameworks. Note: you also need to run lipo and strip away unused architectures from the FAT binaries.

The steps needed (in a nutshell):

  • Create an aggregate target
  • Add a script phase to copy the frameworks from Carthage/Build/iOS/ to BUILT_PRODUCTS_DIR (I use a swift script in the example below, but you can use also use bash, ruby python etc.)
  • Run aggregate target Now you can import the framework(s) in a Playground

Additionally you can leverage the aggregate target in your app:

  • Embed frameworks from BUILT_PRODUCTS_DIR, not Carthage/Build/iOS/
  • Add aggregate target to app scheme to re-establish frameworks after a clean.

Here is an example project setup as above: https://github.com/richardnees/CarthagePlaygrounds

Like using Objective-C classes in Swift code, you can import 3rd-party library/classes into your project and Xcode will ask you if you wanna create a <#YourProjectName>-Bridging-Header header file. Choose yes and import all header files you need in that file. After that, in your playground, you can simply do import <#YourProjectName>-Bridging-Header after import UIKit and you will be able to use the classes you imported to your project.

Update: this is actually incorrect. Please see my comment below. Sorry for any confusion...

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top