Google MLKit Swift Package Manager Wrapper

repository·main·Indexed 40 days ago

https://github.com/d-date/google-mlkit-swiftpm

A Swift Package Manager (SPM) wrapper for Google MLKit that enables the integration of Vision and Language APIs into iOS projects without CocoaPods. Supports modules including Barcode Scanning, Face Detection, Text Recognition, Image Labeling, Object Detection, Pose Detection, Selfie Segmentation, Language ID, Translation, and Smart Reply. Note: Building for the arm64 simulator is currently not supported.

Tokens
5.4K
Snippets
22
Records
35
Agent score
99%

What's inside google-mlkit-swiftpm

  1. Implement Vision and Language features

    main

    The project demonstrates 17 ML Kit features categorized into Vision and Language APIs.

    Vision Features

    • Face Detection: Detects landmarks, contours, and classifications (smiling, eye open, head angle).
    • Barcode Scanning: Supports 13 formats including Code 128/39/93, QR, EAN, UPC, PDF417, and Aztec.
    • Text Recognition: Supports Latin, Chinese, Devanagari, Japanese, and Korean scripts.
    • Image Labeling: Standard on-device labeling and Custom labeling using TFLite models (e.g., bird.tflite).
    • Object Detection: Supports Single/Multiple objects and With/Without classification modes.
    • Pose Detection: Provides Standard and Accurate modes with 33 body landmarks.
    • Selfie Segmentation: Generates foreground/background separation masks.

    Language Features

    • Language Identification: Detects language codes and confidence from text input.
    • Translation: Supports English, Japanese, Chinese, Korean, Spanish, and French. Note that the first use requires an automatic model download.
    • Smart Reply: Generates contextual reply suggestions for conversation interfaces.
  2. Use the BaseDetectionView pattern for vision features

    main

    The demo app uses a generic reusable view called BaseDetectionView to implement vision-based features. This pattern provides a consistent UI for:

    • Photo library picking
    • Camera capture
    • Sample image navigation (previous/next)
    • A detection button with loading states
    • Error handling

    When building your own vision feature views, you can follow this signature to ensure a consistent user experience.

    BaseDetectionView(
      title: String,
      sampleImages: [String],
      detectionHandler: (UIImage) async throws -> Void,
      resultView: () -> Content
    )
  3. Upload XCFramework artifacts to a GitHub release

    main

    Once XCFrameworks are built, you can upload the resulting zip files to an existing GitHub release using upload_release.sh.

    Note: The release must already exist. You can create one using the GitHub CLI:

    gh release create <version> --title "Release <version>" --notes "Updated to MLKit <version>"
    ./scripts/upload_release.sh <version>
    
    # Example
    ./scripts/upload_release.sh 7.0.0
  4. Trigger MLKit builds via GitHub Actions

    main

    You can manually trigger the XCFramework build process through the GitHub UI:

    1. Navigate to the Actions tab.
    2. Select the "Build MLKit XCFrameworks" workflow.
    3. Click "Run workflow".
    4. Enter the desired MLKit version (e.g., 5.1.0).
    5. Select whether to create a release automatically.
    6. Click "Run workflow".
  5. Install Google MLKit via Swift Package Manager

    main

    To use this wrapper, add the package dependency to your Package.swift.

    Important for App Store submissions: The standard from: "9.0.0" version contains internal Info.plist values that cause App Store Connect rejections. If you are submitting to the App Store, you must pin the wrapper-only repackage using the exact: parameter to use the fixed version (e.g., 9.0.0-1).

    After adding the package, add the specific ML Kit modules you need to your target dependencies.

    // Standard installation
    dependencies: [
        .package(url: "https://github.com/d-date/google-mlkit-swiftpm", from: "9.0.0")
    ]
    
    // REQUIRED for App Store submissions to avoid Info.plist rejections
    dependencies: [
        .package(url: "https://github.com/d-date/google-mlkit-swiftpm", exact: "9.0.0-1")
    ]
  6. Configure ML Kit module dependencies

    main

    Once the package is added, you must explicitly include the specific ML Kit products in your target's dependencies. Available products include:

    • MLKitBarcodeScanning
    • MLKitFaceDetection
    • MLKitTextRecognition (and variants: MLKitTextRecognitionChinese, MLKitTextRecognitionDevanagari, MLKitTextRecognitionJapanese, MLKitTextRecognitionKorean)
    • MLKitImageLabeling
    • MLKitObjectDetection
    • MLKitPoseDetection
    • MLKitSegmentationSelfie
    • MLKitLanguageID
    • MLKitTranslate
    • MLKitSmartReply
    .target(
        name: "YourTarget",
        dependencies: [
            .product(name: "MLKitBarcodeScanning", package: "google-mlkit-swiftpm"),
            .product(name: "MLKitFaceDetection", package: "google-mlkit-swiftpm"),
            .product(name: "MLKitTextRecognition", package: "google-mlkit-swiftpm"),
            .product(name: "MLKitImageLabeling", package: "google-mlkit-swiftpm"),
            .product(name: "MLKitObjectDetection", package: "google-mlkit-swiftpm"),
            .product(name: "MLKitPoseDetection", package: "google-mlkit-swiftpm"),
            .product(name: "MLKitSegmentationSelfie", package: "google-mlkit-swiftpm"),
            .product(name: "MLKitLanguageID", package: "google-mlkit-swiftpm"),
            .product(name: "MLKitTranslate", package: "google-mlkit-swiftpm"),
            .product(name: "MLKitSmartReply", package: "google-mlkit-swiftpm"),
        ]
    )
  7. Follow the recommended testing flow for new releases

    main

    When preparing a new version of the MLKit SwiftPM wrapper, follow this sequence to ensure stability:

    1. Build: Run the build script for the target version.
    2. Analyze: Run static analysis on the resulting artifacts.
    3. Verify: Open the Example app and test on a physical device.
    4. Commit: If all tests pass, commit the updated Podfile, Package.swift, and Resources/.
    # 1. Build new version
    ./scripts/build_all.sh <version>
    
    # 2. Run static analysis
    ./scripts/verify_runtime.sh <version>
    
    # 3. Test Example app
    cd Example
    open Example.xcworkspace
    # Build and run on device
    # Test all features
    # Check console
    
    # 4. If all tests pass, commit
    cd ..
    git add Podfile Podfile.lock Package.swift Resources/
    git commit -m "Update to MLKit <version>"
    git tag -a <version> -m "Release <version>"
  8. Perform static analysis on MLKit builds

    main

    After building a new version, you should run manual static analysis to check for common runtime issues such as incorrect architectures, missing Info.plist files, or invalid symbol tables. Use the verify_runtime.sh script and provide the version number as an argument.

    ./scripts/verify_runtime.sh <version>
  9. Build the xcframework-maker tool

    main

    The xcframework-maker tool is a custom utility used to convert MLKit frameworks into XCFrameworks. It handles static frameworks with FAT object files, adds arm64 simulator support, and manages Info.plist files. You must build this tool before attempting to create XCFrameworks.

    make bootstrap-builder
  10. Configure permissions and resources for ML Kit

    main

    To ensure the ML Kit features work correctly in your project, verify the following setup steps:

    Permissions

    Add the following keys to your Info.plist to allow access to hardware and data:

    • Camera: Required for real-time vision features.
    • Photo Library: Required for picking images from the gallery.

    Resources

    • Custom Models: If using custom object detection or image labeling, ensure your .tflite models (e.g., bird.tflite) are included in your app's target bundle resources.
    • Sample Images: Ensure all test images used for navigation are included in the Resources folder and added to the target.
  11. Test the Example app for runtime stability

    main

    The most critical step in verifying a build is running the provided Example app. This ensures that the frameworks link correctly and that features like Face Detection or Barcode Scanning do not crash at runtime.

    Device Testing (Required)

    1. Build for a physical iOS device.
    2. Ensure the app launches without crashing.
    3. Navigate through all available features (Barcode Scanner, Face Detection, etc.).
    4. Monitor the Xcode console for warnings like Could not load bundle... or symbol conflicts.

    Simulator Testing (Optional)

    1. Build for the iOS Simulator.
    2. Note that camera-based features may not function in the simulator.
    3. Verify basic UI navigation works.
    cd Example
    open Example.xcworkspace