

To make sure the file is located there you can use the finder command Go To Folder e copy paste the printed documentDirectoryUrl.path there println(documentDirectoryUrl.path!) String.stringWithContentsOfFile is DEPRECATED and doesn't work anymore with Xcode 6.1.1Ĭreate your documentDirectoryUrl let documentDirectoryUrl = faultManager().URLsForDirectory(.DocumentDirectory, inDomains. You can use playgroundSharedDataDirectory in order to access an image located in the ~/Documents/Shared Playground Data folder of your computer from an iOS Playground: import UIKit Let text = try String(contentsOf: fileUrl, encoding: 8)
#Swift share file mac#
You can use playgroundSharedDataDirectory in order to read the content of a text file located in the ~/Documents/Shared Playground Data folder of your computer from an iOS or Mac Playground: import Foundation Let json = try JSONSerialization.jsonObject(with: data, options: ) You can use playgroundSharedDataDirectory in order to read the content of a json file located in the ~/Documents/Shared Playground Data folder of your computer from an iOS or Mac Playground: import Foundation The path to the directory containing data shared between all playgrounds. playgroundSharedDataDirectory has the following declaration: let playgroundSharedDataDirectory: URL With Swift 3, PlaygroundSupport module provides a global constant called playgroundSharedDataDirectory. Access a file that is located in the ~/Documents/Shared Playground Data folder of your computer Guard let fileUrl = (forResource: "Data", withExtension: "json") else Ģ. You can use url(forResource:withExtension:) in order to read the content of a json file located in the Resources folder of an iOS or Mac Playground: import Foundation Returns the file URL for the resource identified by the specified name and file extension. url(forResource:withExtension:) has the following declaration: func url(forResource name: String?, withExtension ext: String?) -> URL? Tap on the Edit button in the top-right corner. You’ll see two sections: Favorites and Suggestions. Open the Share Sheet and scroll to the end of the apps panel. With Swift 3, Bundle has a method called url(forResource:withExtension:). If you want consistency, it’s best to customize the apps section manually. Access a file that is located in the Resources folder of your Playground Looks like: let path = playgroundSharedDataDirectory.appendingPathComponent("file")ġ. UPDATE: For swift 4.2 use playgroundSharedDataDirectory.
#Swift share file how to#
The official documentation can be found here: XCPlayground Module ReferenceĬool post on how to organize this directory per-playground: Swift, Playgrounds, and XCPlayground Let path = XCPlaygroundSharedDataDirectoryURL.appendingPathComponent("foo.txt") You can use XCPlayground to access this shared folder. Instead, inside the container there is a symbolic link to a Shared Playground Data directory (/Users/ UserName/Documents/Shared Playground Data) which remains when reopening the playground, and can be accessed from multiple playgrounds. Create a new app extension target by going go File, New, Target in the Xcode menu, then in the left panel select Application Extension and select Share in. This means using the normal directory structure you would have to copy the file you want into the new container every time. We are going to add a method called, share, which will take three arguments, items, excludedActivityTypes and ipad.While the answer has been supplied for a quick fix, there is a better solution.Įach time the playground is opened it will be assigned a new container. Luckily it only takes one line to get UIActivityViewController to on iPad. All of these use the same code, but we will also learn how to share on iPhone and iPad because the two platform work differently. In this tutorial we will learn how to share images, text and a URL. UIActivityViewController makes sharing easy and it allows one to share many different types content including, text, images, URL's, Audio and more.
