Display document in UIWebView with proper format (iOS) - ios

I am opening a word document file in UIWebView.
It works perfect but the problem is that when I open that doc file it does not display in proper format like in word.
In UIWebView :
Code :
let url = NSURL(string: self.docUrl.stringByAddingPercentEscapesUsingEncoding(NSUTF8StringEncoding)!)
let request = NSURLRequest(URL: url!)
webView.loadRequest(request)
webView.delegate = self
webView.scalesPageToFit = true // I have also try by removing this statement
webView.autoresizingMask = UIViewAutoresizing.FlexibleWidth | UIViewAutoresizing.FlexibleHeight

Given that you have scales pages to fit, it looks like you might not have constraints setup correctly.
You will need to create constraints between your UIWebView and its parent to ensure that the webView takes over the whole screen.
You can create constraints both in storyboard as well as in code.

Related

Programmatically scroll pdf in UIDocumentInteractionController

I'm showing a pdf file inside a UIDocumentInteractionController, like this:
let docController = UIDocumentInteractionController(url: documentsURL)
let url = NSURL(string:"itms-books:");
if UIApplication.shared.canOpenURL(url! as URL) {
docController.delegate = self
docController.presentPreview(animated: true)
}
I need to automatically scroll to the last page when the controller is shown, is there a way to do that? I didn't find one. Thanks to anyone who will send help!
I am afraid there is not a straight forward solution for this, even the documentation says it's a UIViewController but Xcode shows it is inherited from NSObject.
I'd recommend either rendering the PDF on a WKWebView or a UIScrollView. This will give you a room to wiggle.

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()
}

Swift - iAd Banner on top of webView

So I am loading a local HTML file into a webView with this code:
let localfilePath = NSBundle.mainBundle().URLForResource("countdown", withExtension: "html");
let myRequest = NSURLRequest(URL: localfilePath!);
countdownWebView.loadRequest(myRequest);
self.view.addSubview(countdownWebView)
and am now trying to implement iAds with this standard line:
self.canDisplayBannerAds = true
However the iAd is appearing on top of my webView content, blocking part of it. Is there anything in my code that might suggest why the content is not resizing as it should be?
I would recommend not spending time on this as iAd will discontinue on June 30th.
http://www.theverge.com/2016/1/15/10777496/apple-iad-app-shutdown-june-30th-confirmation

Scale UIWebView content

I would like to ask you about a problem that I'm facing with a project. The problem is that i'm trying to show a UIWebView control, I'm loading a video streaming into the web view , this is the code:
if(self.cameraUrl != nil) {
let url = NSURL (string: self.cameraUrl!)
let requestObj = NSURLRequest(URL: url!)
self.view_webvideo.loadRequest(requestObj)
self.view_webvideo.contentMode = UIViewContentMode.Center;
let zoom = self.view_webvideo.bounds.size.width / self.view_webvideo.scrollView.contentSize.width
self.view_webvideo.scrollView.setZoomScale(zoom, animated: true)
}
As you could see in the code I had research about that and try a lot of things but i can't scale the content, the video have width=360 and height=640, I can't change the video size because it is a third party service.
Also i'm using the property: Scales Page To Fit = true
I want the video scale to fix into the webview component and don't show any scroll
I hope somebody could help me with that.
Thank you in advanced
Try replacing the last three lines with this (similar to what manman said)
self.view_webvideo.stringByEvaluatingJavaScriptFromString("document.getElementsByTagName('video')[0].style.width='100%'")

UIDocumentInteractionController displaying blank pdf

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.

Resources