3

I created a simple environment variable for testing as you can see here:

When I access this env variable in viewDidLoad(), it's accessible on the app's initial build, but when I close out of the app on both my actual iPhone and simulator, then re-open the app, the app crashes and I can't do anything.

Here is the simple code causing the crash on re-open:

import UIKit

class ViewController: UIViewController {
    override func viewDidLoad() {
        super.viewDidLoad()
        print(ProcessInfo.processInfo.environment["TEST"]!)
    }
}

I realize unwrapping the optional is what's causing the crash, but why isn't the environment variable accessible after app close?

jscs
  • 63,694
  • 13
  • 151
  • 195
10000RubyPools
  • 1,182
  • 3
  • 11
  • 24

2 Answers2

3

As soon as you close the app manually and reopen it, the app itself will be launched straight from the simulator and not from Xcode, so the environment variables declared in your run scheme cannot be set. Hence, the force unwrapping crashes.

Dávid Pásztor
  • 51,403
  • 9
  • 85
  • 116
2

The environment variable is only available when run through Xcode. Your code is force-unwrapping a nil value that will never exist in your real iOS app installed on user's devices.

You need to safely check to see if the value is there during testing via Xcode.

rmaddy
  • 314,917
  • 42
  • 532
  • 579
  • 1
    I need these .env variables for my app to run, I.E. a URL I can reference to test my API on a local server and a staging server. If these .env variables cannot be accessed on re-open, how does one go about declaring .env variables meant to be ran without Xcode? – 10000RubyPools Jan 05 '19 at 21:24
  • 1
    You can't use environment variables in an iOS app except when running from Xcode. You need a different way to know which server to use. – rmaddy Jan 05 '19 at 21:25
  • Damn, iOS / Swift is severely behind in terms of technology then, very surprised this isn't available in 2019. Thanks for your help. – 10000RubyPools Jan 05 '19 at 21:27
  • 1
    It has nothing to do with Swift. When an iOS app is installed on a user's device, there is no way for you to specify what environment variables to set. – rmaddy Jan 05 '19 at 21:28
  • 1
    I'm just used to a compiler pulling the .env variables and embedding them directly into the app, similar to a .env file in JavaScript. Seems to be the simplest, most straight forward way to do things. – 10000RubyPools Jan 05 '19 at 21:35
  • 1
    From what I'm researching now, the way to do this is by creating multiple .plist files and configs which is just so overkill in my opinion. Have seen improvements on swift via libraries, but so much of its current state could drastically be simplified. – 10000RubyPools Jan 05 '19 at 21:36
  • You don't want to compile in the choice either. The same binary might be used for the App Store, Test Flight, and/or adhoc deployment. – rmaddy Jan 05 '19 at 21:37
  • 2
    Your original question has been answered. You should do a little searching and if needed, post a new question specific to how to handle specifying a different server at different times as needed. – rmaddy Jan 05 '19 at 21:38
  • @rmaddy Giving an "It's not there" answer followed by "You're question has been answered. Move on." Isn't helpful. Let's cut our friends a little slack and not pick apart their phrasing. The underlying issue they're having is _where to put the env variable so it persists_. – joshuakcockrell Oct 28 '22 at 22:26