使用applicationwillenterforeground获取密码屏幕 [英] Using applicationwillenterforeground for a passcode screen

查看:201
本文介绍了使用applicationwillenterforeground获取密码屏幕的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在iOS4之前,我的应用程序的初始视图控制器将检查viewWillAppear中的密码开/关设置变量,如果设置为on,则会显示一个模态密码屏幕,该屏幕将保持不变,直到输入正确的密码或按下Home按钮。 / p>

使用iOS4,如果我的应用程序已经在后台,我希望用户感到很舒服,如果他们手上的应用程序中包含的数据不容易访问他们的手机给某人使用。



由于应用程序可以返回任何屏幕(应用程序最后一次显示的屏幕),我想我会全部使用UIApplicationWillEnterForegroundNotification具有本地选择器的位置(重复的enterPasscode方法),每个都有基于本地屏幕推送的正确视图控制器,但必须有更好的方法。



I可能在这里有一个轻微的概念误解。有人可以提出另一种方法,或者将我推到下一步。我可以将此作为共享方法,但仍然知道要推送正确的本地视图控制器吗?



谢谢



编辑/更新:



简短版本:它可以工作,但可能仍然有更好的方法(任何帮助赞赏)...



我创建了一个带视图控制器的标准单例。



PasscodeView.h包含:

  UIViewController * viewController; 
@property(非原子,保留)UIViewController * viewController;

PasscodeView.m包含:

  @synthesize viewController; 

我把它放在AppDelegate中:

   - (void)applicationWillEnterForeground:(UIApplication *)application {

PasscodeView * passcodeView = [PasscodeView sharedDataManager];
UIViewController * currentController = [passcodeView viewController];
NSLog(@%@,currentController); //测试
EnterPasscode * passcodeInput = [[EnterPasscode alloc] initWithNibName:@Passcodebundle:nil];
[currentController presentModalViewController:passcodeInput animated:NO];
[passcodeInput release];
}

以及我所有viewDidLoad中的以下内容,更新当前视图控制器进入每个屏幕(只有2行,但似乎还有更好的方法):

  PasscodeView * passcodeView = [PasscodeView sharedSingleton]; 
passcodeView.viewController = [[self navigationController] visibleViewController];

我希望有一种方法可以从applicationWillEnterForeground获取当前的视图控制器,但我找不到它 - 这里的任何帮助仍然赞赏。



为了保持一致性,我更改了一行并添加了一行以获得导航栏以匹配应用程序的其余部分并包含标题。

  UINavigationController * passcodeNavigationController = [[UINavigationController alloc] initWithRootViewController:passcodeInput]; 
[currentController presentModalViewController:passcodeNavigationController animated:NO];


解决方案

您可以通过实施将此行为集中在您的应用委托中

   - (void)applicationWillEnterForeground:(UIApplication *)application; 

您可以实现存储当前适当的模态视图控制器的单例,并在viewWillAppear中更新每个模态视图控制器您的视图控制器。



编辑:我假设您已经有一系列想要显示的视图控制器。我怀疑你确实需要它。如果您有一个名为PasscodeInputController,那么您的applicationWillEnterForeground将如下所示:

   - (void)applicationWillEnterForeground :( UIApplication * )application {
UIViewController * currentController = [window rootViewController];
PasscodeInputController * passcodeInput = [[PasscodeInputController alloc] init];

[currentController presentModalViewController:passcodeInput animated:NO];
[passcodeInput release];
}

我希望能更直接地解决你的问题。


Before iOS4, my app's initial view controller would check a passcode on/off settings variable in viewWillAppear and if set on, present a modal passcode screen that would stay there until the correct passcode was entered or the Home button was pressed.

With iOS4, if my app has been in the background, I would like the user to feel comfortable that the data contained within the app is not easily accessible if they were to hand their phone to someone to use.

Since the app could return to any screen (the screen that the app was last on), I figured I would use the UIApplicationWillEnterForegroundNotification all over the place with local selectors (duplicate enterPasscode methods), each having the correct view controller to push based on the local screen, but there has to be a better way.

I may be having a slight conceptual misunderstanding here. Can someone suggest another approach or nudge me along to the next step. Can I have this as a shared method but still know the correct local view controller to push?

Thanks

EDIT/UPDATE:

Short version: It works, but may there still may be a better way (any help appreciated)...

I created a standard singleton with a view controller.

PasscodeView.h containing:

UIViewController *viewController;
@property (nonatomic, retain) UIViewController *viewController;

PasscodeView.m containing:

@synthesize viewController;

I put this in the AppDelegate:

-(void)applicationWillEnterForeground:(UIApplication*)application {

    PasscodeView *passcodeView = [PasscodeView sharedDataManager];
    UIViewController *currentController =  [passcodeView viewController];
    NSLog(@"%@", currentController);  //testing
    EnterPasscode *passcodeInput = [[EnterPasscode alloc] initWithNibName:@"Passcode" bundle:nil];
    [currentController presentModalViewController:passcodeInput animated:NO];
    [passcodeInput release];
}

and the following in all my viewDidLoad, updating the current view controller as I went into each screen (only 2 lines but still seems that there's a better way):

PasscodeView *passcodeView = [PasscodeView sharedSingleton];
passcodeView.viewController = [[self navigationController] visibleViewController];

I wish there were a way to have gotten the current view controller from applicationWillEnterForeground but I couldn't find it - any help here still appreciated.

For consistency, I changed a line and added a line to get a nav bar to match the rest of the app and to include a title.

UINavigationController *passcodeNavigationController = [[UINavigationController alloc] initWithRootViewController:passcodeInput];
[currentController presentModalViewController: passcodeNavigationController animated:NO];

解决方案

You can centralize this behavior in your app delegate by implementing

-(void)applicationWillEnterForeground:(UIApplication*)application;

You might implement a singleton that stores the currently appropriate modal view controller, updating it in viewWillAppear in each of your view controllers.

Edit: I was assuming that you already had a series of view controllers that you wanted to show. I doubt you actually need it. If you have one called, say PasscodeInputController, then your applicationWillEnterForeground would look something like:

-(void)applicationWillEnterForeground:(UIApplication*)application {
    UIViewController *currentController =  [window rootViewController];
    PasscodeInputController *passcodeInput = [[PasscodeInputController alloc] init];

    [currentController presentModalViewController:passcodeInput animated:NO];
    [passcodeInput release];
}

I hope that addresses your question more directly.

这篇关于使用applicationwillenterforeground获取密码屏幕的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆