0

Is there a way to create a class that is available to both iOS and WatchOS apps?

I'd like to create a mock data source from a single static class. I don't want to the class to live inside the iOS app or the Watchkit Extension. But I'd still like it to be inside the overall project.

If that isn't possible, I guess having the class live inside the iOS app is best? From there, I can transfer its data to the WatchOS app.

rickster
  • 124,678
  • 26
  • 272
  • 326
4thSpace
  • 43,672
  • 97
  • 296
  • 475
  • 2
    I am not quite sure if i understood your problem, but i think including that file into both iOS and WatchOS target will do the job. – Gandalf Jan 15 '16 at 15:04
  • When I create the class in the iOS app, the watch app doesn't see it. – 4thSpace Jan 15 '16 at 16:13
  • Just tick the watch extension option under inspector – lzl Jan 15 '16 at 16:16
  • I have no idea what you are talking about. Can you elaborate? – 4thSpace Jan 15 '16 at 16:40
  • 1
    When you select a source file (.m or .swift) and bring up the "file inspector" for that file there is a group "Target membership" that tells the compiler which of your project's targets to build the file into. If you check the watch app it should build the file for both the iOS app and the paired watch app, although there are not very many base classes in common between the 2. – Duncan C Jan 15 '16 at 16:52
  • The File Inspector is the first tab of the right panel. – jcaron Jan 15 '16 at 18:10
  • Thanks! @lzl, if you put as answer, I'll accept. – 4thSpace Jan 15 '16 at 19:11

3 Answers3

2

Tick the watch extension option under "target membership" of "file inspector" to include that file under the extension :)

lzl
  • 469
  • 4
  • 10
1

Create a framework for all the classes you would like both apps to use and import that framework when you want to use them

  • This would be a good solution where there are lots of classes shared between the two. In the case of a single (or small number) of classes it's almost certainly not worth the overhead. – Stephen Darlington Jan 15 '16 at 22:13
1

After you create the classes, you can edit the "target membership" in file inspector as another answer mentioned, which let's you include the file in each of your targets.

Additionally, since certain classes, functions, etc are not available in WatchKit, you may want to add a preproccessor macro to a target's build settings that you can use to define portions of the code that are only available to the Watch or iPhone.

You can define a preprocessor macro in your iPhone target 'Build Settings' > 'Preprocessing' (make sure 'all' is selected at the top left, not just 'basic'). See screen shot for preprocessor macro definition

Once you have that defined, you can use them in your code using the #ifdef preprocessor:

#ifdef IS_IPHONE  
// iphone specific code here...
#endif

UPDATE:

As mentioned in the comment below, for swift you could use the following without the need for the preprocessor macro:

#if os(iOS)
// iPhone specific code
#endif
Gavin Potts
  • 121
  • 4