NEW: Start my new Ultimate Portfolio App course with a free Hacking with Swift+ trial! >>

How can i restart a SwiftUI application programmatically ?

Forums > SwiftUI

Hi every one,

Please how can i restart a SwiftUI application programmaticlly ?

Thanks.

   

You cant.

   

@twostraws  Site AdminHWS+

Yeah, this is definitely a hard no – you shouldn't attempt to stop or start apps.

   

Hacking with Swift is sponsored by RevenueCat

SPONSORED Building and maintaining in-app subscription infrastructure is hard. Luckily there's a better way. With RevenueCat, you can implement subscriptions for your app in hours, not months, so you can get back to building your app.

Try it for free

Sponsor Hacking with Swift and reach the world's largest Swift community!

I am curious to learn what problem you have that you are trying to solve by restarting your app.

   

@nemecek-filip , @twostraws thanks for your replies. :)

@PointOfNilReturn i appreciate your curiosity MAN :) The problem is about localization. Some one told me to use Application setting url so the user could click on language and then chosing among the available languages. what i see in this solution is making the user to pass by many pages just to change the language and has to click on back button to go to the app. this solution is changing the language on the fly but it's really useless for givving the best user experience. I decided to go by the second way where i change the language setting in the UserDefaults and asking the user to close and start the application to apply changes.

As you can see i just would to restart the application programaticly instead of asking the user to do that manually.

Please if there is a better solution for changing the app language i'll be thankfull for it.

Best regards.

   

I think it was me who suggested the solution. But maybe I wasnt clear. The URL wont just launch the iOS Settings app, it will launch it and navigate to the section with settings for your app. Here user will select new language, tap the back button in top left corner and the app will be re-launched with new language.

I dont think this is asking much from users and I dont think many will decide to change the language anyway.

   

@nemecek-filip yep you're right, it was your solution but believe me in the current project the user will prefer to restart the app manually instead of going throught another pages just to change the language on the fly. The users of the current project are insisting on the easy of use and less navigations, that's why i would to let the app restart automaticly instead of asking the user to do that manually.

Please if there is a better way to change the language on the fly without requesting the restart or going throught other views, it will be the perfect solution for them. Their requirement is changing the language only by a single click of button.

kind regards.

   

In that case you need to manually implement language selection in your app and manually get the correct localization strings based on the selected language.

After user selects new language, you can use something like NotificationCenter.default to post notification and update texts across your app.

   

@nemecek-filip yep i already made 2 buttons for the 2 differents languages and when the user clicks on one i change the settings (in UserDefaults) and request user for restarting the app. I really would like to know how i can implement your suggested solution, please can you give a hintor a link.

Kind rgards.

   

I have no idea how you are setting localization texts in the first place. But you need to do it again after the user switches language. This way you dont have to force the user to restart the app.

   

Hi again dear @nemecek-filip, i'm just puting the below single line in the "English" button action then i give a message to the user for asking him to close and start the application again.

                                UserDefaults.standard.set(["en"], forKey: "AppleLanguages")

Kind regards.

   

Hacking with Swift is sponsored by RevenueCat

SPONSORED Building and maintaining in-app subscription infrastructure is hard. Luckily there's a better way. With RevenueCat, you can implement subscriptions for your app in hours, not months, so you can get back to building your app.

Try it for free

Sponsor Hacking with Swift and reach the world's largest Swift community!

Reply to this topic…

You need to create an account or log in to reply.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.