Skip to content

Commit 73edc0c

Browse files
authored
Merge pull request #40 from nativeapptemplate/env-var-refactor
Read API endpoint from env vars in Debug builds
2 parents 633f411 + 56ed53f commit 73edc0c

3 files changed

Lines changed: 20 additions & 12 deletions

File tree

NativeAppTemplate/Constants.swift

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -94,12 +94,10 @@ enum NativeAppTemplateConstants {
9494

9595
extension String {
9696
#if DEBUG
97-
// static let scheme: String = "http"
98-
// static let domain: String = "192.168.1.21"
99-
// static let port: String = "3000"
100-
static let scheme: String = "https"
101-
static let domain: String = "api.nativeapptemplate.com"
102-
static let port: String = ""
97+
private static let env = ProcessInfo.processInfo.environment
98+
static let scheme: String = env["NATEMPLATE_API_SCHEME"] ?? "https"
99+
static let domain: String = env["NATEMPLATE_API_DOMAIN"] ?? "api.nativeapptemplate.com"
100+
static let port: String = env["NATEMPLATE_API_PORT"] ?? ""
103101
#else
104102
static let scheme: String = "https"
105103
static let domain: String = "api.nativeapptemplate.com"

NativeAppTemplateTests/UI/Shop Settings/NumberTagsWebpageListViewModelTest.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ struct NumberTagsWebpageListViewModelTest {
121121
itemTagsCount: 10,
122122
scannedItemTagsCount: 5,
123123
completedItemTagsCount: 3,
124-
displayShopServerPath: "https://api.nativeapptemplate.com/display/shops/\(id)?type=server"
124+
displayShopServerPath: "/display/shops/\(id)?type=server"
125125
)
126126
}
127127
}

README.md

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -141,13 +141,23 @@ To run this app successfully, ensure you have:
141141

142142
## Running with the NativeAppTemplate-API on localhost
143143

144-
To connect to a local API server, update the following configuration in in Constants.swift:
144+
To connect to a local API server, set these env vars on the Xcode scheme (Edit Scheme → Run → Arguments → Environment Variables):
145145

146-
```swift
147-
static let scheme: String = "http"
148-
static let domain: String = "192.168.1.21"
149-
static let port: String = "3000"
150146
```
147+
NATEMPLATE_API_SCHEME = http
148+
NATEMPLATE_API_DOMAIN = <your-lan-ip>
149+
NATEMPLATE_API_PORT = 3000
150+
```
151+
152+
> **Note:** Never use `127.0.0.1`, `localhost`, or `0.0.0.0` for `NATEMPLATE_API_DOMAIN` — those resolve to the iOS Simulator/device itself, not your Mac. Use your Mac's LAN IP (e.g., `192.168.1.6`) so the simulator or a physical device can reach the API server.
153+
154+
Keep the scheme in `xcuserdata` (per-developer, gitignored), not `xcshareddata`. In Xcode, open **Product → Scheme → Manage Schemes…**, find `NativeAppTemplate`, and **uncheck "Shared"**. This moves the scheme (with your local env vars) to `xcuserdata/<user>.xcuserdatad/xcschemes/` so your API settings are not committed. If Xcode staged a deletion of the previously shared scheme, restore it with:
155+
156+
```bash
157+
git restore --source=HEAD --staged --worktree NativeAppTemplate.xcodeproj/xcshareddata/xcschemes/NativeAppTemplate.xcscheme
158+
```
159+
160+
Debug builds read these at launch via `ProcessInfo.processInfo.environment` in `Constants.swift`; when unset, they fall back to the production defaults (`https://api.nativeapptemplate.com`). Release builds always use the production defaults.
151161

152162
## SwiftLint
153163

0 commit comments

Comments
 (0)