有时我们需要在应用中跳转至系统设置界面,开启Wifi或是打开定位功能,可以通过url完成这样的跳转功能,下面给出Swift的代码。
class ViewController: UIViewController {
var button: UIButton! //声明触发跳转的button
override func viewDidLoad() {
super.viewDidLoad()
self.button = UIButton(frame: CGRect(x: 100, y: 100, width: 120, height: 80))
self.button.setTitle("Open Setting", forState: UIControlState.Normal)
self.button.setTitleColor(UIColor.blackColor(), forState: UIControlState.Normal)
self.view.addSubview(self.button)
self.button.addTarget(self, action: "openSetting", forControlEvents: UIControlEvents.TouchUpInside)
}
// 完成跳转的具体方法
func openSetting() {
var url: NSURL = NSURL(string: UIApplicationOpenSettingsURLString)!
if UIApplication.sharedApplication().canOpenURL(url) {
UIApplication.sharedApplication().openURL(url)
}
}
}