How can Swift add more functionality to web hosting pages
- 2020-06-19 11:48:47
- OfStack
preface
Following an article, we usually jump to a web hosting page in the setup center, and we usually add some features to improve the experience.
1. Add the effect of chrysanthemum loading
2. Add jump to Safari
3. Add the ability to copy links
4. Add the ability to share web pages
First, in the case of WebViewController, we will create an UIWebView. Then I created three function buttons on the left and one close button on the right, and bound them separately. The display and hiding of the load button is realized directly through the code. The code for the header status is as follows:
class WebViewController: UIViewController,UIWebViewDelegate{
let spinner = UIActivityIndicatorView()
@IBOutlet weak var WebView: UIWebView!
@IBAction func CloseAction(_ sender: Any) {
self.dismiss(animated: true, completion: nil)
}
@IBAction func SafariAction(_ sender: Any) {
self.toSafari()
}
@IBAction func CopyAction(_ sender: Any) {
self.toCopy()
}
@IBAction func MoreAction(_ sender: Any) {
self.toMore()
}
1. Add the effect of chrysanthemum loading
The load button needs to appear immediately in the center of the page when it loads, and it needs to be hidden after the page has successfully loaded by starting to rotate.
First add in the page load immediately appear loading chrysanthemum, the code is as follows:
override func viewDidLoad() {
super.viewDidLoad()
// Chrysanthemum button
spinner.activityIndicatorViewStyle = .gray
spinner.center = view.center
spinner.hidesWhenStopped = true
view.addSubview(spinner)
spinner.startAnimating()
self.loadWeb()
// Do any additional setup after loading the view.
}
Then webViewDidFinishLoad detected the loading status of the page, hid and stopped the chrysanthemum rotation immediately after the loading was completed, and the code was as follows:
func webViewDidFinishLoad(_ webView: UIWebView) {
print("web load finish")
self.spinner.stopAnimating()
}
2. Add jump to Safari
When initialized, toSafari, the function that opens the browser, is bound to it, and opening the browser is simple:
@objc func toSafari(){
print("to safari")
if let url = NSURL(string:self.url) {
UIApplication.shared.open(url as URL, options: [:], completionHandler: nil)
}
}
3. Add the ability to copy links
toCopy, the method of copying webpage information, we can copy the title and link at the same time, the code is as follows:
@objc func toCopy(){
print("to copy")
// In just two words, it comes true
let paste = UIPasteboard.general
let str = self.urlTitle+":"+self.url
print(str)
paste.string = str
let alertController = UIAlertController(title: NSLocalizedString("Copy Success!",comment: "Copy Success!"),message: nil,preferredStyle: .alert)
// Display prompt box
self.present(alertController, animated: true, completion: nil)
// It will automatically disappear after two seconds
DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 2) {
self.presentedViewController?.dismiss(animated: false, completion: nil)
}
}
4. Add the ability to share web pages
toMore, the method of sharing web pages, adopts a similar way to sharing application. Here is the content of the web page, such as the title, link and header diagram of the web page. The code is as follows:
@objc func toMore(){
print("to more")
let shareVC:UIActivityViewController = UIActivityViewController(activityItems: [self.urlTitle,self.urlImage,self.url], applicationActivities: nil)
self.present(shareVC, animated: true, completion: {
print("more success")
})
}
Isn't it easy to have one feature and three functions? Of course you need to add another close button.
conclusion