WebView not showing my html file - ios

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.

Related

can't access ios bundle resource

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"

Swift 3 - WKWebView Load Local HTML not loading resource files

I was download my website from online to store in xcode project with a sub-folder and want to run in WKWebView locally. I found the webview loaded webpage miss all CSS/JS/Images resources. I search around the internet and still not working. Is webpage inside relative url incorrect or my code to get the Main Bundle location problem? Anyone can help. Thanks.
Xcode Project Structure
HTML Page Contents Sample
I wrote this code to load WKWebView
let htmlFile = Bundle.main.path(forResource: "provisiontech", ofType: "htm")
let html = try? String(contentsOfFile: htmlFile!, encoding: String.Encoding.utf8)
let baseURL = Bundle.main.resourceURL!.appendingPathComponent("pvtc")
webView.loadHTMLString(html!, baseURL: baseURL)
The webview has no access to the resource folder. You should use loadFileURL(_:allowingReadAccessTo:) instead. Preloading the data is not neccessary, e.g.:
let url = Bundle.main.url(forResource: "provisiontech", ofType: "htm")
webView.loadFileURL(url, allowingReadAccessTo:
Bundle.main.resourceURL!.appendingPathComponent("pvtc"))
Edit: If the webview still cannot access the resources, you should consider whether putting the page and all resources into the same folder.

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!

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!

Playing Video from Online URL works but from Local Path doesn't

I am having trouble playing a local video on my computer. I used a library called Player which is quite straight forward, and in the example project, it's using a Vine video with supplying a link at the top, and it's working:
let videoUrl = NSURL(string: "https://v.cdn.vine.co/r/videos/AA3C120C521177175800441692160_38f2cbd1ffb.1.5.13763579289575020226.mp4")!
Thus, in ViewDidLoad, self.player.setUrl(videoUrl) works.
I tried to download the Vine video to my local. I saved it as aaa.mp4.
I dragged the mp4 file in xCode project.
I added the file in Copy Bundle Resources
Then I used;
let path = NSBundle.mainBundle().pathForResource("aaa", ofType:"mp4")!
let videoUrl = NSURL(string: path)!
viewDidLoad() {
// print(path)
// print(videoUrl)
self.player.setUrl(videoUrl)
}
The video doesn't get played, but
print(path) - gives me:
/Users/sentiasa/Library/Developer/CoreSimulator/Devices/67160785-1BDB-4046-BD58-A8C448938A4F/data/Containers/Bundle/Application/5456FF89-904F-4AE6-A90C-747B6A371745/Player.app/aaa.mp4
and print(videoUrl) - gives me:
/Users/sentiasa/Library/Developer/CoreSimulator/Devices/67160785-1BDB-4046-BD58-A8C448938A4F/data/Containers/Bundle/Appl ... /aaa.mp4
Please note that, if I give the path a filename that doesn't exists, it throws an error, thus I know the file is there (and exists) in my case.
What may be the problem? What am I missing? I don't receive any warnings or errors
Use
NSURL(fileURLWithPath: path)!
instead.

Resources