<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Arik Segal</title>
    <description>The latest articles on DEV Community by Arik Segal (@aharon_segal_ebdb1f69c9a1).</description>
    <link>https://dev.to/aharon_segal_ebdb1f69c9a1</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F985165%2Fe36af2e9-eb82-456b-8644-bc821d4da89a.JPG</url>
      <title>DEV Community: Arik Segal</title>
      <link>https://dev.to/aharon_segal_ebdb1f69c9a1</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/aharon_segal_ebdb1f69c9a1"/>
    <language>en</language>
    <item>
      <title>How to create an iOS app that takes secret photos while the iPhone screen seems to be turned off</title>
      <dc:creator>Arik Segal</dc:creator>
      <pubDate>Tue, 06 Dec 2022 12:57:17 +0000</pubDate>
      <link>https://dev.to/aharon_segal_ebdb1f69c9a1/how-to-create-an-ios-app-that-takes-secret-photos-while-the-iphone-screen-seems-to-be-turned-off-3a0l</link>
      <guid>https://dev.to/aharon_segal_ebdb1f69c9a1/how-to-create-an-ios-app-that-takes-secret-photos-while-the-iphone-screen-seems-to-be-turned-off-3a0l</guid>
      <description>&lt;p&gt;&lt;strong&gt;Problem&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Suppose you want to repeatedly take secret photos of someone, without being caught. What would be the best iPhone application for doing that? Would the built-in Camera app be sufficient?&lt;/p&gt;

&lt;p&gt;There are may things to consider. For start, you can get caught if someone looks over your shoulder and notices that your screen is displaying a camera feed. So, you will need to make sure nobody is behind you when you shoot. Moreover, the tap gesture that is required in order to take a photo is distinguishable from other common gestures (scrolling, texting). A person can guess that you’re taking a photo, just from looking at your hands and body language.&lt;/p&gt;

&lt;p&gt;Another thing to consider is the green camera-in-use indicator. When an iOS application is about to take photos, it must first display a prompt similar to this:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--o96tkQkG--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/63sh5k3pxhczlhksal6y.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--o96tkQkG--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/63sh5k3pxhczlhksal6y.jpeg" alt="Image description" width="300" height="328"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Once the user confirms, an application can take photos at any given moment, without having to supply any visible indication that a photo is being taken. Since taking photos of users secretly is morally wrong (we don’t want Gmail or Instagram to take secret photos of us), a green camera-in-use indicator was added since iOS 14:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--sK6u3xSx--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ogvw3uk9b3z3b7ftkurr.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--sK6u3xSx--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ogvw3uk9b3z3b7ftkurr.jpeg" alt="Image description" width="800" height="420"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The green indicator becomes green once the camera is active, even if the photos aren’t saved nor transmitted anywhere. So, even if we have a program that just takes a photo without displaying anything particular on screen, the green indicator would disclose the fact that the camera is active.&lt;/p&gt;

&lt;p&gt;Finally, iOS has a special sound effect to let everyone know that a photo is taken:&lt;/p&gt;

&lt;p&gt;&lt;iframe width="710" height="399" src="https://www.youtube.com/embed/3zLX75CgQOs"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;p&gt;The only way to take a photo without this sound is to totally mute the volume while shooting.&lt;/p&gt;




&lt;p&gt;&lt;strong&gt;Solution&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;I’ve been studying this and finally created a small program that solves all of the above issues, and enables the user to take secret photos with more confidence.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;The program displays a black screen (no camera feed).&lt;/li&gt;
&lt;li&gt;The program repeatedly takes photos with a one second time interval (no user gesture is needed).&lt;/li&gt;
&lt;li&gt;Since the program controls the screen’s brightness and reduces it to the minimal possible value, the camera-in-use indicator is almost invisible, even if it is green, and by whole, the screen appears to be turned off.&lt;/li&gt;
&lt;li&gt;No need to mute the device: even when the device is set to high volume, the camera shooter sound is muted programatically.&lt;/li&gt;
&lt;li&gt;For a quick escape, touching the screen would immediately cause the program to exit back to the iPhone’s home screen.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--W9opkfzy--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/thwdhw9etznswe05gbuo.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--W9opkfzy--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/thwdhw9etznswe05gbuo.jpg" alt="Image description" width="210" height="216"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;It is important to note that I do not encourage anyone to actually use this program. After all, it is &lt;strong&gt;evil&lt;/strong&gt;. The purpose of this project is just to demonstrate how easy it is to create such a program. Furthermore, the code only uses well known system API’s without accessing any private system methods, so theoretically, it can pass AppStore review (if embedded as a secret module within a bigger app that has some other valuable content).&lt;/p&gt;

&lt;p&gt;The program’s code is publicly available on GitHub in the following address:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://github.com/arixegal/BlackEye/tree/MediumTutorial"&gt;https://github.com/arixegal/BlackEye/tree/MediumTutorial&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;However, in this article I will demonstrate how to create this program, step by step. A basic knowledge of the Swift programming language is required.&lt;/p&gt;




&lt;p&gt;&lt;strong&gt;General settings&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;We will need to create an xCode project, and a blank single page application.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Open a modern version of Xcode.&lt;/li&gt;
&lt;li&gt;Choose “Create a new Xcode Project”.&lt;/li&gt;
&lt;li&gt;In the Available Templates window Choose the “iOS -&amp;gt; App” template&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--O3yuMDez--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/716kgu6ljaop6ujmswsl.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--O3yuMDez--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/716kgu6ljaop6ujmswsl.jpeg" alt="Image description" width="720" height="520"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Enter a name for the project, a team and an Organization Identifier. The language should be &lt;strong&gt;Swift&lt;/strong&gt; and the Interface should be &lt;strong&gt;Storyboard&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--U2NZjuXC--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/m49nhqxqtcazlpr93vwn.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--U2NZjuXC--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/m49nhqxqtcazlpr93vwn.png" alt="Image description" width="754" height="545"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This will create an application that displays a blank screen. One step closer to the end game. In order to test it, type Command+R or select Product-&amp;gt;Run.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;How to display a black screen&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Since our app’s interface is Storyboard-based, and we already have the view controller that manages the display of the main screen, in order to display a black screen, all we have to do is change the background of this screen to black. This can be done directly from Interface Builder.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;In Project Navigator, select the file Main.storyboard&lt;/li&gt;
&lt;li&gt;In the storyboard’s Document Outline, select the View element (under “View Controller Scene” -&amp;gt; “View Controller”)&lt;/li&gt;
&lt;li&gt;In the view’s Attributes Inspector, change the Background to black.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--pw99O5M9--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pnn24sqkz36pfy3r6bcy.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--pw99O5M9--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pnn24sqkz36pfy3r6bcy.png" alt="Image description" width="880" height="465"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This will create an application that displays a black screen. In order to test it, type Command+R or select Product-&amp;gt;Run. It is important to select the color &lt;strong&gt;Black&lt;/strong&gt;, as opposed to other colors that appear to be black, such as &lt;em&gt;Label Color&lt;/em&gt;. Choosing &lt;em&gt;Label Color&lt;/em&gt; instead of &lt;em&gt;Black&lt;/em&gt; as a background color, will result in a black or a white screen, depending on the iPhone’s Dark Mode settings. Choosing Black will always result in a black screen, regardless of the iPhone’s Dark or Light mode.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;How to hide the Status Bar&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;If we run the app now, we will probably see a completely black screen. However, if the simulator or device we run the app on is set to Dark Mode, the screen wouldn’t be completely black. Dark Mode is achieved by selecting “Dark” under Settings -&amp;gt; Display &amp;amp; Brightness -&amp;gt; Appearance.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--7obVfuZn--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zoxgyyi9uhqhk7iwxrk3.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--7obVfuZn--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zoxgyyi9uhqhk7iwxrk3.jpeg" alt="Image description" width="248" height="440"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;When we run our app now, while the device is in Dark mode, The top Status Bar displays various elements, such as the time, power and WiFi indicators. When in Light Mode, the status bar elements were all black, so we couldn’t see them, but now since we are in Dark mode, they are light and visible.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--LVIqvzT5--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5j7ser6anz6nsog2cly8.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--LVIqvzT5--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5j7ser6anz6nsog2cly8.png" alt="Image description" width="280" height="591"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We want the screen to appear as if it is turned off. This means we have to hide the status bar. This can be achieved by adding two new key-value pairs to the target properties.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;In Project Navigator, select the top-most elements, which would be the project itself.&lt;/li&gt;
&lt;li&gt;While the project is selected, under &lt;strong&gt;TARGETS&lt;/strong&gt;, select the main target and navigate to the &lt;strong&gt;Info&lt;/strong&gt; tab.&lt;/li&gt;
&lt;li&gt;Click the &lt;strong&gt;Plus&lt;/strong&gt; button which becomes visible while hovering the mouse over the list items, and add the following key: &lt;strong&gt;UIViewControllerBasedStatusBarAppearance&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;Set the value of the newly added key to &lt;strong&gt;NO&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;Add another key: &lt;strong&gt;UIStatusBarHidden&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;Set the value of the above newly added key to &lt;strong&gt;YES&lt;/strong&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;You will notice that once the keys are added and displayed, the name &lt;strong&gt;UIStatusBarHidden&lt;/strong&gt; is displayed as &lt;em&gt;“Status bar is initially hidden”&lt;/em&gt;, and the name &lt;strong&gt;UIViewControllerBasedStatusBarAppearance&lt;/strong&gt; is displayed as &lt;em&gt;“View controller-based status bar appearance”&lt;/em&gt;. This is OK.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--3YqLQEoK--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/89qjwv8ldiznvjveguor.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--3YqLQEoK--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/89qjwv8ldiznvjveguor.png" alt="Image description" width="880" height="446"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;When running the app now, you will see that the screen is completely black, and all the status bar elements are missing, regardless of the iPhone’s Dark or Light mode. This is exactly what we want.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;How to respond to a tap event&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;When tapping the screen, we would like the app to completely exit. For that, we need to listen to taps. There are many ways of doing that. In this project we will add a black button programatically.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;In Project navigator, select &lt;strong&gt;ViewConroller.swift&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;Add the following code right under the class declaration:
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;private lazy var curtainView: UIView = {
    let size = UIScreen.main.bounds.size
    let btn = UIButton(
        frame: CGRect(
            x: 0,
            y: 0,
            width: size.width,
            height: size.height
         )
    )
    btn.backgroundColor = UIColor.black 
    btn.addTarget(self, action: #selector(quit), for: .touchDown)     

    return btn
}()
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;Add the &lt;strong&gt;quit&lt;/strong&gt; method
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;/// Will quit the application with animation
@objc private func quit() {
    UIApplication.shared.perform(#selector(NSXPCConnection.suspend))
    /// Sleep for a while to let the app goes in background
    sleep(2)
    exit(0)
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;Add the button to the view
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;override func viewDidLoad() {
    super.viewDidLoad()
    view.addSubview(curtainView)
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The above &lt;strong&gt;quit&lt;/strong&gt; method is copied from one of the answers in the following discussion:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://stackoverflow.com/questions/355168/proper-way-to-exit-iphone-application"&gt;https://stackoverflow.com/questions/355168/proper-way-to-exit-iphone-application&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Try running the app and test the behavior when the screen is tapped. If everything is done correctly, a tap on the screen would cause the app to exit.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;How to take a photo&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;There are many online tutorials and examples of how to operate the iPhone cameras programatically. Most of them have too much code for our purpose, since the conventional way of operating the cameras involves displaying the camera feed on the screen, which in our case is not needed.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;In &lt;strong&gt;ViewController.swift&lt;/strong&gt;, add the line &lt;strong&gt;import AVFoundation&lt;/strong&gt; at the top of the file.&lt;/li&gt;
&lt;li&gt;Add the following two instances right under the class declaration:
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;private let photoOutput = AVCapturePhotoOutput()
private let session = AVCaptureSession()
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Add the following 2 methods:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;private func setupCaptureSession() -&amp;gt; AVCaptureSession? {
    session.sessionPreset = .photo
    guard let cameraDevice = AVCaptureDevice.default(for: .video) else {           
        print("Unable to fetch default camera")
        return nil
    }
    guard let videoInput = try? AVCaptureDeviceInput(device: cameraDevice) else {
        print("Unable to establish video input")
        return nil
    }
    session.beginConfiguration()
        session.sessionPreset = AVCaptureSession.Preset.photo 
        guard session.canAddInput(videoInput) else {
            print("Unable to add videoInput to captureSession")
            return nil
        }

        session.addInput(videoInput)

        guard session.canAddOutput(photoOutput) else {
            print("Unable to add videoOutput to captureSession")
            return nil
        }

        session.addOutput(photoOutput)
        photoOutput.isHighResolutionCaptureEnabled = true
     session.commitConfiguration()
     DispatchQueue.global(qos: .background).async { [weak self] in
         self?.session.startRunning()
     }
     return session
}
private func takePhoto() {
    photoOutput.capturePhoto(with: AVCapturePhotoSettings(), delegate: self)
    DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + .seconds(1)) {[weak self] in
        self?.takePhoto()
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;After adding the methods, you will notice that the project won’t compile. The problem is that we passed &lt;em&gt;self&lt;/em&gt; as a delegate to the capturePhoto method, but &lt;em&gt;self&lt;/em&gt;, which is in this case the &lt;em&gt;UIViewController&lt;/em&gt; instance, doesn’t support the AVCapturePhotoCaptureDelegate protocol. We will need this support later on in order to save the photo and mute the sound. Keeping this in mind, in order to resolve the compilation error we will add the following stubs:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;extension ViewController: AVCapturePhotoCaptureDelegate {
    func photoOutput(_ output: AVCapturePhotoOutput, didFinishProcessingPhoto photo: AVCapturePhoto, error: Error?) {}
    func photoOutput(_ output: AVCapturePhotoOutput, willCapturePhotoFor resolvedSettings: AVCaptureResolvedPhotoSettings) {}
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So what do we have so far? two methods, one for configuring the camera session and the other for taking a photo (repeatedly with a one second interval). But we still need to connect them, so the final step is adding the first call:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;override func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(animated) 
    if UIImagePickerController.isSourceTypeAvailable(.camera) { 
        if let _ = setupCaptureSession() {
            takePhoto()
        } else {
            print("Failed to establish capture session")
        }
    } else {
        print("Camera not available") // Would be true in Simulator
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;When running the project now the build should succeed. However, after adding the first call we have a runtime crush. &lt;strong&gt;This app has crashed because it attempted to access privacy-sensitive data without a usage description&lt;/strong&gt;. To resolve this, we need to add some more key value pairs to the target. While we’re here, we will add a usage description for accessing the camera and another usage description for saving a photo into the photo album (which will be implemented later on).&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;In Project Navigator, select the top-most elements, which would be the project itself.&lt;/li&gt;
&lt;li&gt;While the project is selected, under &lt;strong&gt;TARGETS&lt;/strong&gt;, select the main target and navigate to the &lt;strong&gt;Info&lt;/strong&gt; tab.&lt;/li&gt;
&lt;li&gt;Click the &lt;strong&gt;Plus&lt;/strong&gt; button which becomes visible while hovering the mouse over the list items, and add the following (string) key: &lt;strong&gt;NSCameraUsageDescription&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;Set the value of the newly added key to &lt;em&gt;“In order to take secret photos”&lt;/em&gt;.&lt;/li&gt;
&lt;li&gt;Add another (string) key: &lt;strong&gt;NSPhotoLibraryAddUsageDescription&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;Set the value of the above newly added key to &lt;em&gt;“In order to store secret photos”&lt;/em&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;When running the app now, you should be able to see the Camera Access permission dialogue. Once authorized, you would be able to hear the camera shooter sound effect, and see the green camera in use indicator activated.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--W9opkfzy--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/thwdhw9etznswe05gbuo.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--W9opkfzy--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/thwdhw9etznswe05gbuo.jpg" alt="Image description" width="210" height="216"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;How to store the photo&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;In this program we will store each photo into the &lt;em&gt;Recent&lt;/em&gt; photo album. If we wanted, we could have just as well store them on a remote server, but this requires configuring a back-end and is beyond the purpose of this demo.&lt;/p&gt;

&lt;p&gt;Storing the photos is achieved by calling the system method &lt;em&gt;UIImageWriteToSavedPhotosAlbum&lt;/em&gt;.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Find the empty delegate method didFinishProcessingPhoto which was added earlier, and insert the following body:
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;func photoOutput(_ output: AVCapturePhotoOutput, didFinishProcessingPhoto photo: AVCapturePhoto, error: Error?) {
    guard let data = photo.fileDataRepresentation() else {
        print("Processing did finish with no data")
        return
    }
    guard let image = UIImage(data: data) else {
        print("Processing did finish with invalid image data")
        return
    }
    UIImageWriteToSavedPhotosAlbum(
        image, 
        self,  
        #selector(image(_:didFinishSavingWithError:contextInfo:)),           
        nil)

    print("Photo taken") 
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Add the following method to get a success / failure status:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;@objc func image(_ image: UIImage, didFinishSavingWithError error: NSError?, contextInfo: UnsafeRawPointer) {
    if let error = error {
        print("Save error: \(error.localizedDescription)")
    } else {
        print("Saved!")
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;When running the app now, you should be able to see the Gallery Access permission dialogue. Once authorized, you will be able to see the new photos in the “Photos” app. Try running the app for a few seconds and see if the photos are added.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;How to hide the Camera-in-Use indicator&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;iOS applications can control the screen brightness. Setting the screen brightness to the minimal possible value while the app is operating would cause the camera-in-use indicator to be barely-visible.&lt;/p&gt;

&lt;p&gt;We will add a separate class dedicated to controlling the screen brightness:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;final class DimUnDim {
    static let shared = DimUnDim()
    private var originalBrightness = UIScreen.main.brightness
    func dim() {
        print("dim")
        UIScreen.main.wantsSoftwareDimming = true
        UIScreen.main.brightness = 0.0
    }
    func unDim() {
        print("unDim")
        UIScreen.main.brightness = originalBrightness
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We will call Dim when the app becomes active and call UnDim when the app is not active. Edit the file **SceneDelegate.swift **and add the following:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;func sceneDidBecomeActive(_ scene: UIScene) {
    DimUnDim.shared.dim()
}
func sceneWillResignActive(_ scene: UIScene) {     
    DimUnDim.shared.unDim()
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is good as long as the app is running. If it is active the screen is dark, but if the user switches to the home screen or to another app, the screen brightness retains the original value.&lt;/p&gt;

&lt;p&gt;However, if the user manually terminates the app by touching the screen, the screen will remain dark because &lt;em&gt;sceneWillResignActive&lt;/em&gt; is not called. This is inpolite. We will fix this by adding another call to UnDim before quitting.&lt;/p&gt;

&lt;p&gt;Find the Quit method which as added earlier and add the call:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;/// Will quit the application with animation
@objc private func quit() {
    DimUnDim.shared.unDim() // Restore normal screen brightness
    UIApplication.shared.perform(#selector(NSXPCConnection.suspend))
    /// Sleep for a while to let the app go in background
    sleep(2)
    exit(0)
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;When running the project now, screen brightness should behave exactly as we need it to behave.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;How to disable the shooter sound&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Find the empty delegate method willCapturePhotoFor which was added earlier, and insert the following body:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;func photoOutput(_ output: AVCapturePhotoOutput, willCapturePhotoFor resolvedSettings: AVCaptureResolvedPhotoSettings) {
    // dispose system shutter sound
    AudioServicesDisposeSystemSoundID(1108)
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The above method is copied from one of the answers in the following discussion:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://stackoverflow.com/questions/4401232/avfoundation-how-to-turn-off-the-shutter-sound-when-capturestillimageasynchrono"&gt;https://stackoverflow.com/questions/4401232/avfoundation-how-to-turn-off-the-shutter-sound-when-capturestillimageasynchrono&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This is it.&lt;/p&gt;




&lt;p&gt;&lt;strong&gt;Final notes&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The ability to take secret photos as demonstrated in this project goes against the current ongoing trend of protecting user privacy. As demonstrated, the green camera-in-use indicator can be manipulated into non-visibility by controlling the screen brightness. This can be prevented by Apple in future versions of iOS. There shouldn’t be any problem to crash an app that tries to reduce screen brightness while the camera is active (just as we saw that the app is crashed when we failed to produce the camera-usage-description).&lt;/p&gt;

&lt;p&gt;Would it be possible to create a similar program that records a video instead of taking still photos? I don’t know, but it’s worth trying.&lt;/p&gt;

</description>
      <category>swift</category>
      <category>ios</category>
      <category>privacy</category>
    </item>
  </channel>
</rss>
