MoathOthman / MOLH

Localization helper for iOS apps mainly focusing on the LTR/RTL issue
MIT License
117 stars 36 forks source link

RootViewController is return with nil value and do not restart the app correctly #41

Closed fo24 closed 4 years ago

fo24 commented 4 years ago

Thread 1: Fatal error: Unexpectedly found nil while unwrapping an Optional value

MoathOthman commented 4 years ago

@fo24 can you provide more info, e.g. which xcode version , and ios , are you using scene delegate because i have not tried it there yet ?

fo24 commented 4 years ago

@MoathOthman Many thanks for reply. i work with Xcode11 and swift5 And this is my AppDelegate `import UIKit import MOLH

@UIApplicationMain class AppDelegate: UIResponder, UIApplicationDelegate ,MOLHResetable { var window: UIWindow? func reset() { let rootviewcontroller: UIWindow = ((UIApplication.shared.delegate?.window)!)! let stry = UIStoryboard(name: "Main", bundle: nil) rootviewcontroller.rootViewController = stry.instantiateViewController(withIdentifier: "rootnav") }

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
    MOLH.shared.activate(true)

    return true
}`
MoathOthman commented 4 years ago

@fo24 are you using the demo project? i think you might have forgotten to set the initial view controller in the main storyboard or something like that.

fo24 commented 4 years ago

Main storyboard 2019-10-09 10-26-42 @MoathOthman I already make it but in app delegate i added var window: UIwindow? manually

MoathOthman commented 4 years ago

@fo24 I will try to start the project with xcode11 and see what is the issue, if you could solve it as well before, please let us know here ! thnx

fo24 commented 4 years ago

@MoathOthman i already uploaded the project in github i will invite you.

MoathOthman commented 4 years ago

@fo24 This behavior occurs because with the new scene delegate, since now on ipados an app can have multiple scenes and hence multiple rootviewcontroller so there is no shared uiapplication that has a single rootviewcontroller anymore. if you are target ios 12 or you are not supporting iPadOS or you don't want to support multiple windows , then you can remove all the scene delegates code and you should be fine .. i pushed the code in a new branch on ur repo. This is causing an issue in the library as well and i will try to fix it soon. thnx

fo24 commented 4 years ago

Many thnx i will close the issue.

MoathOthman commented 4 years ago

your welcome :)

Emy87aa commented 4 years ago

Did you solved it, because i'm facing the same problem now.

supergenedy commented 4 years ago

I made a solution for this issue till MoathOthman updates the lib you can add this func in your AppDelegate it gets the window from SceneDelegate

 @available(iOS 13.0, *)
    func swichRoot(){
        //Flip Animation before changing rootView
        animateView()

        // switch root view controllers
        let storyboard = UIStoryboard.init(name: "Main", bundle: nil)
        let nav = storyboard.instantiateViewController(withIdentifier: "HomeView")

        let scene = UIApplication.shared.connectedScenes.first
        if let sd : SceneDelegate = (scene?.delegate as? SceneDelegate) {
            sd.window!.rootViewController = nav
        }

    }
   @available(iOS 13.0, *)
    func animateView() {
        var transition = UIView.AnimationOptions.transitionFlipFromRight
        if !MOLHLanguage.isRTLLanguage() {
            transition = .transitionFlipFromLeft
        }
       animateView(transition: transition)
    }

    @available(iOS 13.0, *)
    func animateView(transition: UIView.AnimationOptions) {
        if let delegate = UIApplication.shared.connectedScenes.first?.delegate {
            UIView.transition(with: (((delegate as? SceneDelegate)!.window)!), duration: 0.5, options: transition, animations: {}) { (f) in
            }
        }
    }

You can check if ios > 13 then call this func from the AppDelegate

        if #available(iOS 13.0, *) {
                let delegate = UIApplication.shared.delegate as? AppDelegate
                delegate!.swichRoot()
        } else {
               // Fallback on earlier versions
               MOLH.reset()
        }

And also check if ios > 13 in the reset func in the AppDelegate to call swichRoot func