Архив

Архив Март 2024
27 марта 2024 Нет комментариев
import UIKit
import WebKit
class ViewController: UIViewController, WKUIDelegate{
    var webView: WKWebView!
    lazy var progressbar: UIProgressView = UIProgressView();
    deinit {
        self.webView.removeObserver(self, forKeyPath: "estimatedProgress")
        self.webView.scrollView.removeObserver(self, forKeyPath: "contentOffset")
    }
    override func viewDidLoad() {
        super.viewDidLoad()
        let webConfiguration = WKWebViewConfiguration();
        webConfiguration.dataDetectorTypes = [.all]
        webView = WKWebView(frame: .zero, configuration: webConfiguration)
        webView.uiDelegate = self
        webView.translatesAutoresizingMaskIntoConstraints = false
        self.webView.navigationDelegate = self
        view.addSubview(webView)
        self.webView.frame = self.view.frame
        self.webView.translatesAutoresizingMaskIntoConstraints = false
        NSLayoutConstraint.activate([
            webView.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor),
            webView.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor),
            webView.leadingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leadingAnchor),
            webView.trailingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.trailingAnchor)
        ])
        self.webView.addSubview(self.progressbar)
        self.setProgressBarPosition()
        webView.scrollView.addObserver(self, forKeyPath: "contentOffset", options: .new, context: nil)
        self.progressbar.progress = 0.1
        self.progressbar.trackTintColor = .white
        self.progressbar.progressTintColor = UIColor(red:244/255.0, green: 66/255.0, blue: 8/255.0, alpha: 1)
        webView.addObserver(self, forKeyPath: "estimatedProgress",options: .new, context: nil)
        let myURL = URL(string:"https://ya.ru")
        let myRequest = URLRequest(url: myURL!)
        webView.load(myRequest)
        let refreshControl = UIRefreshControl()
        refreshControl.addTarget(self, action: #selector(reloadWebView(_:)), for: .valueChanged)
        webView.scrollView.addSubview(refreshControl)
        if #available(iOS 11.0, *){
            webView.scrollView.contentInsetAdjustmentBehavior = .never;
        }
        webView.navigationDelegate = self
        webView.allowsBackForwardNavigationGestures = true
    }
    func setProgressBarPosition(){
        self.progressbar.translatesAutoresizingMaskIntoConstraints = false
        self.webView.removeConstraints(self.webView.constraints)
        self.webView.addConstraints([
            self.progressbar.topAnchor.constraint(equalTo: self.webView.topAnchor, constant: self.webView.scrollView.contentOffset.y * -1),
            self.progressbar.leadingAnchor.constraint(equalTo: self.webView.leadingAnchor),
            self.progressbar.trailingAnchor.constraint(equalTo: self.webView.trailingAnchor)
        ])
    }
    override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
        switch keyPath{
        case "estimatedProgress":
            if self.webView.estimatedProgress >= 1.0 {
                UIView.animate(withDuration: 0.3, animations: {() in
                    self.progressbar.alpha = 0.0
                }, completion: { finished in
                    self.progressbar.setProgress(0.0, animated: false)
                })
            } else{
                self.progressbar.isHidden = false
                self.progressbar.alpha = 1.0
                progressbar.setProgress(Float(self.webView.estimatedProgress), animated: true)
            }
        case "contentOffset":
            self.setProgressBarPosition()
        default:
            super.observeValue(forKeyPath: keyPath, of: object, change: change, context: context)
        }
    }
    @objc func reloadWebView(_ sender: UIRefreshControl){
        webView.reload()
        sender.endRefreshing()
    }
    func share(message: String, link: String){
        if let link = NSURL(string: link){
            let objectsToShare = [message, link] as [Any]
            let activityVC = UIActivityViewController(activityItems: objectsToShare, applicationActivities: nil)
            self.present(activityVC, animated: true, completion: nil)
        }
    }
}
extension ViewController: WKNavigationDelegate{
    func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void){
        guard
            let url = navigationAction.request.url else{
            decisionHandler(.cancel)
            return
        }
        let string = url.absoluteString
        if(string.contains("mailto:")){
            UIApplication.shared.open(url, options: [:], completionHandler: nil)
            decisionHandler(.cancel)
            return
        }
        if(string.contains("tel:")){
            UIApplication.shared.open(url, options: [:], completionHandler: nil)
            decisionHandler(.cancel)
            return
        }
        if(string.contains("?share")){
            let urlArr = string.components(separatedBy: "?")
            let link = urlArr[0]
            share(message: "", link: link)
            decisionHandler(.cancel)
            return
        }
        decisionHandler(.allow)
    }
}

https://josh.blog/2020/02/swiftui-webview-with-a-progress-bar

Categories: Other Tags:
27 марта 2024 Нет комментариев
self.progressbar.trackTintColor = .white
self.progressbar.progressTintColor = UIColor(red:244/255.0, green: 66/255.0, blue: 8/255.0, alpha: 1)
Categories: iOS Tags:
27 марта 2024 Нет комментариев
func share(message: String, link: String){
	if let link = NSURL(string: link){
		let objectsToShare = [message, link] as [Any]
		let activityVC = UIActivityViewController(activityItems: objectsToShare, applicationActivities: nil)
		self.present(activityVC, animated: true, completion: nil)
	}
}
share(message: "test", link: "https://ya.ru/")

https://stackoverflow.com/questions/37938722/how-to-implement-share-button-in-swift

Categories: iOS Tags:
27 марта 2024 Нет комментариев
if(string.contains("?share")){
	let urlArr = string.components(separatedBy: "?")
	let link = urlArr[0]
}
Categories: iOS Tags: