跳转至

Iphone and Macbook Approach

This is the gold standard setup for mobile development. Switching to a MacBook + iPhone simplifies the engineering process by about 50%.

You eliminate all the "bridging" hacks required on Windows (WSL, ADB, Port Forwarding). You gain native access to the hardware, the simulator, and the critical iOS configuration tools.

Here is your revised, streamlined Executable Plan for the Mac + iPhone ecosystem.

1. The Setup (Much Faster)

On a Mac, you don't need complex Linux subsystems. You just use Homebrew and Xcode.

  1. Install Xcode: Download it from the App Store. Open it once to accept the license components.
  2. Install CocoaPods: (Required for iOS dependencies)
    sudo gem install cocoapods
    
  3. Install Flutter:
    • Download the Flutter SDK for macOS (Silicon/Intel).
    • Unzip it to ~/development/flutter.
    • Add to your path (Zsh is default on Mac):
      echo 'export PATH="$PATH:$HOME/development/flutter/bin"' >> ~/.zshrc
      source ~/.zshrc
      
  4. Verify Setup: Run flutter doctor. It will likely ask you to run some Xcode command-line tool agreements, which are easy copy-paste commands.

2. Project Initialization

Create the project exactly as before, but now we configure the iOS layer immediately.

flutter create autobiography_app --org com.yourname.biography
cd autobiography_app

3. Critical iOS Configuration (The "Permissions" Layer)

Since you are recording audio, Apple requires strict permission declarations. If you don't add these, your app will crash instantly upon hitting "Record."

Open the file ios/Runner/Info.plist (you can open this file in VS Code or right-click ios/Runner.xcworkspace and open it in Xcode).

Add these keys:

  1. Microphone Usage:
    • Key: Privacy - Microphone Usage Description
    • Value: "This app needs your microphone to record your autobiography memories."
  2. Documents Access (For debugging files):
    • Key: LSSupportsOpeningDocumentsInPlace -> YES
    • Key: UIFileSharingEnabled -> YES

The "Background Audio" Settings: If you want to talk for 30 minutes, the screen might turn off. To keep recording: 1. Open Xcode. 2. Click the "Runner" (blue icon) on the left. 3. Select the "Signing & Capabilities" tab. 4. Click "+ Capability" and add Background Modes. 5. Check the box "Audio, AirPlay, and Picture in Picture".

4. The "Recorder" Code (Optimized for iPhone)

Apple devices use CoreAudio. We want to use the .m4a container with AAC encoding, which Gemini loves.

File: lib/services/audio_recorder.dart

import 'package:flutter_sound/flutter_sound.dart';
import 'package:path_provider/path_provider.dart';
import 'package:permission_handler/permission_handler.dart'; // Add this package

class AudioRecorderService {
  final FlutterSoundRecorder _recorder = FlutterSoundRecorder();
  bool _isRecorderInitialised = false;

  Future<void> init() async {
    // 1. Request Permission explicitly on iOS
    var status = await Permission.microphone.request();
    if (status != PermissionStatus.granted) {
      throw RecordingPermissionException('Microphone permission not granted');
    }

    await _recorder.openRecorder();

    // 2. Set the iOS Audio Session to "Speech" mode
    // This optimizes the mic for voice (cancels background noise)
    await _recorder.setSubscriptionDuration(const Duration(milliseconds: 100));
    _isRecorderInitialised = true;
  }

  Future<String> startRecording() async {
    if (!_isRecorderInitialised) await init();

    final dir = await getApplicationDocumentsDirectory(); // iOS preferred path
    String fileName = 'memory_${DateTime.now().millisecondsSinceEpoch}.m4a';
    String path = '${dir.path}/$fileName';

    // 3. Start Recording with AAC/MP4
    await _recorder.startRecorder(
      toFile: path,
      codec: Codec.aacMP4, // Native iOS format
      bitRate: 64000,      // Perfect balance for Gemini API
      sampleRate: 44100,   // Standard iOS sample rate
    );
    return path;
  }

  Future<String?> stopRecording() async {
    return await _recorder.stopRecorder();
  }
}

5. Running the App (The Workflow)

This is where the Mac shines. You have two options:

Option A: The Simulator (Fast Development) You don't even need to plug in your phone yet.

open -a Simulator
flutter run
Note: The Simulator input uses your Mac's microphone.

Option B: The Physical iPhone (Wireless Debugging) 1. Plug your iPhone into the Mac via cable. 2. Open Xcode. 3. Go to Window > Devices and Simulators. 4. Check "Connect via Network". 5. Unplug the cable. 6. Run flutter run -d <Your iPhone Name> from your terminal.

You can now walk around your house, dictating your autobiography, while the logs stream wirelessly to your Mac screen.

6. The "Magic Moment" Verification

Since you are using Firebase and Google Cloud, the backend logic remains identical. 1. Record on iPhone. 2. App uploads to Firebase Storage. 3. Cloud Function triggers. 4. Gemini processes it. 5. Firestore updates.

Pro Tip for iOS Dev: When testing on the iPhone, you can use the Files app on your phone. Because we enabled UIFileSharingEnabled, you can go to Files > On My iPhone > Autobiography App and actually see/play the .m4a files you just recorded before they are uploaded. This is a lifesaver for debugging audio quality.