本文将演示如何使用应用程序单例对象,打开指定的网页。
在项目导航区,打开视图控制器的代码文件【ViewController.swift】
1 import UIKit 2 3 class ViewController: UIViewController { 4 5 override func viewDidLoad() { 6 super.viewDidLoad() 7 // Do any additional setup after loading the view, typically from a nib. 8 9 //创建一个字符串存储一个网络地址 10 let website = "https://www.cnblogs.com/strengthen/" 11 //将字符串转换为网址对象 12 let url = URL(string:website) 13 //获取应用程序单例对象, 14 //使用它打开网页的功能, 15 //打开指定网址的网页。 16 UIApplication.shared.openURL(url!) 17 } 18 19 override func didReceiveMemoryWarning() { 20 super.didReceiveMemoryWarning() 21 // Dispose of any resources that can be recreated. 22 } 23 }