22

I was hoping someone could shed some light on this. I've tried researching but couldn't really find anything...

Is there a way to share a class between the iPhone application and the apple watch extension?

Let's say in the watch extension I have a class myClass; I am unable to use this class within the phone application. Is there anyway to get around this?

Will Richardson
  • 7,780
  • 7
  • 42
  • 56
prawn
  • 2,643
  • 2
  • 33
  • 49

5 Answers5

44

iPhone application and the apple watch extension are basally different targets in your project. For each file (except the .h file), you can specify which target you want to include it to.

There are at least two ways to do this.

1) From the top menu select View -> Utilities -> File Inspector. The right panel will show up and you should find the Target Membership section with checkboxes next to the available targets for you project. Check the targets you want to include this file to. In this case, the iPhone app and the WatchKit extension.

2) You can also click on the project to go to the project settings. Select the target you want. (i.e. iPhone app or WatchKit extension), click on the Build Phases tab. In the Compile sources, you can click on the "+" icon to add more files to build for the target. (i.e. myClass.m in your case)

Hope this helps!

j-techsha
  • 464
  • 4
  • 5
7

Another possible solution (and what eventually solved my problem) is making sure your Watch Target also knows where your bridging file lives!

  • Open your project settings and select your WatchKit extension.

  • In Build Settings, search for the keyword "bridging"

  • Make sure the Objective-C Bridging Header file is set enter image description here

erparker
  • 1,301
  • 10
  • 21
6

There are two ways to do this.

  1. Create a framework with your shared code in there and link that framework to your WatchKit extension and your iPhone app. This is the cleanest and recommended way.

  2. Add the source file (and dependancies) for MyClass to both your iPhone and WatchKit extension targets.

Stephen Johnson
  • 5,293
  • 1
  • 23
  • 37
1

In case you wish to share Obj-C classes from iPhone app in Swift code in Watch App Extension:

  1. Include all the .m files that you wish to share from iPhone app, in the Target for WatchKit Extension app (i.e. in Target Membership).

  2. Create a bridging header (a new .h file) file (typically named [projectName]-Bridging-Header.h). You can optionally name it as [projectName]-Watch-Bridging-Header.h (or anything else that is suitable).

  3. Include all the .h files corresponding to .m included in the WatchKit Extension target as imports in the bridging-header file (e.g. #import "MyLogger.h").
  4. You are ready to use the Obj-C classes in WatchKit Extension App code now.
rkwatra
  • 195
  • 1
  • 3
0

with the simplest method, please select 3 number checkbox.

enter image description here

as select this checkbox you add this class to another target. Anymore, you can access this class from another target which is apple watch side.

enter image description here

Okan TEL
  • 26
  • 3