In my iOS application I have found huge memory usage on presenting a UIWebView. I am trying to implement a didReceiveMemoryWarning delegate method. So, what is best practice for watching for memory warnings with a UIWebView?
Here is the code. The problem is that it is not catching the memory warnings which makes me think I am implementing the delegate wrong.
class SigninViewController: UIViewController, UIWebViewDelegate {
var appname: String = ""
var webView = UIWebView(frame: CGRectMake(0, 0, UIScreen.mainScreen().bounds.width, UIScreen.mainScreen().bounds.height))
let appManager = AppManager()
override func viewDidLoad() {
super.viewDidLoad()
let address = "https://google.com"
webView.loadRequest(NSURLRequest(URL: NSURL(string: address)!))
webView.delegate = self
self.view.addSubview(webView)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
webView.reload()
}
...
}
Related
I have a log when I load youtube URL in WKWebView.
I have searched the same title of my question in stackOverFlow.
But it didn't work for me.
What's wrong with my code?
And I use swift4 & xcode9.2.
Thanks.
Transport also set true.
Warning like this:
Could not signal service com.apple.WebKit.Networking: 113: Could not find specified service
import UIKit
import WebKit
class DetailViewController: UIViewController {
var videoId: String = ""
var videoTitle: String = ""
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
loadUI()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func loadUI() {
view.backgroundColor = UIColor.white
naviSetting()
webViewSetting()
}
func naviSetting() {
self.title = videoTitle
}
func webViewSetting() {
let webview = WKWebView(frame: CGRect(x: 0, y: 0, width: self.view.frame.width, height: self.view.frame.height))
let url = URL(string: "https://www.youtube.com/watch?v=\(self.videoId)")
let request = URLRequest(url: url!)
webview.load(request)
self.view.addSubview(webview)
}
}
I have tried to encode parameter and Its working fine for me. Please check below code.
var wkWebView = WKWebView()
override func viewDidLoad()
{
super.viewDidLoad()
self.automaticallyAdjustsScrollViewInsets = false
self.wkWebView.translatesAutoresizingMaskIntoConstraints = false
self.wkWebView.frame = CGRect.init(x: 0, y: 0, width: self.wkWebView.frame.size.width, height: self.wkWebView.frame.size.height)
self.view.addSubview(wkWebView)
loadUrl()
}
override func viewWillLayoutSubviews()
{
super.viewWillLayoutSubviews()
self.wkWebView.frame = CGRect.init(x: 0, y: 0, width: self.view.frame.size.width, height: self.view.frame.size.height)
self.wkWebView.contentMode = .scaleAspectFit
}
func loadUrl()
{
let webConfiguration = WKWebViewConfiguration()
webConfiguration.allowsInlineMediaPlayback = true
let url_join = "v=1p38GWfCIhQ"
let myURL = URL(string: "https://www.youtube.com/watch?")
var myRequest = URLRequest(url: myURL!)
myRequest.httpMethod = "POST"
myRequest.httpBody = url_join.data(using: String.Encoding.utf8)
wkWebView.load(myRequest)
}
I hope It will also work for you. :)
For me this Code worked fine.
Notice, I Used http:// and not https://.
But you already have set the settings for http:// to work.
import UIKit
import WebKit
class ViewController: UIViewController, WKNavigationDelegate {
#IBOutlet weak var webView: WKWebView!
private let url = URL(string: "http://www.google.com")
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
let request = URLRequest(url: url!)
webView = WKWebView(frame: self.view.frame)
self.view.addSubview(webView)
webView.navigationDelegate = self
webView.load(request)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
I am actually using a WKWebView in my IOS application. The url loaded successfully but when clicking on the Facebook and Twitter share buttons nothing happens. How can I solve this issue?
Here is the following code:
ViewController:
import UIKit
import WebKit
class ViewController: UIViewController,WKNavigationDelegate {
#IBOutlet var containerView : UIView! = nil
var webView = WKWebView()
override func loadView() {
super.loadView()
self.webView = WKWebView()
self.view = self.webView
}
override func viewDidLoad() {
super.viewDidLoad()
let url = NSURL(string: "https://www.examplepage.com")!
webView.load(NSURLRequest(url: url as URL) as URLRequest)
webView.allowsBackForwardNavigationGestures = true
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
I have loaded a webpage to xcode WebView. But only the upper portion of the page is loaded. I can not scroll down to the bottom of the page. Same fact for pdf. Only upper 2 pages can be scrolled. What can i do ?Here is my code.
Thanks in advance.
import UIKit
class ViewController: UIViewController {
#IBOutlet var webView: UIWebView!
override func viewDidLoad() {
super.viewDidLoad()
var URL = NSURL(string: "http://www.archetapp.com")
webView.loadRequest(NSURLRequest(URL: URL!))
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
//for pdf
import UIKit
class ViewController: UIViewController {
#IBOutlet var webViews: UIWebView!
var path = ""
override func viewDidLoad() {
super.viewDidLoad()
path = NSBundle.mainBundle().pathForResource("ibook", ofType: "pdf")!
let url = NSURL.fileURLWithPath(path)
webViews.scalesPageToFit = true
webViews.scrollView.scrollEnabled = true
webViews.userInteractionEnabled = true
self.webViews.loadRequest(NSURLRequest(URL: url!))
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}
Try this!
import UIKit
class ViewController: UIViewController, UIWebViewDelegate {
#IBOutlet var webView : UIWebView
override func viewDidLoad() {
super.viewDidLoad()
//load initial
path = NSBundle.mainBundle().pathForResource("ibook", ofType: "pdf")!
let url = NSURL.fileURLWithPath(path)
var req = NSURLRequest(URL : url)
webView.delegate = self // <---
webView.loadRequest(req)
}
func webViewDidStartLoad(webView : UIWebView) {
//UIApplication.sharedApplication().networkActivityIndicatorVisible = true
println("webViewDidStartLoad")
}
func webViewDidFinishLoad(webView : UIWebView) {
//UIApplication.sharedApplication().networkActivityIndicatorVisible = false
webViews.scalesPageToFit = true
webViews.scrollView.scrollEnabled = true
webViews.userInteractionEnabled = true
println("webViewDidFinishLoad")
}
}
I had the same issue just today and at least in my case it was caused by having the "Scale Pages to Fit" option checked in the WebView properties. I'm assuming Carlos' reply regarding the zoom scale fixes it anyways but I didn't need the option enabled in the first place so that was my easy fix.
Im crating a simple wrapper, however no matter what device I test a website with loading it into a webview its always a bit to wide and has horizontal scrolling. I can turn that off by changing the content size but then i just cut off part of the view on the right hand side.
import UIKit
class ViewController: UIViewController, UIWebViewDelegate {
#IBOutlet var webView: UIWebView?
let endpointURL : String = "http://www.google.com/"
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
webView?.delegate = self
var url : NSURL = NSURL(string: endpointURL)!
var request : NSURLRequest = NSURLRequest(URL: url)
webView?.loadRequest(request)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func webViewDidStartLoad(webView: UIWebView) {
}
func webViewDidFinishLoad(webView: UIWebView) {
//webView.scrollView.contentSize = CGSizeMake(webView.frame.size.width, webView.scrollView.contentSize.height)
}
}
Use webView.scalesPageToFit = true
This should size the web page to fit the size of the UIWebView.
I suspect your problem might be related to the frame of the WebView.Try resizing your webview to your screen bounds.
webView.frame = UIScreen.mainScreen().bounds
some disapointement with uiwebview, for very simple code
class ViewController: UIViewController {
#IBOutlet weak var mavue: UIWebView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
let requestURL = NSURL(string: "http://www.google.com")
let request = NSURLRequest(URL: requestURL!)
mavue?.loadRequest(request)
println("myWebView : \(mavue)")
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
println("myWebView : (mavue)") return nil
i try it on simulator, i have loaded my controllerview from presentViewController maybe it's the reason why?
but my code doesn't show mywebview.
mywebView is connected to my IBoutlet
thank
EDIT : It 's work, I think that th mistake was to keep the default uiviewcontroller , when i use a subclass all is ok
Thank