简体   繁体   中英

How to instantiate a UIViewController from AppDelegate?

I have the following setup in my app: 故事板设置

There is a container view which holds 3 view controllers (A, B and C). I'm able to switch from A, B or C by swiping left or right. Both A or B contain their own collection view. If I tap on any cell in the collection view inside A or B, the PlayerVC (named Player in the pic above ^^) launches and a video begins to play using AVPlayer .

The problem is:

  • This app is able to share Universal Links so when a user taps on a universal link , it takes them directly to the app, launches the PlayerVC , and begins playing a video. The problem is how do I initialize the other view controllers if the user is taken directly to the PlayerVC if they tap on a universal link ? Or how to get to PlayerVC from app delegate ?

Here are some scenarios:

  • If the user is taken directly to the PlayerVC , then if they dismiss the PlayerVC the app crashes.
  • If the app is already launched but in background mode, let's say the user was in view controller B , then when the user taps on a universal link , how do I go from view controller B to PlayerVC ?

This is the delegate method in the app delegate that gets called when a universal link is tapped by a user (please notice the HELP comment):

class AppDelegate: UIResponder, UIApplicationDelegate {
    // Other App Delegate methods.....

    func application(_ application: UIApplication, continue userActivity: NSUserActivity, restorationHandler: @escaping ([Any]?) -> Void) -> Bool {

        // 1) Make sure the passed `user activity` has expected characteristics.
        guard userActivity.activityType == NSUserActivityTypeBrowsingWeb, let url = userActivity.webpageURL else {
            return false
        }

        // HELP: I need to get to `PlayerVC` from here?

        return true

        // If we can't do the above we default to opening the page in safari
    }
}

Note: Our app is running iOS 10. We are using Apple's new api's for Universal Links: https://developer.apple.com/library/content/documentation/General/Conceptual/AppSearch/UniversalLinks.html

Any thoughts?

The problem is:

This app is able to share Universal Links so when a user taps on a universal link, it takes them directly to the app, launches the PlayerVC, and begins playing a video. The problem is how do I initialize the other view controllers if the user is taken directly to the PlayerVC if they tap on a universal link? Or how to get to PlayerVC from app delegate?

For this problem, you'd need to first instantiate the container and its child view controllers, then you can show the PlayerVC

If the user is taken directly to the PlayerVC, then if they dismiss the PlayerVC the app crashes.

With the implementation that I told you, it shouldn't crash. There is always gonna be a container controller instantiated

If the app is already launched but in background mode, let's say the user was in view controller B, then when the user taps on a universal link, how do I go from view controller B to PlayerVC?

You can store in your AppDelegate a global variable of your container controller. With this you can access to controller B. Then you can call

container.present(playerVC, animated: true, completion: nil)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM