IPhone app: Avoid the white screen after the splash screen. Allow the splash screen to linger, hide it after loading UIWebview? Screensaver not hiding properly

Our goal is simple for an iPhone application: show a pop-up page, then hide it when UIWebview is ready to show its page.

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

Unfortunately, the screen saver will not disappear after we close it. The default splash screen remains visible, hiding the UIWebview below it.

We understand that this may violate Apple’s principles.

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

We are 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 

Error Curret ViewController.m:

 // // 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 
+4
source share
3 answers

Here is a way to achieve this, first of all, get rid of all the code in AppDelegate. In the root view controller, add an instance variable of the UIImageView class called a “splash”.

Now in 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 method / block callback complete webView loading

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

therefore, dispatch_once ensures that the method will be executed once and only once during the life of the application.

EDIT:

To get a 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]; }); } 
+3
source

Default.png is used by default as a screensaver, and then automatically deleted. But you create another instance and put it in the view yourself. Delete this piece 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:

0
source

[First ViewController.m]

The reason the screen saver does not disappear is because the ViewDidLoad in the ViewController is called before the screen saver is added to the screen window.

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

[Second ViewController.m]

You probably won’t want to add a screensaver for two reasons: 1) in ViewWillAppear, most likely, it has not yet been created; and 2) ViewWillAppear: you can call several times, which you may not need.

[Work way]

A suitable approach (what I use) is a combination of your first and second trials. I mean

  • Add a screensaver to the AppDelegate window;
  • Do some background tasks to get the data (for example, or download a web view);
  • when the task is completed, publish a notification. The screensaver registered in TaskDoneNotification will process the necessary cleanup and remove it from its supervisor.

Done

0
source

All Articles