Search code examples
iosstatic-librariesuiwindow

iOS: Can a library in Objective C produce a pop up window and manage an interaction?


I want to make a class in a library that will, upon request, do a complicated interaction with the user. The app that initializes the library will not provide any info like its UIWindow or anything. Is this technically possible? I know I can do a simple popup but what about something that needs to be constructed on the fly? Thanks.


Solution

  • Use UIViewController for any complex user interaction. In order to present a view controller modally, you'll have to find a suitable parent view controller. You can get access to the app's root view controller using the -rootViewController method of UIWindow like that:

    NSArray *windows = [[UIApplication sharedApplication] windows];
    UIViewController *rootViewController = (windows.count > 0) ? [[windows objectAtIndex:0] rootViewController] : nil;
    if (rootViewController)
        // Present your view controller modally
    else
        // Unable to present any view controller
    

    You can also provide a delegate method and use this to get a view controller:

    - (UIViewController *) rootViewControllerForComplexClass:(id)complexClass;  
    

    In your library you have access to the delegate and access the view controller like that:

    UIViewController *rootViewController = [self.delegate rootViewControllerForComplexClass:self];
    // Present your view controller modally