# Liderit Printer Daemon — iOS Wrapper

This directory contains the Swift wrapper app for the Liderit printer daemon on iOS.

## Architecture

The iOS implementation uses a **Go framework compiled for iOS via cgo** wrapped in a **Swift app**.

```
┌──────────────────┐
│  iOS App         │
│  (Swift/SwiftUI) │
└────────┬─────────┘
         │
         │  cgo / FFI
         ▼
┌────────────────────┐
│  Go Framework      │
│  (printer daemon   │
│   logic)           │
└────────┬───────────┘
         │
         │  (via HTTP/mTLS)
         ▼
┌────────────────────┐
│  go-daemon server  │
│  receive jobs      │
└────────────────────┘
```

## Requirements

- **Xcode** 14.0+ with iOS SDK 14.0+
- **Swift** 5.7+
- **Go** 1.23+
- **goswift** (for Go ↔ Swift FFI)
- Apple Developer Account (for device provisioning & App Store distribution)

## Build Steps

### 1. Build the Go Framework for iOS

```bash
cd ..

# Requires gomobile tool:
go install github.com/golang/mobile/cmd/gomobile@latest

# Build for iOS (arm64, real device):
gomobile bind -target=ios -o ios/LideritPrinter.xcframework ./cmd/printer

# Optional: Build for iOS Simulator (amd64):
gomobile bind -target=ios -o ios/LideritPrinterSim.xcframework ./cmd/printer \
  -ldflags="-linkmode external -o /path/to/simulator/build"
```

Output: `ios/LideritPrinter.xcframework`

### 2. Create Xcode App Project

```bash
cd ios

# If starting fresh, create a new project:
xcodebuild -scheme LideritPrinter -configuration Release build

# OR manually in Xcode:
# File > New > Project > App (iOS)
# Import LideritPrinter.xcframework to the project
# Link in Build Phases > Link Binary With Libraries
```

### 3. Build and Archive

```bash
xcodebuild -scheme "LideritPrinter" -archivePath build/LideritPrinter.xcarchive archive
xcodebuild -exportArchive -archivePath build/LideritPrinter.xcarchive \
  -exportOptionsPlist ExportOptions.plist -exportPath build/ipa
```

Output: `build/ipa/LideritPrinter.ipa`

### 4. Deploy to Device (Development)

```bash
# Plug in device via USB
xcodebuild -scheme "LideritPrinter" -configuration Debug build -destination generic/platform=iOS install
```

### 5. Deploy to App Store

```bash
# Requires valid Apple Developer signing identity
xcodebuild -exportArchive -archivePath build/LideritPrinter.xcarchive \
  -exportOptionsPlist ExportOptions.plist \
  -exportPath build/ \
  -signingStyle automatic

# Or upload manually via Xcode or Transporter
```

## Implementation Notes

### Configuration Interface (scaffold)

The repository now includes a minimal iOS configuration UI scaffold under:

- `App/Config/AppConfig.swift`
- `App/Config/ConfigStore.swift`
- `App/Config/ConfigView.swift`
- `App/Config/ConfigHostViewController.swift`

This interface is configuration-only for now: server address, client key, and default printer mapping.
It persists values to `Application Support/LideritPrinter/printer-config.json` and does not yet include full Go bridge print execution wiring.

### App Structure

The Swift app provides:
- **SceneDelegate**: Manages app lifecycle, starts the daemon when app enters foreground
- **PrinterViewController**: UI for printer configuration, status monitoring, manual trigger
- **PasteboardMonitor**: Listens for files shared via document picker or paste

### Printing (AirPrint)

The app uses **AirPrint** (iOS native printing API):
- `UIPrinterDiscovery` — discovers network & Bonjour printers
- `UIPrintInteractionController` — presents print UI
- `UIPrintJob` — tracks job status

The Go daemon receives print jobs and forwards them to AirPrint via Swift callbacks (implemented via cgo).

### Background Execution

**Limitation**: iOS suspends apps when backgrounded. To keep the daemon alive:
- Use **NSURLSession background transfers** (for file delivery only)
- Use **VoIP background mode** (with WebRTC; not recommended for printing)
- **Recommended**: Keep app in foreground or use App Clips for triggered printing

For best results, recommend users keep the app open or integrate with your Panel UI (Web-based printing interface).

### File Storage

The app stores files and config in:
- `~/Documents/` — user-facing files
- `~/Library/Application Support/` — app config & certs (iOS sandboxed)

Certificate paths:
```swift
let certsDir = FileManager.default.urls(for: .applicationSupportDirectory, in: .userDomainMask)[0]
  .appendingPathComponent("certs")
// client.crt, client.key, ca.crt
```

### Permissions Required

The app requires the following in Info.plist:
```xml
<key>NSLocalNetworkUsageDescription</key>
<string>Liderit Printer needs access to your local network to discover printers.</string>

<key>NSBonjourServices</key>
<array>
  <string>_ipp._tcp</string>
  <string>_printer._tcp</string>
</array>

<key>NSDocumentsFolderUsageDescription</key>
<string>Liderit Printer needs access to Documents to save received files.</string>
```

## Troubleshooting

### App Crashes on Startup

Check Xcode logs:
```bash
log stream --device --predicate 'process == "LideritPrinter"'
```

### Go Framework Won't Load

Ensure cgo is properly linked:
```bash
otool -L ios/LideritPrinter.xcframework/ios-arm64/LideritPrinter.framework/LideritPrinter
```

### Printer Discovery Failed

1. Open the app's printer list
2. Ensure a printer is on the same Wi-Fi network or connected via USB
3. Enable airplane mode → disable → re-enable to force network discovery

## Limitations & Recommendations

1. **Background Execution**: iOS restricts daemon mode. The app must remain active or use designated background modes.
2. **Silent Printing**: iOS does NOT support silent printing (always shows print UI to user).
3. **Foreground Service**: iOS doesn't have a concept of "foreground service" like Android; use push notifications to wake the app if needed.

**Recommendation**: Market this as a **user-initiated print handler** rather than a true daemon. Integrate with Panel UI for on-demand printing via web interface.

## Next Steps

- Implement Swift ↔ Go bindings via gomobile / cgo
- Create SwiftUI UI for printer setup and job monitoring
- Integrate UIPrinterDiscovery for printer enumeration
- Implement APNs (Apple Push Notifications) for job triggers
- Publish to App Store (requires review & Apple Developer Program enrollment)
