SwiftUI macOS add costum file format (UTI) - swift

I have a "normal" swiftUI macOS project, and I need to read some "custom" files like data.dat and data.DCA.
How do I add these extensions to my Xcode project?
At the moment I use the following code to read the data, but for non standard file extensions I just get the error: Error Domain=NSCocoaErrorDomain Code=260 "The file “test.dat” couldn’t be opened because there is no such file." UserInfo={NSFilePath=/Users/UserName/*/RandomAppName/test.dat, NSUnderlyingError=0x600001af0450 {Error Domain=NSPOSIXErrorDomain Code=2 "No such file or directory"}}
let userName = NSUserName()
let path = "/Users/\(userName)/*/RandomAppName/test.dat"
var savedData = Data()
var savedString = String()
do {
let url = URL(fileURLWithPath: path)
savedData = try Data(contentsOf: url)
if let ssavedString = String(data: savedData, encoding: .utf8) {
savedString = ssavedString
print(savedString)
}
} catch {
print(error)
}

Related

SwiftUI macOS Sandboxed application active read the .ssh/config file

I have Sandboxed application active I need to read the following .ssh/config file.
But I am having the following error:
Ooops! Something went wrong: Error Domain=NSCocoaErrorDomain Code=260
"The file “config” couldn’t be opened because there is no such file."
UserInfo={NSFilePath=/Users/name/Library/Containers/sa.GitRepository/Data//.ssh/config,
NSUnderlyingError=0x600003ccf120 {Error Domain=NSPOSIXErrorDomain Code=2 "No such file or directory"}}
Can you tell me how I can solve the problem?
Code:
guard let desktopPath = NSSearchPathForDirectoriesInDomains(.desktopDirectory,
.userDomainMask,
true).first else { return }
_path = State(initialValue: desktopPath)
let pathConfig = desktopPath.replacingOccurrences(of: "Desktop", with: "/.ssh/config")
do {
let contents = try String(contentsOfFile: pathConfig, encoding: .utf8)
let myStrings = contents.components(separatedBy: .newlines)
let users = myStrings.filter { $0.contains("github.com-") }.map {$0.replacingOccurrences(of: "Host github.com-", with: "")}
if(users.count > 0){ _user = State(initialValue: users[0]) }
_userArray = State(initialValue: users)
}
catch let error as NSError {
print("Ooops! Something went wrong: \(error)")
}

Permissions error renaming a file on disk

I am trying to rename a file on the local hard disk from a swift MacOS application.
Basically I am bringing up the open panel to select the folder where the files are. then I enumerate the files and rename them to the modification date.
Here is the relevant code:
let openPanel = NSOpenPanel()
openPanel.canChooseDirectories = true
openPanel.canChooseFiles = false
openPanel.canCreateDirectories = false
openPanel.allowsMultipleSelection = false
var mtsVideosFolderPathString : String! = ""
if openPanel.runModal() == NSApplication.ModalResponse.OK
{
mtsVideosFolder = openPanel.urls[0] as URL
mtsVideosFolderPathString = mtsVideosFolder?.path
let fileManager = FileManager.default
let enumerator = fileManager.enumerator(atPath: mtsVideosFolderPathString)
while let element = enumerator?.nextObject() as? String
{
if element.hasSuffix("MTS")
{
let filePath = "\(mtsVideosFolderPathString!)/\(element)"
let fileModDate = self.fileModificationDate(url: URL(fileURLWithPath: filePath))
let format = DateFormatter()
format.timeZone = .current
format.dateFormat = "yyyy.MM.dd HH.mm.ss"
let dateString = format.string(from: fileModDate!)
let fromFilename = "\(mtsVideosFolderPathString!)/\(element)"
let toFilename = "\(mtsVideosFolderPathString!)/\(dateString).mts"
print("Rename \(fromFilename) to \(toFilename)")
do {
try fileManager.moveItem(at: URL(fileURLWithPath: fromFilename), to: URL(fileURLWithPath: toFilename))
}
catch let error as NSError
{
print("Ooops! Something went wrong: \(error)")
}
}
}
}
When I run the app its fails at moveItem in the try/catch with the following error:
Rename /Volumes/BigNFast/test/00000.mts to /Volumes/BigNFast/test/2013.08.05 20.09.50.mts
Ooops! Something went wrong: Error Domain=NSCocoaErrorDomain Code=513 "“00000.mp4” couldn’t be moved because you don’t have permission to access “ test”." UserInfo={NSSourceFilePathErrorKey=/Volumes/BigNFast/test/00000.mp4, NSUserStringVariant=(
Move
), NSDestinationFilePath=/Volumes/BigNFast/test/2013.08.05 20.09.50.mp4, NSFilePath=/Volumes/BigNFast/test/00000.mp4, NSUnderlyingError=0x6000002bb450 {Error Domain=NSPOSIXErrorDomain Code=1 "Operation not permitted"}}
So, the question is, how do I set the permissions? How to I rename a file on disk?
Thank you for any help
Signing and Capabilities -> File Access -> User Selected File
Change it to read and write with the selector.

NSCocoaErrorDomain Code=257 file couldn’t be opened because you don’t have permission to view it : FileManager attributesOfItem returns nil in iOS13

FileManager returns permission error while trying to get the file size, in iOS 13 devices.
do {
let attr = try FileManager.default.attributesOfItem(atPath: my_file_path) //--> Getting nil
fileSize = attr[FileAttributeKey.size] as! UInt64
} catch {
print("Error: \(error)")
}
Error returned:
Error Domain=NSCocoaErrorDomain Code=257 "The file “trim.1A9FFC19-EE2C-438A-BF3D-97E05A97EF9E.MOV” couldn’t be opened because you don’t have permission to view it." UserInfo={NSFilePath=/private/var/mobile/Containers/Data/PluginKitPlugin/ADB8684E-12B5-451D-A20F-158B899BB3DD/tmp/trim.1A9FFC19-EE2C-438A-BF3D-97E05A97EF9E.MOV, NSUnderlyingError=0x280af0510 {Error Domain=NSPOSIXErrorDomain Code=1 "Operation not permitted"}}
The issue observed only after I updated to iOS 13. In earlier versions everything is working fine.
iOS 13 SDK consider photo app as an another app, so when we dismiss the image picker controller video url will be invalidate.
I had the problem before when I try to upload video to AWS, what i did just create a temporary folder and copy the existing video url path before dismiss the Image-picker.then it upload, it's worked.
func createDirectory(videoURL:URL){
let Directorypath = getDirectoryPath()
var objcBool:ObjCBool = true
let isExist = FileManager.default.fileExists(atPath:Directorypath,isDirectory: &objcBool)
// If the folder with the given path doesn't exist already, create it
if isExist == false{
do{
try FileManager.default.createDirectory(atPath: Directorypath, withIntermediateDirectories: true, attributes: nil)
}catch{
print("Something went wrong while creating a new folder")
}
}
let fileManager = FileManager()
do {
if fileManager.fileExists(atPath:Directorypath) {
try? fileManager.removeItem(at: URL(fileURLWithPath:Directorypath))
}
try fileManager.copyItem(at:videoURL.absoluteURL, to: URL(fileURLWithPath:Directorypath))
self.imagePicker.dismiss(animated: true, completion:nil)
}catch let error {
print(error.localizedDescription)
}
}

Error Domain=NSCocoaErrorDomain Code=256

override func viewDidLoad() {
super.viewDidLoad()
let dataURLString: String = Bundle.main.path(forResource: "IMG_0568", ofType: "JPG")!
let dataURL = URL(string: dataURLString)
do {
let binaryData = try Data(contentsOf: dataURL!, options: [])
let kbData = binaryData.subdata(in: 0..<1024)
let stringArray = kbData.map{String(format: "%02X", $0)}
let binaryString = stringArray.joined(separator: "-")
print(binaryString)
editorTextView.text = (binaryString)
} catch {
print("Failed to read the file.")
//Error Domain=NSCocoaErrorDomain Code=256 "The file “IMG_0568.JPG” couldn’t be opened." UserInfo={NSURL=/Users/..../IMG_0568.JPG}
}
I want to display the Binary Data of Image File that I have added to my Xcode project (image name: IMG_0568.JPG).
But there's the error
(Error Domain=NSCocoaErrorDomain Code=256 "The file “IMG_0568.JPG”
couldn’t be opened." UserInfo={NSURL=/Users/..../IMG_0568.JPG})
How can I fix this problem?
This is a very common mistake:
URLs in the file system must be initialized with URL(fileURLWithPath
let dataURL = URL(fileURLWithPath: dataURLString)
The difference is:
URL(fileURLWithPath expects a path starting with a slash like /Users/myUser/file.ext
URL(string expects an URL string including the scheme like file:///Users... or http://example.com
However you can avoid the mistake by using the URL related API of Bundle
let dataURL = Bundle.main.url(forResource: "IMG_0568", withExtension: "JPG")!

How can I read a file in a swift playground

Im trying to read a text file using a Swift playground with the following
let dirs : String[]? = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.UserDomainMask, true) as? String[]
if (dirs != nil) {
let directories:String[] = dirs!;
let dir = directories[0]; //documents directory
let path = dir.stringByAppendingPathComponent(file);
//read
let content = String.stringWithContentsOfFile(path, encoding: NSUTF8StringEncoding, error: nil)
}
However this fails with no error. It seems the first line stops the playground from outputting anything below
You can also put your file into your playground's resources. To do this: show Project Navigator with CMD + 1. Drag and drop your file into the resources folder. Then read the file:
On Xcode 6.4 and Swift 1.2:
var error: NSError?
let fileURL = NSBundle.mainBundle().URLForResource("Input", withExtension: "txt")
let content = String(contentsOfURL: fileURL!, encoding: NSUTF8StringEncoding, error: &error)
On Xcode 7 and Swift 2:
let fileURL = NSBundle.mainBundle().URLForResource("Input", withExtension: "txt")
let content = try String(contentsOfURL: fileURL!, encoding: NSUTF8StringEncoding)
On Xcode 8 and Swift 3:
let fileURL = Bundle.main.url(forResource: "Input", withExtension: "txt")
let content = try String(contentsOf: fileURL!, encoding: String.Encoding.utf8)
If the file has binary data, you can use NSData(contentsOfURL: fileURL!) or Data(contentsOf: fileURL!) (for Swift 3).
While the answer has been supplied for a quick fix, there is a better solution.
Each time the playground is opened it will be assigned a new container. This means using the normal directory structure you would have to copy the file you want into the new container every time.
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.
You can use XCPlayground to access this shared folder.
import XCPlayground
let path = XCPlaygroundSharedDataDirectoryURL.appendingPathComponent("foo.txt")
The official documentation can be found here: XCPlayground Module Reference
Cool post on how to organize this directory per-playground: Swift, Playgrounds, and XCPlayground
UPDATE: For swift 4.2 use playgroundSharedDataDirectory. Don't need to import anything.
Looks like:
let path = playgroundSharedDataDirectory.appendingPathComponent("file")
1. Access a file that is located in the Resources folder of your Playground
With Swift 3, Bundle has a method called url(forResource:withExtension:). url(forResource:withExtension:) has the following declaration:
func url(forResource name: String?, withExtension ext: String?) -> URL?
Returns the file URL for the resource identified by the specified name and file extension.
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
do {
guard let fileUrl = Bundle.main.url(forResource: "Data", withExtension: "json") else { fatalError() }
let data = try Data(contentsOf: fileUrl)
let json = try JSONSerialization.jsonObject(with: data, options: [])
print(json)
} catch {
print(error)
}
You can use url(forResource:withExtension:) in order to read the content of a text file located in the Resources folder of an iOS or Mac Playground:
import Foundation
do {
guard let fileUrl = Bundle.main.url(forResource: "Text", withExtension: "txt") else { fatalError() }
let text = try String(contentsOf: fileUrl, encoding: String.Encoding.utf8)
print(text)
} catch {
print(error)
}
As an alternative to let image = UIImage(named: "image"), you can use url(forResource:withExtension:) in order to access an image located in the Resources folder of an iOS Playground:
import UIKit
do {
guard let fileUrl = Bundle.main.url(forResource: "Image", withExtension: "png") else { fatalError() }
let data = try Data(contentsOf: fileUrl)
let image = UIImage(data: data)
} catch {
print(error)
}
2. Access a file that is located in the ~/Documents/Shared Playground Data folder of your computer
With Swift 3, PlaygroundSupport module provides a global constant called playgroundSharedDataDirectory. playgroundSharedDataDirectory has the following declaration:
let playgroundSharedDataDirectory: URL
The path to the directory containing data shared between all playgrounds.
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
import PlaygroundSupport
do {
let fileUrl = PlaygroundSupport.playgroundSharedDataDirectory.appendingPathComponent("Data.json")
let data = try Data(contentsOf: fileUrl)
let json = try JSONSerialization.jsonObject(with: data, options: [])
print(json)
} catch {
print(error)
}
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
import PlaygroundSupport
do {
let fileUrl = PlaygroundSupport.playgroundSharedDataDirectory.appendingPathComponent("Text.txt")
let text = try String(contentsOf: fileUrl, encoding: String.Encoding.utf8)
print(text)
} catch {
print(error)
}
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
import PlaygroundSupport
do {
let fileUrl = PlaygroundSupport.playgroundSharedDataDirectory.appendingPathComponent("Image.png")
let data = try Data(contentsOf: fileUrl)
let image = UIImage(data: data)
} catch {
print(error)
}
Swift 3 (Xcode 8)
The code below works in both iOS and macOS playgrounds. The text file ("MyText.txt" in this example) must be in the Resources directory of the playground. (Note: You may need to open the navigator window to see the directory structure of your playground.)
import Foundation
if let fileURL = Bundle.main.url(forResource:"MyText", withExtension: "txt")
{
do {
let contents = try String(contentsOf: fileURL, encoding: String.Encoding.utf8)
print(contents)
} catch {
print("Error: \(error.localizedDescription)")
}
} else {
print("No such file URL.")
}
This works for me. The only thing I changed was to be explicit about the file name (which is implied in your example) - perhaps you have a typo in the off-screen definition of the "file" variable?
let dirs = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.UserDomainMask, true) as? [String]
let file = "trial.txt" // My change to your code - yours is presumably set off-screen
if let directories = dirs {
let dir = directories[0]; //documents directory
let path = dir.stringByAppendingPathComponent(file);
//read
let content = NSString(contentsOfFile: path, usedEncoding: nil, error: nil)
// works...
}
Update Swift 4.2
As #raistlin points out, this would now be
let dirs = NSSearchPathForDirectoriesInDomains(
FileManager.SearchPathDirectory.documentDirectory,
FileManager.SearchPathDomainMask.userDomainMask,
true)
or, more tersely:
let dirs = NSSearchPathForDirectoriesInDomains(.documentDirectory,
.userDomainMask, true)
Select the .playground file.
Open Utility inspector, In the playground press opt-cmd-1 to open the File Inspector. You should see the playground on the right. If you don't have it selected, press cmd-1 to open the Project Navigator and click on the playground file.
Under 'Resource Path' in Playground Settings choose 'Relative To Playground' and platform as OSX.
On Mavericks with Xcode 6.0.1 you can read using iOS platform too.
import UIKit
let dirs : [String]? = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.UserDomainMask, true) as? [String]
let myDir = "/Shared Playground Data"
let file = "README.md" // My change to your code - yours is presumably set off-screen
if (dirs != nil) {
let directories:[String] = dirs!;
let dir = directories[0] + myDir; // iOS playground documents directory
let path = dir.stringByAppendingPathComponent(file);
//read
let content = String.stringWithContentsOfFile(path, encoding: NSUTF8StringEncoding, error: nil)
// works...
println(content!)
}
Remember, you need to create a directory called "Shared Playground Data" in your Documents directory. Im my case I used this command: mkdir "/Users/joao_parana/Documents/Shared Playground Data" and put there my file README.md
String.stringWithContentsOfFile is DEPRECATED and doesn't work anymore with Xcode 6.1.1
Create your documentDirectoryUrl
let documentDirectoryUrl = NSFileManager.defaultManager().URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask).first! as NSURL
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!)
// should look like this: /Users/userName/Library/Containers/com.apple.dt.playground.stub.OSX.PLAYGROUNDFILENAME-5AF5B25D-D0D1-4B51-A297-00015EE97F13/Data/Documents
Just append the file name to the folder url as a path component
let fileNameUrl = documentDirectoryUrl.URLByAppendingPathComponent("ReadMe.txt")
var fileOpenError:NSError?
Check if the file exists before attempting to open it
if NSFileManager.defaultManager().fileExistsAtPath(fileNameUrl.path!) {
if let fileContent = String(contentsOfURL: fileNameUrl, encoding: NSUTF8StringEncoding, error: &fileOpenError) {
println(fileContent) // prints ReadMe.txt contents if successful
} else {
if let fileOpenError = fileOpenError {
println(fileOpenError) // Error Domain=NSCocoaErrorDomain Code=XXX "The file “ReadMe.txt” couldn’t be opened because...."
}
}
} else {
println("file not found")
}
I was unable to read a file with ease in playground and ended up just creating a command line app in Xcode. This seemed to work for me very well.
The other answers, relying on "playgroundSharedDataDirectory" never works for me, especially if using an iOS playground.
let documentsDirectoryShareURL = PlaygroundSupport.playgroundSharedDataDirectory.absoluteURL
let fileManager = FileManager()
try? fileManager.copyItem(at: URL(fileURLWithPath: "/Users/rufus/Documents/Shared Playground Data/"), to: documentsDirectoryShareURL)
I just do the above now. I can populate my documents/shared folder, and it is just manually automatically copied to the playgrounds documents directory.
My code will not overwrite files that exist there. You could enhance this if you need it to look at file timestamps and then copy if necessary etc.
Swift 5.7.1 - Xcode 14.1
func readFile() -> [String] {
if let fileURL = Bundle.main.url(forResource: "File", withExtension: "txt") {
do {
let content = try String(contentsOf: fileURL)
var x = content.components(separatedBy: "\n")
x.removeAll { data in
data.isEmpty
}
return x
} catch {
print(error)
}
}
return [String]()
}
//Usage:
let input = readFile()