Add an Apple Watch Target on Flutter app
Tags: flutter, applewatch
Published: 2022-07-20 16:06:42

Refer from the Flutter document: https://docs.flutter.dev/development/platform-integration/ios/apple-watch

First off, you need to follow the instruction provided in the Flutter document (link above) first.

1. Enable bitcode in Xcode
Apple Watch targets require bitcode to be enabled, so follow the steps in [Creating an iOS Bitcode enabled app](https://github.com/flutter/flutter/wiki/Creating-an-iOS-Bitcode-enabled-app) to use bitcode in your app.

2. Add an Apple Watch target
In the menu, select File > New > Target. Once the dialog opens, select watchOS at the top and click Watch App for iOS App. Click Next, enter a product name, and select Enter.

3. When you run the project using a usual command `flutter run`. You will get the error like this

```
# flutter run                                                                                                                                                     
Launching lib/main.dart on iPhone 13 Pro in debug mode...
Running Xcode build...                                          
Xcode build done.                                            2.1s
Failed to build iOS app
Error output from Xcode build:
↳
    2022-07-20 23:02:24.107 xcodebuild[18239:137665] Requested but did not find extension point with identifier Xcode.IDEKit.ExtensionSentinelHostApplications for extension Xcode.DebuggerFoundation.AppExtensionHosts.watchOS of plug-in com.apple.dt.IDEWatchSupportCore
    2022-07-20 23:02:24.108 xcodebuild[18239:137665] Requested but did not find extension point with identifier Xcode.IDEKit.ExtensionPointIdentifierToBundleIdentifier for extension Xcode.DebuggerFoundation.AppExtensionToBundleIdentifierMap.watchOS of plug-in com.apple.dt.IDEWatchSupportCore
    ** BUILD FAILED **


Xcode's output:
↳
    Writing result bundle at path:
        /var/folders/_6/2nt5c3lx2tbc9bkg1vkb5tfm0000gp/T/flutter_tools.zDoJxh/flutter_ios_build_temp_dirlIvUEW/temporary_xcresult_bundle

    note: Using new build system
    note: Planning
    note: Build preparation complete
    note: Building targets in dependency order
    error: unable to resolve product type 'com.apple.product-type.application.watchapp2' for platform 'iphonesimulator' (in target 'watch' from project 'Runner')
    error: unable to resolve product type 'com.apple.product-type.application.watchapp2' for platform 'iphonesimulator' (in target 'watch' from project 'Runner')
    error: Couldn't look up product type 'com.apple.product-type.application.watchapp2' in domain 'iphonesimulator': Couldn't load spec with identifier 'com.apple.product-type.application.watchapp2' in domain 'iphonesimulator' (in target 'watch' from project 'Runner')
    warning: duplicate output file '' on task: RegisterExecutionPolicyException /Users/gie/Develops/test/flutter/app_watch/build/ios/Debug-iphonesimulator (in target 'watch' from project 'Runner')
    error: unable to resolve product type 'com.apple.product-type.watchkit2-extension' for platform 'iphonesimulator' (in target 'watch WatchKit Extension' from project 'Runner')
    error: unable to resolve product type 'com.apple.product-type.watchkit2-extension' for platform 'iphonesimulator' (in target 'watch WatchKit Extension' from project 'Runner')
    error: Couldn't look up product type 'com.apple.product-type.watchkit2-extension' in domain 'iphonesimulator': Couldn't load spec with identifier 'com.apple.product-type.watchkit2-extension' in domain 'iphonesimulator' (in target 'watch WatchKit Extension' from project 'Runner')

    Result bundle written to path:
        /var/folders/_6/2nt5c3lx2tbc9bkg1vkb5tfm0000gp/T/flutter_tools.zDoJxh/flutter_ios_build_temp_dirlIvUEW/temporary_xcresult_bundle


Error (Xcode): unable to resolve product type 'com.apple.product-type.application.watchapp2' for platform 'iphonesimulator'

Error (Xcode): Couldn't look up product type 'com.apple.product-type.application.watchapp2' in domain 'iphonesimulator': Couldn't load spec with identifier 'com.apple.product-type.application.watchapp2' in domain 'iphonesimulator'

Error (Xcode): unable to resolve product type 'com.apple.product-type.watchkit2-extension' for platform 'iphonesimulator'

Error (Xcode): Couldn't look up product type 'com.apple.product-type.watchkit2-extension' in domain 'iphonesimulator': Couldn't load spec with identifier 'com.apple.product-type.watchkit2-extension' in domain 'iphonesimulator'

Could not build the application for the simulator.
Error launching application on iPhone 13 Pro.
```

4. What you need to do is to update the WatchKit Extension by providing it the `WKCompanionAppBundleIdentifier` by using the following code:

```
<key>WKCompanionAppBundleIdentifier</key>
<string>$(PRODUCT_BUNDLE_IDENTIFIER)</string>
```

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5s5edgyt4ykcr9w0hfag.png)
 
5. Clean and rerun your project again

```
flutter clean
flutter run
```