如何在iOS上的Swift 4中以编程方式重新启动应用程序?

uxhixvfz  于 11个月前  发布在  iOS
关注(0)|答案(5)|浏览(189)

我遇到问题。语言更改后,我想重新启动应用程序。
所以我想得到一个警告消息与文本“你想重新启动应用程序更改语言?”“”“
如果用户按下YES,我如何重新启动应用程序?
我的解决方案:

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)

字符串
在应用程序关闭后,用户将手动启动应用程序。

dffbzjpn

dffbzjpn1#

你不能重新启动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中。上面是“重新启动应用程序”的代码。

brtdzjyr

brtdzjyr2#

它并不完美,但我通过在退出应用程序之前发送定时本地通知解决了这个问题。然后用户只需要点击通知即可重新启动应用程序,这样他们就不需要寻找应用程序重新启动它。我还建议显示一个提醒,通知用户重新启动:


的数据


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)
}

字符串

iqjalb3h

iqjalb3h3#

您可以更改根控制器,而不需要重新启动它。只需更改根视图控制器或更新或刷新或召回根视图控制器:

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...
}

字符串

e5nqia27

e5nqia274#

您可以添加到AppDelegate

func resetApp() {   
    UIApplication.shared.windows[0].rootViewController = UIStoryboard(
        name: "Main",
        bundle: nil
        ).instantiateInitialViewController()
}

字符串
在需要的地方调用此函数

let appDelegate = AppDelegate()
appDelegate.startWith()

kuarbcqp

kuarbcqp5#

将其添加到viewDidLoad以启动viewcontroller(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)

相关问题