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
Related
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.
Because I could just find some outdated information / not working solutions to my problem I decided to ask this kind of question again. (See outdated / wrong solutions:)
WKWebView: Is it possible to preload multiple URLs?
(Xcode, Swift) Is it possible to load multiple WKWebViews simultaneously if they are on different viewControllers?
Swift 3, iOS 10.3 - Preload UIWebView during Launch Screen
My app is separated in one native part and one HTML part. The HTML is saved as a local file (index.html) and should be load into the myWebView view.
#IBOutlet weak var myWebView: UIWebView!
func loadWebview() {
let url = Bundle.main.url(forResource: "index", withExtension: "html")
let request = URLRequest(url: url!)
myWebView.loadRequest(request)
myWebView.scrollView.isScrollEnabled = false
myWebView.allowsLinkPreview = false
myWebView.delegate = self
}
Because my DOM tree is very large, switching from the native part to the web part (on button click) takes quite a long time at -least for the first time switching- because afterward, I'm sure the webView-request gets cached.
To my question: How can I preload the WebView on app init to avoid the white screen (maybe 0.5s - 1s duration) when switching from the native to the Web part?
EDIT:
The WKWebView is displaying the scrollbar while the UIWebView was not!
Using (like with UIWebView) this styles:
::-webkit-scrollbar {
display: none;
}
is not working and adding these lines:
webview.scrollView.showsHorizontalScrollIndicator = false
webview.scrollView.showsVerticalScrollIndicator = false
is also not working at all.
Firstly, you should switch to WKWebView,UIWebView is no longer recommended to be used by Apple.
Secondly, you can create a pool of web views that get created and asked to load when the app starts. This way by the time the user switches to the web interface the web view might've got a chance to fully load.
For this you can use a class like this:
/// Keeps a cache of webviews and starts loading them the first time they are queried
class WebViewPreloader {
var webviews = [URL: WKWebView]()
/// Registers a web view for preloading. If an webview for that URL already
/// exists, the web view reloads the request
///
/// - Parameter url: the URL to preload
func preload(url: URL) {
webview(for: url).load(URLRequest(url: url))
}
/// Creates or returns an already cached webview for the given URL.
/// If the webview doesn't exist, it gets created and asked to load the URL
///
/// - Parameter url: the URL to prefecth
/// - Returns: a new or existing web view
func webview(for url: URL) -> WKWebView {
if let cachedWebView = webviews[url] { return cachedWebView }
let webview = WKWebView(frame: .zero)
webview.load(URLRequest(url: url))
webviews[url] = webview
return webview
}
}
and ask it to preload the url sometimes during the app startup:
// extension added for convenience, as we'll use the index url in at least
// two places
extension Bundle {
var indexURL: URL { return self.url(forResource: "index", withExtension: "html")! }
}
webviewPreloader.preload(url: Bundle.main.indexURL)
Thirdly, you might need to use a container view instead of the actual web view, in your controller:
#IBOutlet weak var webviewContainer: UIView!
What remains is to add the preloaded web view to the container when needed:
func loadWebview() {
// retrieve the preloaded web view, add it to the container
let webview = webviewPreloader.webview(for: Bundle.main.indexURL)
webview.frame = webviewContainer.bounds
webview.translatesAutoresizingMaskIntoConstraints = true
webview.autoresizingMask = [.flexibleWidth, .flexibleHeight]
webviewContainer.addSubview(webview)
}
And not lastly, be aware that keeping alive instances of web views, might carry performance penalties - memory and CPU-wise.
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%'")
I have a sign up and sign in view controller and a moving gif background along with other storyboarded UI elements.
Currently I have the gif coding on each view controller obviously making the gif restart as I segue between each view controller and also more concerning having a quick pop of white before it loads whenever moving between the pages.
What can I add to my code to:
1. Prevent the white pop when it segues
2. Swapping between view controllers smoothly with the gif just continuing as if nothing has happened, so it looks like you move the UI not the page.
Look at Vine Sign In / Sign Up for reference as it basically functions in a similar way and want the background gif to function as it does on Vine.
override func viewDidLoad() {
super.viewDidLoad()
let filePath = NSBundle.mainBundle().pathForResource("beachwater", ofType: "gif")
let gif = NSData(contentsOfFile: filePath!)
let webViewBG = UIWebView(frame: self.view.frame)
webViewBG.loadData(gif!, MIMEType: "image/gif", textEncodingName: "UTF-8", baseURL: NSURL(string: "")!)
webViewBG.userInteractionEnabled = false;
webViewBG.layer.zPosition = -3.0;
self.view.addSubview(webViewBG)
As a starting point don't use a UIWebView for this. It is the reason you are seeing a white background just before the gif, the web view has been rendered on screen then shortly after the gif is rendered.
I'd recommend separating out the frames of the gif and loading them into a UIImage (and finally into a UIImageView) using one of the following techniques:
animatedImageNamed:duration:
animatedImageWithImages:duration:
If you don't want to separate the frames, use a gif rendering library such as FLAnimatedImage
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.