我有问题。更改语言后,我想重新启动我的应用程序。
所以我想收到一条警报消息,其中包含文本“您想重新启动应用程序以更改语言吗?”“是”“否”
如果用户按“是”,我如何重新启动应用程序?
我的解决方案:
let alertController = UIAlertController(title: "Language".localized(), message: "To changing language you need to restart application, do you want to restart?".localized(), preferredStyle: .alert)
let okAction = UIAlertAction(title: "Yes".localized(), style: UIAlertActionStyle.default) {
UIAlertAction in
NSLog("OK Pressed")
exit(0)
}
let cancelAction = UIAlertAction(title: "Restart later".localized(), style: UIAlertActionStyle.cancel) {
UIAlertAction in
NSLog("Cancel Pressed")
}
alertController.addAction(okAction)
alertController.addAction(cancelAction)
self.present(alertController, animated: true, completion: nil)
应用程序关闭后,用户将手动启动应用程序。
您无法重新启动 iOS 应用程序。你可以做的一件事就是弹出到你的 rootViewController。
func restartApplication () {
let viewController = LaunchScreenViewController()
let navCtrl = UINavigationController(rootViewController: viewController)
guard
let window = UIApplication.shared.keyWindow,
let rootViewController = window.rootViewController
else {
return
}
navCtrl.view.frame = rootViewController.view.frame
navCtrl.view.layoutIfNeeded()
UIView.transition(with: window, duration: 0.3, options: .transitionCrossDissolve, animations: {
window.rootViewController = navCtrl
})
}
在我的一个应用程序中,我需要重新启动。我将所有加载逻辑包装到 LaunchScreenViewController 中。上面是“重新启动应用程序”的代码。
它并不完美,但我通过在退出应用程序之前发送定时本地通知解决了这个问题。然后,用户只需单击通知即可重新启动应用程序,因此他们不需要寻找应用程序来重新启动它。我还建议显示一条警报,通知用户重新启动:
import UserNotifications
import Darwin // needed for exit(0)
struct RestartAppView: View {
@State private var showConfirm = false
var body: some View {
VStack {
Button(action: {
self.showConfirm = true
}) {
Text("Update Configuration")
}
}.alert(isPresented: $showConfirm, content: { confirmChange })
}
var confirmChange: Alert {
Alert(title: Text("Change Configuration?"), message: Text("This application needs to restart to update the configuration.\n\nDo you want to restart the application?"),
primaryButton: .default (Text("Yes")) {
restartApplication()
},
secondaryButton: .cancel(Text("No"))
)
}
func restartApplication(){
var localUserInfo: [AnyHashable : Any] = [:]
localUserInfo["pushType"] = "restart"
let content = UNMutableNotificationContent()
content.title = "Configuration Update Complete"
content.body = "Tap to reopen the application"
content.sound = UNNotificationSound.default
content.userInfo = localUserInfo
let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 0.5, repeats: false)
let identifier = "com.domain.restart"
let request = UNNotificationRequest.init(identifier: identifier, content: content, trigger: trigger)
let center = UNUserNotificationCenter.current()
center.add(request)
exit(0)
}
您可以更改根控制器,无需重新启动它。只需更改根视图控制器或更新或刷新或调用根视图控制器即可:
let alertController = UIAlertController(title: "Language".localized(), message: "To change language you need to restart the application. Do you want to restart?".localized(), preferredStyle: .alert)
let okAction = UIAlertAction(title: "Yes".localized(), style: UIAlertActionStyle.default) {
UIAlertAction in
// Change update / refresh rootview controller here...
}
您可以添加到AppDelegate
func resetApp() {
UIApplication.shared.windows[0].rootViewController = UIStoryboard(
name: "Main",
bundle: nil
).instantiateInitialViewController()
}
在你想要的地方调用这个函数
let appDelegate = AppDelegate()
appDelegate.startWith()
将其添加到 viewDidLoad 中以启动视图控制器(VC):
override func viewDidLoad() {
super.viewDidLoad()
// Make dismiss for all VC that was presented from this start VC
self.children.forEach({vc in
print("Dismiss \(vc.description)")
vc.dismiss(animated: false, completion: nil)
})
// ....
}
并在重启启动器中:
// ...
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewController(withIdentifier: "startVC")
self.present(vc, animated: false, completion: nil)