Hi I open one url in my app
UIApplication.shared.open(webURL! as URL, options: [:])
I want when finished work in browser I get response , and get last URL of webpage
how to do that?
swift
Put below code in your viewDidLoad.
let webView = UIWebView()
webView.scalesPageToFit = true
webView.delegate = self
view.addSubview(webView)
webView.fillOnSupperView()
webView.delegate = self
let request = URLRequest(url:
URL(string:"https://www.google.com"))
webView.loadRequest(request) // load your url here
Related
I get a PDF in Json, retrieve it and have it displayed in a webview, but I also need it to be shared with the UIActivityViewController, but when I pass the webview it does not show options available for sharing
I am doing the following:
//I get the json
if let url = URL(string: "http://10.1.80.106:9000/api/recibo/imprimir?vigencia=2015-06-30") {
var urlRequest = URLRequest(url: url)
urlRequest.setValue("application/pdf", forHTTPHeaderField: "Content-Type")
urlRequest.setValue("aaaabbbccc", forHTTPHeaderField: "Authorization")
//i create the webview
let webView = UIWebView(frame: self.view.frame)
webView.loadRequest(urlRequest as URLRequest)
webView.scalesPageToFit = true
let pdfVC = UIViewController()
pdfVC.view.addSubview(webView)
pdfVC.title = "Recibo de Pagamento"
self.navigationController?.pushViewController(pdfVC, animated: true)
//instantiate the UIActivityViewController for the share, and step the WebView as parameter
let vc = UIActivityViewController(activityItems: [(webView.request?.url?.absoluteString) as Any], applicationActivities: [])
if let popoverController = vc.popoverPresentationController {
popoverController.sourceView = self.view
popoverController.sourceRect = self.view.bounds
}
self.present(vc, animated: true, completion: nil)
}
Actually, instead of WebView you could also use the QuickLook Framework, which is designed for this specific purpose. You just pass
the location of the file and conform to the protocols. It will present
a view controller with the document inside. You can also have the native options to share the file.
Check out this answer about it's implementation and example.
I have link for web soket. I checked it on https://www.websocket.org and it connected. However when I start to create URL object
if let url = URL(string: "wss://ios-devchallenge-11.tk/write?id={id}") {
socket = WebSocket(url: url, protocols: ["chat", "superchat"])
socket?.delegate = self
socket?.connect()
}
I got error. What do I wrong?
I turn out I set wrong url. I change code as below (removed {})
if let url = URL(string: "wss://ios-devchallenge-11.tk/write?id=id") {
socket = WebSocket(url: url, protocols: ["chat", "superchat"])
socket?.delegate = self
socket?.connect()
}
I am having trouble loading a local HTML file. Here is my code. please help.
let URL = NSBundle.mainBundle().pathForResource("index2", ofType: "html")
let request = NSURLRequest(URL: url!)
Webview.loadRequest(request)
by use the following code. I have manage to load the html file but it doesn't load up the CSS!
let htmlFile = NSBundle.mainBundle().pathForResource("index1", ofType: "html")
let html = try? String(contentsOfFile: htmlFile!, encoding: NSUTF8StringEncoding)
GSFWebView.loadHTMLString(html!, baseURL: nil)
Simply write this code inside your ViewDidLoad i have added url of this page as example, just replace it with yours. On storyboard you don't need to do anything, if you have any extra view added on you view controller as container for website then replace self.view in my code with your view.
let myWebView:UIWebView = UIWebView(frame: CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: UIScreen.main.bounds.height))
//add your url on line below
myWebView.loadRequest(URLRequest(url: URL(string: "https://stackoverflow.com/questions/26647447/load-local-html-into-uiwebview-using-swift")!))
self.view.addSubview(myWebView)
And if you want to load page from local file, you can do it by defining url this way.
Swift 2
let url = NSBundle.mainBundle().URLForResource("webFileName", withExtension:"html")
Swift 3
let url = Bundle.main.url(forResource: "webFileName", withExtension: "html")
I only had to add this code to the viewDidLoad method:
1- First of all, you have to get the local URL from the Bundle. (previously, I added the index.html file to the project, copying when asked)
2- Then you have to create the request URL to be loaded from the webView.
3- After all, add the new webView created as a subView from the main view.
That worked for me.
if let url = Bundle.main.url(forResource: "index", withExtension: "html"){// 1st step
webView.loadFileURL(url, allowingReadAccessTo: url)
let request = URLRequest(url: url as URL) // 2nd step
webView.load(request)
self.view.addSubview(self.webView) // 3rd step
}
In order to load urls, I tried to connect .loadRequest with webview, but there is not such loadRequest. Is there anyone who knows why this is occurring to me?
func configurationView(){
if let webData: Web = self.detailItem {
if let webView = self.WebView {
let url = NSURL(string: webData.url)
let urlRequest = NSURLRequest(URL: url!)
(here I am trying to dp WebView.loadRequest(urlRequest), but I cannot!)
}
}
}
I am using AlamofireImage in my project quite a lot and I use
let URL = NSURL(string: "https://cdn.domain.com/profile/image.jpg")!
imageView.af_setImageWithURL(URL)
to fetch an image from my CDN. I have done some tests but correct me if I am wrong, this seems to store the downloaded image in to a cache. My tests included downloading a 5mb image. The first time it took about 20 seconds, the second time was instant.
The thing I would like to know is how can I clear the cache for a specific URL/image and re-download the image?
Say for example I update a users profile pic. The image name/URL will be exactly the same but I know the image has changes as the user selected a new image from their library or camera. I know the image has been uploaded successfully to the CDN as I can see the new image in the folder directly on the CDN.
You need to remove the image from the in-memory cache as well as the on-disk cache. You can do this as follows:
func clearImageFromCache() {
let URL = NSURL(string: "https://cdn.domain.com/profile/image.jpg")!
let URLRequest = NSURLRequest(URL: URL)
let imageDownloader = UIImageView.af_sharedImageDownloader
// Clear the URLRequest from the in-memory cache
imageDownloader.imageCache?.removeImageForRequest(URLRequest, withAdditionalIdentifier: nil)
// Clear the URLRequest from the on-disk cache
imageDownloader.sessionManager.session.configuration.URLCache?.removeCachedResponseForRequest(URLRequest)
}
Currently, the URLCache can only be cleared in this manner on the master branch. I just pushed f35e4748 which allows access to the underlying sessionManager in the ImageDownloader. This is not yet available in an actual release yet, but should be here sometime this week.
Swift3 and AlamofireImage 3.x
It appears removeImage does all that is needed.
// Clear what is in the cache, this will force a refresh to ensure fresh image is loaded next time
let urlRequest = Foundation.URLRequest(url: validUrl)
let imageDownloader = UIImageView.af_sharedImageDownloader
if let imageCache2 = imageDownloader.imageCache {
_ = imageCache2.removeImage(for: urlRequest, withIdentifier: nil)
}
Hope this might help you:
let URL = NSURL(string: "https://cdn.domain.com/profile/image.jpg")!
let imageDownloader = UIImageView.af_sharedImageDownloader
let imageCache = imageDownloader.imageCache
// Setting CachePolicy as reloadIgnoringLocalCacheData so that it won't use URL Cache next time when it is hitting same URL
let urlRequest = URLRequest(url: URL, cachePolicy: URLRequest.CachePolicy.reloadIgnoringLocalCacheData)
// Clear the Image from the in-memory cache
let _ = imageCache?.removeImage(for: urlRequest, withIdentifier: nil)
imageView.af_setImage(withURLRequest: urlRequest, placeholderImage: UIImage(named: "placeholder"), completion: { (response) in
self.imageView.image = response.result.value
})
Swift 5:
func clearImageCache(forUrl urlString: String) {
guard let url = URL(string: urlString) else {
return
}
let imageDownloader = UIImageView.af_sharedImageDownloader
let urlRequest = URLRequest(url: url)
// Clear the URLRequest from the in-memory cache
_ = imageDownloader.imageCache?.removeImage(for: urlRequest, withIdentifier: nil)
// Clear the URLRequest from the on-disk cache
imageDownloader.session.sessionConfiguration.urlCache?.removeCachedResponse(for: urlRequest)
}
My solution for the caching issue:
func af_setImageIgnoreCache(string: String?) {
guard let url = string, let nsurl = URL(string: url) else { return }
let urlRequest = URLRequest(url: nsurl, cachePolicy: .reloadIgnoringCacheData)
let imageDownloader = ImageDownloader.default
if let imageCache = imageDownloader.imageCache as? AutoPurgingImageCache, let urlCache = imageDownloader.sessionManager.session.configuration.urlCache {
_ = imageCache.removeImages(matching: urlRequest)
urlCache.removeCachedResponse(for: urlRequest)
}
af_setImage(withURLRequest: urlRequest)
}
The response left by #cnoon was correct. The reason it isn't working for so many of you was because you were probably using some sort of filter and passing nil as the withIdentifier parameter.
I was using a circular filter in this scenario:
// Clearing the cache didn't work like this
imageDownloader.imageCache?.removeImage(for: urlRequest, withIdentifier: nil)
// Worked when adding a CircleFilter().identifier() as `withIdentifier` as such:
imageDownloader.imageCache?.removeImage(for: urlRequest, withIdentifier: CircleFilter().identifier)