Crashalot
Crashalot

Reputation: 34503

iPhone app: avoiding white screen after splash screen. Let splash screen linger, hide it after UIWebview loads? Splash screen not hiding properly

Our goal is simple for an iPhone app: show a splash page then hide it when a UIWebview is ready to show its page.

We need the default splash screen to linger until the UIWebview is ready to display. Otherwise, a white screen appears briefly.

Unfortunately, the splash screen fails to hide after we make it linger. The default splash screen remains visible, concealing the UIWebview underneath.

We understand this may violate Apple guidelines.

This is more for a prototype than anything, and we would like to understand what we're doing wrong. Any clues?

We're using Xcode 4.2.

AppDelegate.m:

//
//  AppDelegate.m
//
//  Created by Macintosh User on 6/4/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import "AppDelegate.h"

#import "ViewController.h"

@implementation AppDelegate

@synthesize window = _window;
@synthesize viewController = _viewController;
@synthesize imgv;

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
    self.viewController = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil];
    self.window.rootViewController = self.viewController;
    [self.window makeKeyAndVisible];

    imgv = [[UIImageView alloc] init];
    [imgv setImage:[UIImage imageNamed:@"Default.png"]];
    [imgv setFrame:CGRectMake(0, 0, 320, 480)];
    [self.window addSubview:imgv];

    return YES;
}

@end

ViewController.m:

//
//  ViewController.m
// 
//
//  Created by Macintosh User on 6/4/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import "AppDelegate.h"
#import "ViewController.h"

@implementation ViewController

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    CGRect webFrame = CGRectMake(0.0, 0.0, 320.0, 460.0);
    UIWebView *webView = [[UIWebView alloc] initWithFrame:webFrame];
    [webView setBackgroundColor:[UIColor clearColor]];
    NSString *urlAddress = @"http://www.cnn.com";
    NSURL *url = [NSURL URLWithString:urlAddress];
    NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
    [webView loadRequest:requestObj];

    for (id subview in webView.subviews)
        if ([[subview class] isSubclassOfClass: [UIScrollView class]])
            ((UIScrollView *)subview).bounces = NO;

    AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    UIImageView *imageView = appDelegate.imgv;
    [imageView removeFromSuperview];
    [imageView setHidden:YES];
    imageView = nil;

    [self.view addSubview:webView];
    [self.view bringSubviewToFront:webView];
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    NSLog(@"Done loading UIWebView");
}

@end

Curret ViewController.m generating errors:

//
//  ViewController.m
//  Stroll
//
//  Created by Macintosh User on 6/4/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import "AppDelegate.h"
#import "ViewController.h"

@implementation ViewController

@synthesize splash;

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    CGRect webFrame = CGRectMake(0.0, 0.0, 320.0, 460.0);
    UIWebView *webView = [[UIWebView alloc] initWithFrame:webFrame];

    webView.delegate = self;

    [webView setBackgroundColor:[UIColor clearColor]];
    NSString *urlAddress = @"http://www.cnn.com";
    NSURL *url = [NSURL URLWithString:urlAddress];
    NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
    [webView loadRequest:requestObj];

    for (id subview in webView.subviews)
        if ([[subview class] isSubclassOfClass: [UIScrollView class]])
            ((UIScrollView *)subview).bounces = NO;

    /*
    AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    UIImageView *imageView = appDelegate.imgv;
    [imageView removeFromSuperview];
    [imageView setHidden:YES];
    imageView = nil; */

    [self.view addSubview:webView];
    [self.view bringSubviewToFront:webView];
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    NSLog(@"Done loading UIWebView");
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];

    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{

        self.view.userInteractionEnabled = NO;

        splash = [[UIImageView alloc] initWithFrame:[UIScreen mainScreen].bounds];

        splash.image = [UIImage imageNamed:@"Default.png"];
        [self.view addSubview:splash];
    });
}

-(void) webViewDidFinishLoad:(UIWebView *)webView {

    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        [splash removeFromSuperView];
    });
}

@end

Upvotes: 4

Views: 6843

Answers (3)

Kaan Dedeoglu
Kaan Dedeoglu

Reputation: 14841

Here's a way to achieve it, get rid of all the code in your AppDelegate first of all. In your root view controller add an instance variable of class UIImageView called "splash".

Now in the rootViewController.m:

-(void) viewWillAppear:(BOOL) animated {

    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{

        self.view.userInteractionEnabled = NO;

        splash = [[UIImageView alloc] initWithFrame:[UIScreen mainScreen].bounds];

        splash.image = [UIImage imageNamed:@"Default.png"];
        [self.view addSubview:splash];
    });
    }

Now in your webView load completion callback method/block

static dispatch_once_t onceToken;
        dispatch_once(&onceToken, ^{

          [splash removeFromSuperView];
        });

so the dispatch_once makes sure the method will run once and only once in the life time of the application.

EDIT:

To get your callback:

in viewController.h -> viewC : UIViewController < UIWebViewDelegate >

in viewController.m

-(void) viewDidLoad{

    CGRect webFrame = CGRectMake(0.0, 0.0, 320.0, 460.0);
    UIWebView *webView = [[UIWebView alloc] initWithFrame:webFrame];

    webView.delegate = self;

    [webView setBackgroundColor:[UIColor clearColor]];
    NSString *urlAddress = @"http://www.cnn.com";
    NSURL *url = [NSURL URLWithString:urlAddress];
    NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
    [webView loadRequest:requestObj];
}

then

-(void) webViewDidFinishLoad:(UIWebView *)webView {

    static dispatch_once_t onceToken;
        dispatch_once(&onceToken, ^{

          [splash removeFromSuperView];
        });
}

Upvotes: 3

xingzhi.sg
xingzhi.sg

Reputation: 433

[First ViewController.m]

The reason that the splash screen is not disappearing is because the ViewDidLoad in ViewController is called before the splash screen being added to the screen window.

To be specific, the ViewDidLoad method was called around [self.window makeKeyAndVisible], right before the splash being called.

[Second ViewController.m]

You won't probably want to add splash screen here because of two reasons: 1) at ViewWillAppear, it is likely that the view hasn't been created yet; and 2) ViewWillAppear: can be called several times which you might not really want.

[The workable way]

A workable approach (what I am using) is a combination of your first and second trials. I mean

  1. Add splash screen on to the window in the AppDelegate;
  2. Do some background tasks to fetch data (for example, or load a webview);
  3. when the task is done, post a notification. The splash screen which registered to the TaskDoneNotification will process necessary cleanup and remove itself from its superview.

Done

Upvotes: 0

Morion
Morion

Reputation: 10860

Default.png use as splashscreen by default, then removes automatically. But you create another instance and place it to the view by yourself. Remove this part of code

imgv = [[UIImageView alloc] init];
[imgv setImage:[UIImage imageNamed:@"Default.png"]];
[imgv setFrame:CGRectMake(0, 0, 320, 480)];
[self.window addSubview:imgv];

from your application:didFinishLaunchingWithOptions: method

Upvotes: 0

Related Questions