Open some items in UIWebView, and others in external browser - ios

I'm creating a html5 app, embed in UIWebView. I have delegate webview to controller, and on shouldStartLoadWithRequest, I'm looking for url pattern to open or not on external browser.
It's work perfect!
But when phone has no connection, I'm loading local html file. Because shouldStartLoadWithRequest return FALSE on first request, offline.html not loading
Anyone has do that?

Try this immediately after set path :
do {
let path = NSBundle.mainBundle().pathForResource("offline", ofType: "html", inDirectory:"offline")
let string = try String(contentsOfFile: path!, encoding:NSUTF8StringEncoding)
webView.loadHTMLString(string, baseURL: NSURL(string: "http://"))
} catch {
print(error)
}
or
let url = NSURL(fileURLWithPath:NSBundle.mainBundle().pathForResource("offline", ofType:"html", inDirectory: "offline")!)
let request = NSURLRequest(URL: url);
webView.loadRequest(request);
I tried it and it works.
Make sure your "offline" directory is a folder (BLUE) and not a group (YELLOW).
Hope this help.

I found the problem. I'm not triggering error -1003 (no host found)! removing switch work perfectly!

Related

In webView leads to crash and UI unresponsiveness

I have added a WebView to load HTML and the baseUrl method. But it leads to crashing the app and showing a warning
This method should not be called on the main thread as it may lead to
UI unresponsiveness.
//load HTML
let htmlPath = Bundle.main.path(forResource: "index", ofType: "html")
let folderPath = Bundle.main.bundlePath
let baseUrl = URL(fileURLWithPath: folderPath, isDirectory: true)
do {
let htmlString = try NSString(contentsOfFile: htmlPath!, encoding: String.Encoding.utf8.rawValue)
self.webView.loadHTMLString(htmlString as String, baseURL: URL(string: newBaseURL))
} catch {
// catch error
print(error.localizedDescription)
}
I have called this piece of code in viewDidLoad(). Also it has been added in dispatch queue. Any help is much appreciated.
Call loadHTMLString from the main thread.
DispatchQueue.main.async {
self.webView.loadHTMLString(htmlString as String, baseURL: URL(string: newBaseURL))
}
You have 2 issues and one of them is a bug address by Apple itself -
a. Warning of WKWebkit on main thread - This has been covered by one of Apple's engineers where he goes into detail on this thread. Short answer - Its a bug and Apple is working on it in further releases. Now according to them this is supposed to be a warning and it shouldnt have any issues with the API itself.
b. The Webview is unresponsive. As explained above the warning shouldn't be causing an issue with anything API related. So for this I would suggest you to check the HTML you are trying to render separately. If that works fine, maybe try to launch it in a separate device, or even simulator. Try to make the HTML render on a consistent basis in other environment(device / simulator). I think either the device you are using has an older OS.
Please try to test the same HTML on separate devices / simulators and share your results here.
To fix this issue, you can move the code that loads the HTML string and sets the baseURL to a background thread using DispatchQueue. This will allow the main thread to continue running and handling user input, preventing the app from becoming unresponsive. Checkout the below code:
DispatchQueue.global(qos: .userInitiated).async {
let htmlPath = Bundle.main.path(forResource: "index", ofType: "html")
let folderPath = Bundle.main.bundlePath
let baseUrl = URL(fileURLWithPath: folderPath, isDirectory: true)
do {
let htmlString = try NSString(contentsOfFile: htmlPath!, encoding: String.Encoding.utf8.rawValue)
DispatchQueue.main.async {
self.webView.loadHTMLString(htmlString as String, baseURL: URL(string: newBaseURL))
}
} catch {
// catch error
print(error.localizedDescription)
}
}

Swift4 app crashes when loading local HTML [duplicate]

This question already has answers here:
WKWebView does load resources from local document folder
(8 answers)
Closed 5 years ago.
While working on a web app for weeks, I used a external URL in my (WKWebView) web view. Now I'm moving towards production I want to embed the webapp and load a local webpage.
I simply moved from
let url = URL(string: "http://hidden-url.com/")
self.webView!.load(URLRequest(url: url!))
To
let url = URL(string: Bundle.main.path(forResource: "index_prod", ofType: "html")!)
self.webView!.load(URLRequest(url: url!))
But this causes my app to crash. I'm sure the file is loaded correctly and a print before, in-between and after the lines will appear in the console.
The error: Thread 1: EXC_BAD_ACCESS (code=1, address=0x10)
You need to load the string content from the file, use contentsOfFile method to get the string and use loadHTMLString method of webview, print the error or create some enum which shows the error
enum WebError: Swift.Error {
case fileNotFound(name: String)
case parsing(contentsOfFile: String)
}
guard let url = Bundle.main.path(forResource: "index_prod", ofType: "html") else {
throw WebError.fileNotFound(name: file) // throw file not found error
}
do {
let html = try String(contentsOfFile: url)
self.webView.loadHTMLString(html, baseURL: Bundle.main.bundleURL)
} catch {
throw WebError.parsingError(contentsOfFile: file)
}

WebView (not WKWebView) calling Localized HTML file

A Mac app requires that a HTML file be called in a WebView (the legacy type, not the newer WKWebView) in a localized form to present the user with some content.
As I side note, I realize that WebView should not be used today, and WKWebView is preferred, however this is a legacy app that currently needs support.
I've used a similar method for the iOS version, however it does not seem to be working. The HTML files are simply called "Term.HTML" and are placed in each localization folder alongside the localized string and all other localized content. This is the code I tried to use:
NSString *htmlFile = [[NSBundle mainBundle] pathForResource:NSLocalizedString(#"fileTerm", nil) ofType:#"html"];
htmlString = [NSString stringWithContentsOfFile:htmlFile encoding:NSUTF8StringEncoding error:nil];
[termsView takeStringURLFrom:htmlString];
Where my localized strings file each contain a line that says:
"fileTerm" = "Term";
This is what links the declaration of the first line to the actual file. It works in iOS. However, when running the app and the view containing the WebView attempt to the run, XCode will automatically create a breakpoint on the third line when I actually attempt to give the HTML file to "termsView" which is my WebView. After skipping this breakpoint, and forcing the app to run, the whole view containing the WebView will simply not appear. I would be thankful if anyone knew why this was or if there was a better way to do this? Thank you everyone!
may be someone needs in SWift: I solved this problem with saving 3 html file for every language, and then in ViewController class checked current app language. And called up the html file for current language
func loadHtmlFile() {
let preferredLanguage = NSLocale.preferredLanguages[0]
if preferredLanguage == "kz" {
let url = Bundle.main.url(forResource: "aboutUs_kz", withExtension:"html")
let request = URLRequest(url: url!)
webView.load(request)
}
if preferredLanguage == "ru" {
let url = Bundle.main.url(forResource: "aboutUs_ru", withExtension:"html")
let request = URLRequest(url: url!)
webView.load(request)
}
if preferredLanguage == "en" {
let url = Bundle.main.url(forResource: "aboutUs_en", withExtension:"html")
let request = URLRequest(url: url!)
webView.load(request)
}
}
in viewDidLoad()
override func viewDidLoad() {
super.viewDidLoad()
loadHtmlFile()
}

AVPlayer - Play from Local URL

Surprisingly i have not been able to find an answer to this despite many searching.
I have a file that has been cached, i can retrieve the cache directory and file name which i believe to be the url?
this is code to get the file and then play it
let cachedFilePath = getCachedFilePath.result as! String
audioFileURL = cachedFilePath
self.audioPlayerItem = AVPlayerItem(url: NSURL(string: audioFileURL!) as! URL)
This is what is returned if i print audioFileURL:
/Users/Genie/Library/Developer/CoreSimulator/Devices/A2FB00CE-B018-4FDF-9635-35FD6678DF8D/data/Containers/Data/Application/E18B5E89-B973-4277-AA5C-1378C69D80CD/Library/Caches/Parse/PFFileCache/7e4a8fc0eb37b655b859e4959f986da2_The%20Name%20of%20the%20Wind%2017-92.mp3
The player loads but it never plays, It just sits there spinning. so i am wondering if i am passing the url to the player correctly?
i read on one thread to use file:// before it but that didn't work.
In the place where you're creating an NSURL, you have the replace the argument name string to fileURLWithPath
Change
NSURL(string: audioFileURL!)
to
NSURL(fileURLWithPath: audioFileURL!)
Also, why are you casting NSURL to URL ? Why not use URL directly?
try like this,i hope it will work for you.
self.audioPlayerItem = AVPlayerItem(url:NSURL(fileURLWithPath: audioFileURL!) as! URL)
AVPlayerItem(url:URL(fileURLWithPath: Bundle.main.path(forResource: "filename", ofType: "mp4")!))
And make sure your video is added to the bundle:
project root > your Target > Build Phases > Copy Bundle Resources.

Youtube videos cannot be played in UIWebView (iOS, Swift)

I think I know what was my problem but I couldn't find a solution for it.
On my app, you can see that there is a YouTube video, but when you click on it, it won't play the video. Here's the line of code that may cause the issue:
webView.loadHTMLString(partyRock.videoURL, baseURL: nil)
I think that sending nil to the baseURL may cause the issue, but I'm not sure what to replace with.
Thanks
Why are you using loadHTMLString? This property sets the main page content and base URL and I don't think you want to do that.
Use this instead:
let requestURL = URL(string: partyRock.videoURL)
let request = URLRequest(url: requestURL!)
webView.loadRequest(request)
let request = URLRequest(url: URL(string: "https://www.youtube.com/watch?v=KBcIKsJBo2Y")!)
webView.loadRequest(request)

Resources