35

我的应用程序中有机密信息,所以当应用程序即将移至后台时,我想用闪屏隐藏它们。

我确实在 iOS6 及更高版本上运行该应用程序。

我试图在其中显示视图,applicationWillResignActive但问题是即使用户滑动控制面板,它也会显示启动画面。我希望它仅在应用程序移至后台时显示。

我试图显示我的splashScreen,applicationDidEnterBackground但它在之前需要screenShot,因此在动画期间恢复时会显示信息。

这里是我想要的精神:

- (void)applicationDidEnterBackground:(UIApplication *)application {
    [_window addSubview:__splashController.view];
}
4

8 回答 8

41

我认为问题在于您正在模拟器中进行测试。在设备上,它应该可以正常工作。

我对此进行了测试,并且有效。当应用程序进入后台时,添加带有初始图像的图像视图 -

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

        UIImageView *imageView = [[UIImageView alloc] initWithFrame:self.window.bounds];

        imageView.tag = 101;    // Give some decent tagvalue or keep a reference of imageView in self
    //    imageView.backgroundColor = [UIColor redColor];
        [imageView setImage:[UIImage imageNamed:@"Default.png"]];   // assuming Default.png is your splash image's name

        [UIApplication.sharedApplication.keyWindow.subviews.lastObject addSubview:imageView];
}

当应用程序回到前台时 -

- (void)applicationWillEnterForeground:(UIApplication *)application
{
    UIImageView *imageView = (UIImageView *)[UIApplication.sharedApplication.keyWindow.subviews.lastObject viewWithTag:101];   // search by the same tag value
    [imageView removeFromSuperview];

}

注意 - 在模拟器(iOS 7.0)上,当您按两次主页按钮(Cmd + H)进行检查时,不会显示添加的子视图,但在设备上它可以按预期工作(例如paypalBofA应用程序)

编辑:(附加信息)

除了如上所述通过添加子视图/模糊来隐藏/替换敏感信息之外,iOS 7ignoreSnapshotOnNextApplicationLaunch还为您提供了通过UIApplicationinsideapplicationWillResignActiveapplicationDidEnterBackground.

UIApplication.h

// Indicate the application should not use the snapshot on next launch, even if there is a valid state restoration archive.
// This should only be called from methods invoked from State Preservation, else it is ignored.
- (void)ignoreSnapshotOnNextApplicationLaunch NS_AVAILABLE_IOS(7_0);

此外,allowScreenShot可以在Restrictions Payload中探索标志。

于 2013-11-05T16:03:12.220 回答
10

Swift 3.0 回答那些懒得翻译的人。

func applicationDidEnterBackground(_ application: UIApplication) {

    let imageView = UIImageView(frame: self.window!.bounds)
    imageView.tag = 101
    imageView.image = ...

    UIApplication.shared.keyWindow?.subviews.last?.addSubview(imageView)
 }

func applicationWillEnterForeground(_ application: UIApplication) {

    if let imageView : UIImageView = UIApplication.shared.keyWindow?.subviews.last?.viewWithTag(101) as? UIImageView {
        imageView.removeFromSuperview()
    }

}
于 2017-02-28T12:50:51.113 回答
4

有同样的问题,基本上我是applicationDidEnterBackground用来在内容顶部显示一个 UIWindow 的,但是在 iOS8 中它不像在 iOS7 中那样工作。

我找到的解决方案是在其中创建 UIWindowapplicationWillResignActive但将其隐藏 securityWindow.hidden = YES;然后applicationDidEnterBackground我要做的就是改变securityWindow.hidden = NO

这似乎与 iOS7 在使用 NotificationCenter 或 ControlPanel 时在多任务处理时模糊内容而不影响视图一样工作。

于 2014-11-11T10:18:59.233 回答
3

不知道为什么,但这里描述的方法都不适合我。出于安全原因,我只是想遮住屏幕。因此,帮助的是来自 Apple 的技术问答:https ://developer.apple.com/library/ios/qa/qa1838/_index.html

我想主要的区别是使用 UIViewController?无论如何,以下代码在 IOS 9.1 上非常适合我:

- (void)applicationDidEnterBackground:(UIApplication *)application
{     
    UIViewController *blankViewController = [UIViewController new];
    blankViewController.view.backgroundColor = [UIColor blackColor];

    [self.window.rootViewController presentViewController:blankViewController animated:NO completion:NULL];
}

- (void)applicationWillEnterForeground:(UIApplication *)application
{
    [self.window.rootViewController dismissViewControllerAnimated:NO completion:NO];
}
于 2015-12-15T09:37:06.623 回答
2

需要编写如下代码:

-(void)applicationWillResignActive:(UIApplication *)application
{
    imageView = [[UIImageView alloc]initWithFrame:[self.window frame]];
    [imageView setImage:[UIImage imageNamed:@"Portrait(768x1024).png"]];
    [self.window addSubview:imageView];
}

这里要删除imageview:

- (void)applicationDidBecomeActive:(UIApplication *)application
{
    if(imageView != nil) {
        [imageView removeFromSuperview];
        imageView = nil;
    }
}

它正在工作并经过适当的测试。

于 2014-10-29T11:34:34.110 回答
1

我认为这将有助于Swift 3.0

func applicationWillResignActive(_ application: UIApplication) {

        let imageView = UIImageView(frame: self.window!.bounds)
        imageView.tag = 101
        imageView.backgroundColor = UIColor.white
        imageView.contentMode = .center
        imageView.image = #image#
        UIApplication.shared.keyWindow?.subviews.last?.addSubview(imageView)

    }

func applicationWillEnterForeground(_ application: UIApplication) {
        ReachabilityManager.shared.stopMonitoring()
        if let imageView : UIImageView = UIApplication.shared.keyWindow?.subviews.last?.viewWithTag(101) as? UIImageView {
            imageView.removeFromSuperview()
        }
    }
于 2017-03-28T10:05:43.547 回答
0
@interface MyAppDelegate ()
@property (strong, nonatomic) MySplashView *splashView;
@end
@implementation MyAppDelegate
- (void)applicationWillResignActive:(UIApplication *)application {
    // hide keyboard and show a splash view
    [self.window endEditing:YES];
    MySplashView *splashView = [[MySplashView alloc] initWithFrame:self.window.bounds];
    [self.window addSubview:splashView];
    self.splashView = splashView;
}
- (void)applicationDidBecomeActive:(UIApplication *)application {
    // remove the splash view
    if (self.splashView) {
        [self.splashView removeFromSuperview];
        self.splashView = nil;
    }
}
@end
于 2014-06-11T09:20:41.390 回答
0

这为我修复了它,抱歉,这是为 Xamarin.Forms 准备的,但你应该明白这一点。您需要在 xamarin 或UIView snapshotViewAfterScreenUpdatesiOS 上调用 UIView.SnapshotView(true)。在 iOS7 和 iOS8 上的 DidEnterBackground 中工作:

public override void DidEnterBackground(UIApplication uiApplication)
{
    App.Current.MainPage = new DefaultPage();
    **UIApplication.SharedApplication.KeyWindow.SnapshotView(true);**
    base.DidEnterBackground(uiApplication);
}
于 2015-09-17T01:33:47.363 回答