在applicationDidEnterBackground之前显示视图或启动画面(以避免活动视图截图)

时间:2013-11-05 15:42:14

标签: ios background ios7 screenshot uiapplicationdelegate

我的应用程序中有机密信息,所以当应用程序即将移动到后台时,我想用启动画面隐藏它们。

我确实在iOS6上运行应用程序。

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

我试图在applicationDidEnterBackground中显示我的splashScreen,但是在动画播放期间恢复时会显示screenShot。

这就是我想要的精神:

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

8 个答案:

答案 0 :(得分:39)

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

我对此进行了测试并且有效。当应用进入后台时,添加带有启动图像的imageview -

- (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];
}

当app回到前台时 -

- (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)进行检查时,未显示添加的子视图,但在设备上它按预期工作(如paypal,{{1应用程序)

编辑:(其他信息)

除了如上所述通过添加子视图/模糊来模糊/替换敏感信息之外, iOS 7 还可以通过BofA ignoreSnapshotOnNextApplicationLaunch内的UIApplication忽略屏幕快照applicationWillResignActiveapplicationDidEnterBackground

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);

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

答案 1 :(得分:7)

对于那些懒惰翻译的人来说,

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()
    }

}

答案 2 :(得分:4)

有同样的问题,基本上我使用applicationDidEnterBackground在内容之上显示UIWindow,但在iOS8中它没有像在iOS7中那样工作。

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

这似乎与iOS7在使用NotificationCenter或ControlPanel时在不影响视图的情况下模糊内容完全相同。

答案 3 :(得分: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];
}

答案 4 :(得分:1)

需要编写如下代码:

-(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;
    }
}

它正在运行并经过适当测试。

答案 5 :(得分: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

答案 6 :(得分:0)

这为我修好了,抱歉这是Xamarin.Forms,但你应该明白这个想法。您需要在xamarin中调用UIView.SnapshotView(true)或在iOS上调用UIView snapshotViewAfterScreenUpdates。适用于iOS7和iOS8上的DidEnterBackground:

public override void DidEnterBackground(UIApplication uiApplication)
{
    App.Current.MainPage = new DefaultPage();
    **UIApplication.SharedApplication.KeyWindow.SnapshotView(true);**
    base.DidEnterBackground(uiApplication);
}

答案 7 :(得分:0)

我认为这对 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()
        }
    }