【问题标题】:How to block confidential information with Launch Screen or Image in Swift 4? [duplicate]如何在 Swift 4 中使用 Launch Screen 或 Image 阻止机密信息? [复制]
【发布时间】:2018-03-27 19:05:08
【问题描述】:

如何通过在应用移至后台时使用启动图像阻止视图来隐藏应用内的机密信息?我正在使用 Swift 和 iOS 11 创建应用程序。

以下是其他人在旧版本 Swift 上的 App Delegate 中使用的东西,但我不明白为什么 if 可以在当前版本上工作:

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

【问题讨论】:

    标签: ios iphone swift xcode


    【解决方案1】:

    目前你的代码在 Objective-c 而不是 swift 中,你可以在 swift 中试试这个

    func applicationDidEnterBackground(_ application: UIApplication) {
    
        let im = UIImageView()
    
        im.tag = 12
    
        im.frame = (self.window?.frame)!
    
        im.image = UIImage.init(named: "add.png")
    
        application.keyWindow?.subviews.last?.addSubview(im)               
    }
    
    func applicationWillEnterForeground(_ application: UIApplication) {
    
        if let sub = application.keyWindow?.subviews.last
        {
            for vv in sub.subviews
            {
                if(vv.tag == 12)
                {
                    vv.removeFromSuperview()
                }
    
            }
    
        }
    
    }
    

    【讨论】:

    • xcode 是否可以根据 iPhone 型号选择要使用的图像? (较小手机的较小图像...等)
    • @Daniel 使用您可以在 Assests.xcassests 中为图像放置 1x、2x、3x
    【解决方案2】:

    我已经使用以下代码更新了applicationDidEnterBackgroundapplicationWillEnterForeground,并且成功了:

    applicationDidEnterBackground:

    func applicationDidEnterBackground(_ application: UIApplication) {
        let imageView: UIImageView = UIImageView.init(frame: self.window!.bounds)
        imageView.tag = 101
        imageView.image = UIImage(named: "Default.png")
        UIApplication.shared.keyWindow?.subviews.last?.addSubview(imageView)
    }
    

    applicationWillEnterForeground:

    func applicationWillEnterForeground(_ application: UIApplication) {
        if let subviews = UIApplication.shared.keyWindow?.subviews.last?.subviews {
            for view in subviews {
                if view.tag == 101 {
                    view.removeFromSuperview()
                }
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2023-01-30
      • 1970-01-01
      • 1970-01-01
      • 2018-12-02
      • 1970-01-01
      • 2014-08-19
      • 1970-01-01
      • 2015-05-29
      • 2021-10-26
      相关资源
      最近更新 更多