can't access ios bundle resource - ios

I am adding my first .wav file to my iOS project using XCode 11.3.1. I have tried various ways to access this resource:
let fileURL = Bundle.main.url(forResource: "greetings.wav", withExtension: nil)
let fileURL = Bundle.main.url(forResource: "greetings", withExtension: "wav")
let fileURL = Bundle.main.path(forResource: "greetings", ofType: "wav")
In all of these cases, fileURL is nil. Other things I have tried:
When I click on the .wav in Project Navigator, I see that Target Membership is checked for my target.
I have ensured greetings.wav is in the same folder as my ViewController which is executing the above statement.
I have also tried both the "Copy to Group" and "Copy to Folder Reference" when adding this resource
Nothing as of yet has produced a non-nil fileURL. Any help/additional troubleshooting so that I can get a valid URL for my .wav file would be greatly appreciated! Thanks.

Check whether your file is available in Copy Bundle Resources under build settings

Just offering possible suggestions here:
Check if you have imported the AVFoundation Module
Check if your resource is called "greetings.wav"

Related

WebView not showing my html file

I have a webView in my ViewController. I have created a BullsEye.html file in my project and I want to show that html file in my web view. Following is my code
if let url = Bundle.main.url(forResource: "BullsEye",
withExtension: "html") {
if let htmlData = try? Data(contentsOf: url) {
let baseURL = URL(fileURLWithPath: Bundle.main.bundlePath)
webView.load(htmlData, mimeType: "text/html",
textEncodingName: "UTF-8", baseURL: baseURL)
}
}
The above code is written in viewDidLoad. What am I missing?
I ran your code as is and was able to make things work as seen here after changing the webView loader code. This tells me you likely have a UIWebView lurking somewhere, likely your view in storyboard.
I would recommend you make sure that you are consistently using WKWebView throughout:
Import WebKit in your class file
Have a valid output link to it if you are using a storyboard
If using a storyboard, ensure that you are using WKWebView and not the deprecated UIWebView
There is no problem with your source code itself, I think.
But maybe does Bundle.main.url(forResource: "BullsEye", withExtension: "html") return nil?
If so, you should check for the two things below:
Whether the file to be read is included in Copy Bundle Resources
Files to be included in the project are registered in TARGETS> Build Phases> Copy Bundle Resources.
Whether the file to be read exists in the project directory
Open the project directory in the Finder and check if the file you are trying to load actually exists.
Hope this helps!
Below code will help you.
func loadHtmlFile() {
if let fileurl = Bundle.main.url(forResource: "BullsEye", withExtension: "html") {
let request = URLRequest(url: fileurl!)
webView.loadRequest(request)
}
}
See - swiftdeveloperblog
Also if the code don't seem to work for you, make sure to open html file in any browser and check if it's a valid html file.

Storing Sounds In a Folder (Swift File Path) [duplicate]

I have a set of audio files inside a folder. I am able to access the file when the file is placed at main bundle, but if the files are moved inside the folder I am not able to access the files.
Code:
let audioFileName:String = "audioFiles/" + String(index)
let audioFile = NSBundle.mainBundle().pathForResource(audioFileName, ofType: "mp3")!
I have an audio file inside the folder audioFiles and I would want to get its path.
Error:
fatal error: unexpectedly found nil while unwrapping an Optional value
First make sure when you drag your folder audioFiles to your project to check copy items if needed and select create folder references. Make sure it shows a blue folder if your project.
Also NSBundle method pathForResource has an initialiser that you can specify in which directory your files are located:
let audioFileName = "audioName"
if let audioFilePath = Bundle.main.path(forResource: audioFileName, ofType: "mp3", inDirectory: "audioFiles") {
print(audioFilePath)
}
If you would like to get that file URL you can use NSBundle method URLForResource(withExtension:, subdirectory:)
if let audioFileURL = Bundle.main.url(forResource: audioFileName, withExtension: "mp3", subdirectory: "audioFiles") {
print(audioFileURL)
}
The answer by Leo should work, the critical step is checking "Copy Items if Needed". However, what do you do if you already created a file or forgot that critical step?
Its easy to fix.
Go to Project -> Build Phases -> Copy Bundle Resources
Here you will see a handy list of all the files you've added to your bundle (including all your xcassets!)
Click the add button, and find your missing unlinked file.
Your code will now work (I built my own example, so it won't be same as yours in name):
if let url = Bundle.main.url(forResource: "testData2", withExtension: "txt") {
do {
let myData = try Data(contentsOf: url)
print(myData.count)
} catch {
print(error)
}
}
So why weren't we finding the file to begin with? Simple. We were asking the bundle. "Hey where is my file". Bundle was like... I don't know about that file, and if you checked the list of what was part of it (the Bundle Resources that are being copied) it wasn't there. taDa!

Xcode and iOS app data file placements defaults

Is my understanding correct for iOS7-iOS9:
1)
"built-in" app cache path is NSHomeDirectory() + "/Library/Caches/"
2)
app launch images and icons are placed NSBundle.mainBundle().resourcePath! (where does this map down to?)
3) a "blue" folder I have added in "xcode - build phases - copy bundle resources called "ownassets"" is placed in...? NSHomeDirectory() + "/Library/"
I am just tying to make 100% sure if I understood correctly where files are placed, so I can read and load them during app execution (and write in my cache)
Note: I realize that release code should use system calls to get paths since Apple may changes paths in future iOS versions.
Note For hose looking on how to get normal IO file read access to folders and content you added in "Build phases > Copy Bundle Resources" this is how: NSBundle.mainBundle().resourcePath! + "/" + nameoffolder
You should not think much about the actual locations on disk, but use the higher-level system APIs to find files/paths:
Caches directory:
let cacheURL = try NSFileManager.defaultManager().URLForDirectory(.CachesDirectory, inDomain: .UserDomainMask, appropriateForURL: nil, create: true)
App resources in the bundle:
let image = UIImage(named: "imageName")
let path = NSBundle.mainBundle().pathForResource("image", ofType: "png")
App resources in subdirectories:
let path = NSBundle.mainBundle().pathForResource("info", ofType: "dat", inDirectory: "AdditionalResources")
For more info, see the File System Programming Guide.

Swift - How do I get the file path inside a folder

I have a set of audio files inside a folder. I am able to access the file when the file is placed at main bundle, but if the files are moved inside the folder I am not able to access the files.
Code:
let audioFileName:String = "audioFiles/" + String(index)
let audioFile = NSBundle.mainBundle().pathForResource(audioFileName, ofType: "mp3")!
I have an audio file inside the folder audioFiles and I would want to get its path.
Error:
fatal error: unexpectedly found nil while unwrapping an Optional value
First make sure when you drag your folder audioFiles to your project to check copy items if needed and select create folder references. Make sure it shows a blue folder if your project.
Also NSBundle method pathForResource has an initialiser that you can specify in which directory your files are located:
let audioFileName = "audioName"
if let audioFilePath = Bundle.main.path(forResource: audioFileName, ofType: "mp3", inDirectory: "audioFiles") {
print(audioFilePath)
}
If you would like to get that file URL you can use NSBundle method URLForResource(withExtension:, subdirectory:)
if let audioFileURL = Bundle.main.url(forResource: audioFileName, withExtension: "mp3", subdirectory: "audioFiles") {
print(audioFileURL)
}
The answer by Leo should work, the critical step is checking "Copy Items if Needed". However, what do you do if you already created a file or forgot that critical step?
Its easy to fix.
Go to Project -> Build Phases -> Copy Bundle Resources
Here you will see a handy list of all the files you've added to your bundle (including all your xcassets!)
Click the add button, and find your missing unlinked file.
Your code will now work (I built my own example, so it won't be same as yours in name):
if let url = Bundle.main.url(forResource: "testData2", withExtension: "txt") {
do {
let myData = try Data(contentsOf: url)
print(myData.count)
} catch {
print(error)
}
}
So why weren't we finding the file to begin with? Simple. We were asking the bundle. "Hey where is my file". Bundle was like... I don't know about that file, and if you checked the list of what was part of it (the Bundle Resources that are being copied) it wasn't there. taDa!

allowing NSBundle.mainBundle() in swift iOS app to find "der" format certificate files

I am extremely new to Swift iOS programming in xcode. For whatever reason, my NSBundle.mainBundle() is able to find image files in the code base,
var path = NSURL(fileURLWithPath: NSBundle.mainBundle()
.pathForResource("icon", ofType: "png")!)
returns a non-nil string.
However, I have a "DER" format public CA certificate in the same directory.
path = NSURL(fileURLWithPath: NSBundle.mainBundle()
.pathForResource("cacer", ofType: "der")!)
returns nil.
Any advice to give? Any extra information needed?
Looks like cancer.der isn't referenced to app target.
Make sure that checkbox is on for that file:

Resources