How to “preload” a view controller without clicking it?

Background: I have a free app that supports ads. The main idea is that when the application starts, the content (HTML) is downloaded from the server in the background, and only if the download is successful, the content is displayed.

Previous solution: I successfully implemented this in a universal application. I originally uploaded a variable NSDatawith a url, and if it was successful, I presented a modal view containing UIWebView(the corresponding code in another question ). The problem is that external originals (images) are not loaded into the original request NSData, so in brilliant ingenious thinking I thought about something else:

New solution: A new way, and this is what I need help with, is that I create an instance of the modal controller (without showing it yet) and the web view will start loading the URL directly. When the method is called webViewDidFinishLoad, I fire the notification back to the parent object, which then executes presentModalViewController.

Here are the methods that instantiate the view manager and present it modally:

- (void)launchAd
{
    if ([ServerCheck serverReachable:@"openx.freewave-wifi.com" hideAlert:YES])
    {
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(presentAdModal) name:@"AdLoaded" object:nil];

        AdViewController *adView = [[AdViewController alloc] initWithNibName:nil bundle:nil];
        [[adView view] awakeFromNib]; //THIS LINE IS WHAT THIS QUESTION IS ABOUT

        navController = [[UINavigationController alloc] initWithRootViewController:adView];
        [adView release], adView = nil;

        [navController setModalPresentationStyle:UIModalPresentationFormSheet];
        [navController setModalTransitionStyle:UIModalTransitionStyleCoverVertical];
        [[navController navigationBar] setTintColor:[UIColor colorWithRed:0.94 green:0.00 blue:0.32 alpha:1.00]];
    }
    else
        LogError(@"Not presenting ad.");
}

- (void)presentAdModal
{
    LogInfo(@"Presenting advertisement modal.");

    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"AdLoaded" object:nil];

    [tabBarController presentModalViewController:navController animated:YES];

    [navController release];
}

And in the AdView controller:

- (void)webViewDidFinishLoad:(UIWebView *)theWebView
{
    LogInfo(@"Ad content did load; we can show it.");

    [timeout invalidate], timeout = nil;

    [self setTitle:@"From Our Sponsor"];

    [[NSNotificationCenter defaultCenter] postNotificationName:@"AdLoaded" object:nil];
}

Now what works above . However, it took me a long time to figure out how to invoke the delegation methods of the view controller in the controller AdViewto run before the call presentModalViewController. I did this by adding [[adView view] awakeFromNib];, but I know that this is the wrong way to do this.

So, all of the above works fine, effectively having a view controller and preloading it UIWebViewbefore displaying it. I just want to know what I should do instead [[adView view] awakeFromNib];- I want to do it right.

Or is that right? I think some people will say, “If it works, then it’s right,” but I know that it’s not true.

+3

Source: https://habr.com/ru/post/1796064/


All Articles