Aplicación para iPhone: evita la pantalla en blanco después de la pantalla de bienvenida. Deje que la pantalla de bienvenida permanezca, esconda después de las cargas UIWebview? La pantalla Splash no se oculta correctamente

Nuestro objective es simple para una aplicación de iPhone: muestre una página de bienvenida y luego oculte cuando una UIWebview esté list para mostrar su página.

Necesitamos que la pantalla de bienvenida pnetworkingeterminada permanezca hasta que UIWebview esté list para mostrarse. De lo contrario, aparecerá brevemente una pantalla en blanco.

Lamentablemente, la pantalla de bienvenida no se esconde después de que lo demoremos. La pantalla de bienvenida pnetworkingeterminada permanece visible, ocultando la UIWebview debajo.

Entendemos que esto puede violar las directrices de Apple.

Esto es más para un prototipo que cualquier otra cosa, y nos gustaría entender lo que estamos haciendo mal. ¿Alguna pista?

Estamos usando 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; } - (void)applicationWillResignActive:(UIApplication *)application { /* Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state. Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game. */ } - (void)applicationDidEnterBackground:(UIApplication *)application { /* Use this method to release shanetworking resources, save user data, invalidate timers, and store enough application state information to restre your application to its current state in case it is terminated later. If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits. */ } - (void)applicationWillEnterForeground:(UIApplication *)application { /* Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background. */ } - (void)applicationDidBecomeActive:(UIApplication *)application { /* Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface. */ } - (void)applicationWillTerminate:(UIApplication *)application { /* Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:. */ } @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 - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; NSLog(@"Received memory warning!"); // Release any cached data, images, etc that aren't in use. } #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 shanetworkingApplication] 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)viewDidUnload { [super viewDidUnload]; // Release any retained subviews of the main view. // eg self.myOutlet = nil; } - (void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; } - (void)viewDidAppear:(BOOL)animated { [super viewDidAppear:animated]; } - (void)viewWillDisappear:(BOOL)animated { [super viewWillDisappear:animated]; } - (void)viewDidDisappear:(BOOL)animated { [super viewDidDisappear:animated]; } - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { // Return YES for supported orientations //return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown); return interfaceOrientation == UIInterfaceOrientationPortrait; } @end 

Curret ViewController.m generando errores:

 // // 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; - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; NSLog(@"Received memory warning!"); // Release any cached data, images, etc that aren't in use. } #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 shanetworkingApplication] 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)viewDidUnload { [super viewDidUnload]; // Release any retained subviews of the main view. // eg self.myOutlet = nil; } - (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]; }); } - (void)viewDidAppear:(BOOL)animated { [super viewDidAppear:animated]; } - (void)viewWillDisappear:(BOOL)animated { [super viewWillDisappear:animated]; } - (void)viewDidDisappear:(BOOL)animated { [super viewDidDisappear:animated]; } - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { // Return YES for supported orientations //return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown); return interfaceOrientation == UIInterfaceOrientationPortrait; } @end 

Aquí hay una manera de lograrlo, deshacerse de todo el código en su AppDelegate antes que nada. En su controller de vista raíz, agregue una variable de instancia de class UIImageView denominada "splash".

Ahora en 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]; }); } 

Ahora en su método de callback de finalización de carga webView / bloque

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

por lo que dispatch_once se asegura de que el método se ejecute una vez y solo una vez en la vida útil de la aplicación. ¡Hazme saber si esto funciona!

EDITAR:

Para get su callback:

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

en viewController.m

– (vacío) 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]; 

}

entonces

 -(void) webViewDidFinishLoad:(UIWebView *)webView { static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ [splash removeFromSuperView]; }); } 

Default.png se usa como pantalla de bienvenida por defecto, luego se elimina automáticamente. Pero creas otra instancia y la colocas en la vista por ti mismo. Eliminar esta parte del código

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

desde su application:didFinishLaunchingWithOptions: method

[First ViewController.m]

La razón por la que la pantalla de presentación no desaparece se debe a que ViewDidLoad en ViewController se invoca antes de agregar la pantalla de bienvenida a la window de la pantalla.

Para ser específico, el método ViewDidLoad se llamó a [self.window makeKeyAndVisible], justo antes de llamar al splash.

[Second ViewController.m]

Probablemente no querrá agregar la pantalla de bienvenida aquí por dos razones: 1) en ViewWillAppear, es probable que la vista aún no se haya creado; y 2) ViewWillAppear: se puede llamar varias veces, que es posible que no desee.

[La forma viable]

Un enfoque factible (lo que estoy usando) es una combinación de su primera y segunda testing. quiero decir

  1. Agregue la pantalla de bienvenida a la window en el AppDelegate;
  2. Realice algunas tareas en segundo plano para get datos (por ejemplo, o cargue una vista web);
  3. Cuando finalice la tarea, publique una notificación. La pantalla de bienvenida que se registró en TaskDoneNotification procesará la limpieza necesaria y se eliminará de su supervisión.

Hecho