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()
}
Related
Inside my app I have a html credits file which I want to load into a webView. The code I'm using works perfectly fine for my main target. However later on I created a second target: the pro-version of the app but for this one the code doesn't work because url is nil.
Both targets are selected on Target Membership for credits.html
I have tried changing the Location of the file in the identity inspector to all possibilities with no success.
I guess the file is somehow saved with a path only the main target can access.
if let urlPath = Bundle.main.path(forResource: "credits", ofType: "html") {
if let url = URL(string: urlPath) {
webView.loadRequest(URLRequest(url: url))
}
}
You need to use Bundle(identifier:) to get a bundle for a specific target. You can learn the Bundle identifier for each specific target by going to the Build settings of your target and looking at the Product Bundle Identifier variable.
if let targetBundle = Bundle(identifier: "yourBundleId"), let filePath = targetBundle.url(forResource: "credits", withExtension: "html") {
webView.loadRequest(URLRequest(url: url))
}
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)
}
Problem :
actually i am getting url link from api response. and by that link i am loading webview. but when webview load its also showing advertisement so is there any possible way to remove that ad from my webview?
here is my code
override func viewDidLoad() {
super.viewDidLoad()
let url : NSURL = NSURL(string: webviewurl)!
let request : NSURLRequest = NSURLRequest(URL: url)
myweb.loadRequest(request)
}
let me know if is there any possible way to remove ad from webview or may be from url
Usually, you can't change the content of webview you get because what you actually get is a HTML file and then rendered as a webpage.
If the ad only exists in mobile phone, there may be a DNS hijacking,
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!
I'm trying to display a pdf on iOS devices using the UIDocumentInteractionController presentPreviewAnimated method, but it keeps displaying a blank document. I think it might have to do with the character encoding, but I'm not sure. If I use a UIWebView, I can get the pdf to display, just not with the document interaction controller.
// UPDATE 9/18/14
This is now working with the GM release of Xcode 6.
// UPDATE 8/22/14
Oddly enough, from the DocumentInteractionController, if I tap on the "Open In" icon in the top right corner and choose something like iBooks, the pdf displays correctly. It seems as though it's just the preview that doesn't want to display it on the screen.
Here's my code (in Swift):
// data is coming in as NSISOLatin1StringEncoding
func displayPdfInUIDocumentInteractionController(data: NSData) {
let fileName = NSTemporaryDirectory().stringByAppendingPathComponent("myFile.pdf")
let url: NSURL! = NSURL(fileURLWithPath: fileName)
// this does not seem to make a difference
// let pdfString = NSString(data: data, encoding: NSISOLatin1StringEncoding)
// pdfString.writeToURL(url!, atomically: true, encoding: NSISOLatin1StringEncoding, error: nil)
data.writeToURL(url, atomically: true)
if url != nil {
let docController = UIDocumentInteractionController(URL: url)
docController.UTI = "com.adobe.pdf"
docController.delegate = self
docController.presentPreviewAnimated(true)
}
}
This code does display the pdf correctly:
// data is coming in as NSISOLatin1StringEncoding
func displayPdfInUIWebView(data: NSData) {
let rect = UIScreen.mainScreen().bounds
let screenSize = rect.size
let webView = UIWebView(frame: CGRectMake(0,0,screenSize.width,screenSize.height))
webView.autoresizesSubviews = true
webView.autoresizingMask = (UIViewAutoresizing.FlexibleHeight | UIViewAutoresizing.FlexibleWidth)
webView.loadData(data, MIMETYype: "application/pdf", textEncodingName: "ISO-8859-1", baseUrl: nil)
self.view.addSubview(webView)
}
Is there any reason the first function should not be working? It doesn't error out, just displays a blank page.
I'm not using Swift, but I had basically the same problem with straight up Objective-C. Before iOS8, my UIDocumentInteractionController displayed pretty much every file type i threw at it including PDF. But in iOS8, the PDF files would no longer display for me.
I WAS creating it this way:
[[[UIDocumentInteractionController alloc] init] autorelease]
I changed the call to create it like this:
[UIDocumentInteractionController interactionControllerWithURL:myUrl]
And now my PDF files display again (and the others appear to be ok too still).
This is working with the GM release of Xcode. Guess it was just a bug.