Partial component from URL in Swift - swift

I have this URL:
file:///var/mobile/Containers/Data/Application/182BB05D-E460-42CD-9030-EBD907799E0F/Documents/video/f9cf9f593a3f50c9ee50acb51aaeee1ee1febbe3c2cd4aded49b39e01b42f9ac/6B617D7C-F153-4F7C-BE02-4362CAC4A4C9.mov
And I want to get just this string back from the URL
video/f9cf9f593a3f50c9ee50acb51aaeee1ee1febbe3c2cd4aded49b39e01b42f9ac/6B617D7C-F153-4F7C-BE02-4362CAC4A4C9.mov
Is using pathComponents the only way?

You can try to get the documents directory path and remove it from your video file path.
Example :
let documentsPath = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0]
let videoPath = "file:///var/mobile/Containers/Data/Application/182BB05D-E460-42CD-9030-EBD907799E0F/Documents/video/f9cf9f593a3f50c9ee50acb51aaeee1ee1febbe3c2cd4aded49b39e01b42f9ac/6B617D7C-F153-4F7C-BE02-4362CAC4A4C9.mov"
videoPath.replacingOccurrences(of: documentsPath, with: "")

Try this..
var filePath = "file:///var/mobile/Containers/Data/Application/182BB05D-E460-42CD-9030-EBD907799E0F/Documents/video/f9cf9f593a3f50c9ee50acb51aaeee1ee1febbe3c2cd4aded49b39e01b42f9ac/6B617D7C-F153-4F7C-BE02-4362CAC4A4C9.mov"
if let range = filePath.range(of: "video")?.lowerBound {
let url = filePath.substring(from: range)
}

Related

swift save file after download Alamofire

download and save file
let destination: DownloadRequest.DownloadFileDestination = { _, _ in
// var fileURL = self.createFolder(folderName: downloadFolderName)
var fileURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0]
let fileName = URL(string : currentFile.link )
fileURL = fileURL.appendingPathComponent((fileName?.lastPathComponent)!)
return (fileURL, [.removePreviousFile, .createIntermediateDirectories])
}
Alamofire.download(currentDownloadedFile.link , to: destination).response(completionHandler: { (DefaultDownloadResponse) in
print("res ",DefaultDownloadResponse.destinationURL!);
completion(true)
})
but when i wont to check file in this dirrectory i get nil
let filemanager:FileManager = FileManager()
let fileURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0]
let files = filemanager.enumerator(atPath: fileURL.absoluteString) // = nil
while let file = files?.nextObject() {
print(file)
}
if i save local path to file and after reload app wont to share it -> "share" app cant send file (mb cant found it)
can u pls help me. how it works ? why when i print all files he didnt find it? how to save file who after reboot app it will be saved in same link
You are using the wrong API
For file system URLs use always path, absoluteString returns the full string including the scheme (e. g. file:// or http://)
let files = filemanager.enumerator(atPath: fileURL.path)

In Swift 3, how to display images from link to local file directory in tableviewcell

I want to save images in local file not on server and displays those images in UItableviewcell via link to that file\folder.
I am not sure what path i used to save in database. I am saving imagefolder in desktop.But that path: /desktop/imagefolder/image.jpg not working.
iOS apps have access only to sandbox , that mean you write only in the folder Document , Temp and Library . See the documentation.
Here an exemple how to write and read an image in documents directory.
let documentsDirectory = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0]
let path = "\(documentsDirectory)/image.png"
let image = UIImage(contentsOfFile: path)
And now how to save:
let image = ...
let documentsDirectory = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0]
let path = "\(documentsDirectory)/image.png"
if let data = UIImagePNGRepresentation(image!) {
try? data.write(to: URL(fileURLWithPath: path))
}

How to get user home directory path (Users/"user name") without knowing the username in Swift3

I am making a func that edits a text file in the Users/johnDoe Dir.
let filename = "random.txt"
let filePath = "/Users/johnDoe"
let replacementText = "random bits of text"
do {
try replacementText.write(toFile: filePath, atomically: true, encoding: .utf8)
}catch let error as NSError {
print(error: + error.localizedDescription)
}
But I want to be able to have the path universal. Something like
let fileManager = FileManager.default
let downloadsURL = FileManager.default.urls(for: .downloadsDirectory, in: .userDomainMask).first! as NSURL
let downloadsPath = downloadsURL.path
but for the JohnDoe folder. I haven't been able to find any documentation on how to do this. The closest thing I could find mentioned using NSHomeDirectory(). And I am not sure how to use it in this context.
when I try adding it like...
let fileManager = FileManager.default
let downloadsURL = FileManager.default.urls(for: NSHomeDirectory, in: .userDomainMask).first! as NSURL
let downloadsPath = downloadsURL.path
I get an error:
"Cannot Convert value of type 'String' to expected argument type 'FileManager.SearchPathDirectory'"
I've tried it .NSHomeDirectory, .NSHomeDirectory(), NShomeDirectory, NShomeDirectory()
You can use FileManager property homeDirectoryForCurrentUser
let homeDirURL = FileManager.default.homeDirectoryForCurrentUser
If you need it to work with earlier OS versions than 10.12 you can use
let homeDirURL = URL(fileURLWithPath: NSHomeDirectory())
print(homeDirURL.path)
There should be an easier way but -- at worst -- this should work:
let filePath = NSString(string: "~").expandingTildeInPath
Swift 5 (and maybe lower)
let directoryString: String = NSHomeDirectory()
let directoryURL: URL = FileManager.default.homeDirectoryForCurrentUser
Maybe
FileManager.homeDirectoryForCurrentUser: URL
It's listed as "beta" for 10.12, though.

instance member "baseURL" cannot be used on type "URL"

On the fifth line i m getting the error that " instance member "baseURL" cannot be used on type "URL". Someone please help me solve this error.
func getFileURL() -> URL {
let fileName = (imagePath! as NSString).lastPathComponent
let dirPath = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0]
let pathArray:[String] = [dirPath, fileName]
let fileURL = URL.baseURL(withPathComponents: pathArray)
return fileURL!
}
let fileURL = URL.baseURL(withPathComponents: pathArray)
You're calling baseURL on the class URL, not an actual instance of URL. It would be like trying to eat not an apple, but the word "apple". Try this instead:
func getFileURL() -> URL {
let fileName = (imagePath! as NSString).lastPathComponent
let dirPath = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0]
let fileURL = URL(fileURLWithPath: dirPath, isDirectory: true).appendingPathComponent(fileName)
return fileURL.baseURL!
}
EDIT: I'm not exactly sure about what you're trying to ask, so you may need to change return fileURL.baseURL! to return fileURL.
func getFileURL() -> String {
var paths: [AnyObject] = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)
filePath = paths[0].stringByAppendingString("/output/outPut.mov") // whatever filetype you want
return File Path
}

path to an audio file in resources folder in Xcode swift

In swift how can I get the file path for a test audio File that i have copied into the resources folder in Xcode
The code I have gives file not found. Am I looking in the right Directories with NSSearchPathForDirectoriesInDomains? Any help much appreciated!
let name = "test.wav"
let paths = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)[0] as NSString
let filePath = paths.stringByAppendingPathComponent(name)
let checkValidation = NSFileManager.defaultManager()
if (checkValidation.fileExistsAtPath(filePath)) {
print("found .wav")
} else {
print("file not found")
}
i just found this that works.
let name = "test.wav"
let soundPath = (NSBundle.mainBundle().resourcePath! as NSString).stringByAppendingPathComponent(name)
let soundURL = NSURL.fileURLWithPath(soundPath)