Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set up a project with shared frameworks between iOS and watchOS 2 similar to Lister App?

The Lister app is one of Apple's only Apple Watch sample projects. It was first written to support the watchOS 1, and later updated on 9/16/15 to support watchOS 2. Because of this, there are many targets, groups, etc. that aren't consistently named, making it hard to understand what is what.

I want to create a similar project, where I have an iOS and watchOS 2 with shared code/frameworks. I'm having a hard time understanding how to set up such a project due to the complexity of the Lister app.

How should I set up my project so that I can share code between my watchOS 2 and iOS app? (Note: support for watchOS 1 is not a requirement.)


For reference, here is a list of the targets, schemes, and groups from the Lister app:

  • Targets: Lister, Lister Tests, Lister Today, Lister WatchKit Extension, Lister WatchKit App, ListerKit, ListerKit Tests, Lister Watch Extension, Lister Watch App, ListerKit (watchOS), Lister (OS X), Lister Today (OS X), ListerKit (OS X), ListerKit Tests (OS X)
  • Schemes: Lister, Lister Today, Lister WatchKit App, Glance - Lister WatchKit App, ListerKit, Lister Watch App, Glance - Lister Watch App, ListerKit (watchOS), Lister (OS X), Lister Today (OS X), ListerKit (OS X)
  • Groups: Lister iOS App, Lister WatchKit App, Shared Lister WatchKit Code, Lister Watch App, Lister OS X App, Shared ListerKit Framework Code, Shared Lister Resources
like image 929
Senseful Avatar asked Dec 20 '15 23:12

Senseful


1 Answers

The project is so complex because it supports both watchOS 1 and watchOS 2.

Summary

If all you want is a to support watchOS 2 and share code between iOS and watchOS 2, you will need to create two frameworks, one for each platform.

watchOS 1:

  • Code is shared between watchOS 1 and watchOS 2 by simply including .swift files in both targets (Lister WatchKit Extension, and Lister Watch Extension). (See the Shared Lister WatchKit Code group.)
  • Code is shared between watchOS 1 and iOS via the Lister iOS Framework (ListerKit).

watchOS 2:

  • Code is shared between watchOS 2 and watchOS 1 by simply including .swift files in both targets (Lister WatchKit Extension, and Lister Watch Extension). (See the Shared Lister WatchKit Code group.)
  • Code is shared between watchOS 2 and iOS by including shared files (e.g. List.swift) in both the Lister iOS Framework (ListerKit) and Lister watchOS 2 Framework (ListerKit (watchOS)). (See the Shared ListerKit Framework Code group.)

    In the Sharing Code Between an iOS App and a watchOS App section of the watchOS 2 Transition Guide, they confirm this kind of set up for watchOS 2:

    In watchOS 2, you can share code, but not frameworks, between your iOS app and Watch app. Because the apps run on separate platforms with different architectures, source files must be compiled separately for each platform. If you still want to use a framework to manage any shared source files, you must create separate framework targets for each platform and add your shared source files to each framework.

    [...]

    NOTE

    After you create a separate framework target for your WatchKit extension, you must manage the contents of that framework separately for both iOS and watchOS. If you add files to the iOS version of the framework, remember to add them to the watchOS version.


Detailed Target Info

Note: In this project, wherever you see WatchKit this usually refers to watchOS 1, while Watch refers to watchOS 2.

Lister iOS App (Lister):

  • Target: Lister
  • Product Name: Lister
  • Supported Platforms: iOS
  • Group: Lister iOS App/Main App
  • Target Dependency: Lister iOS Framework (ListerKit)
  • Target Dependency: Lister iOS Today Widget (Lister Today)
  • Target Dependency: Lister watchOS 2 App (Lister Watch App)
  • Target Dependency: Lister watchOS 1 Extension (Lister WatchKit Extension)
  • Links With: Lister iOS Framework (ListerKit.framework)
  • Embeds: Lister iOS Framework (ListerKit.framework)
  • Embeds: Lister watchOS 1 Extension (Lister WatchKit Extension.appex)
  • Embeds: Lister iOS App (Lister.appex)
  • Embeds: Lister watchOS 2 App (Lister Watch App.app)

Lister iOS Today Widget (Lister Today or Lister):

  • Target: Lister Today
  • Product Name: Lister
  • Supported Platforms: iOS
  • Group: Lister iOS App/Today Widget
  • Target Dependency: Lister iOS Framework (ListerKit)
  • Links With: Lister iOS Framework (ListerKit.framework)
  • Used by: Lister iOS App (Lister)

Lister iOS Framework (ListerKit):

  • Target: ListerKit
  • Product Name: ListerKit
  • Supported Platforms: iOS
  • Group: Lister iOS App/ListerKit Framework (iOS)
  • Group: Shared ListerKit Framework Code
  • Used by: Lister iOS App (Lister)
  • Used by: Lister iOS Today Widget (Lister Today)
  • Used by: Lister watchOS 1 Extension (Lister WatchKit Extension)

Lister watchOS 1 Extension (Lister WatchKit Extension):

  • Target: Lister WatchKit Extension
  • Product Name: Lister WatchKit Extension
  • Supported Platforms: iOS
  • Group: Lister WatchKit App/Lister WatchKit Extension
  • Group: Shared Lister WatchKit Code
  • Target Dependency: Lister iOS Framework (ListerKit)
  • Target Dependency: Lister watchOS 1 App (Lister WatchKit App)
  • Used by: Lister iOS App (Lister)

Lister watchOS 1 App (Lister WatchKit App):

  • Target: Lister WatchKit App
  • Product Name: Lister WatchKit App
  • Supported Platforms: iOS
  • Group: Lister WatchKit App/Lister WatchKit App
  • Used by: Lister watchOS 1 Extension (Lister WatchKit Extension)

Lister watchOS 2 Extension (Lister Watch Extension):

  • Target: Lister Watch Extension
  • Product Name: Lister Watch Extension
  • Supported Platforms: watchOS
  • Group: Lister Watch App/Lister Watch Extension
  • Group: Shared Lister WatchKit Code
  • Group: Lister Watch App/ListerKit Framework (watchOS)
  • Target Dependency: Lister watchOS 2 Framework (ListerKit (watchOS))
  • Links With: Lister watchOS 2 Framework (ListerWatchKit.framework)
  • Embeds: Lister watchOS 2 Framework (ListerWatchKit.framework)

Lister watchOS 2 App (Lister Watch App):

  • Target: Lister Watch App
  • Product Name: Lister Watch App
  • Supported Platforms: watchOS
  • Group: Lister Watch App/Lister Watch App
  • Used by: Lister iOS App (Lister)

Lister watchOS 2 Framework (ListerKit (watchOS) or ListerWatchKit):

  • Target: ListerKit (watchOS)
  • Product Name: ListerWatchKit
  • Supported Platforms: watchOS
  • Group: Lister iOS App/ListerKit Framework (iOS)/iCloud & Local Storage/ListUtilities.swift
  • Group: Shared ListerKit Framework Code`
  • Used by: Lister watchOS 2 Extension (Lister Watch Extension)

Things to note:

  • The Shared Lister WatchKit Code group contains code that is shared by both the watchOS 1 and watchOS 2 extensions.
  • The name of the Lister Watch App/ListerKit Framework (watchOS) group implies that it should be a part of the ListerKit (watchOS) target, but it is a part of the Lister Watch Extension target instead.
  • * The Shared ListerKit Framework Code is primarily meant to share code between the iOS, watchOS and OS X ListerKit frameworks. However, there are some files omitted from the ListerKit (watchOS) target: SegueHandlerType.swift, CheckBoxLayer.swift
  • Although the term WatchKit is primary used to refer to watchOS 1 in this project, the Lister watchOS 2 Framework's product name is ListerWatchKit, yet it is only used in watchOS 2. Note: watchOS 1 does not use a framework.
  • I have omitted OS X groups/targets/etc from the list.
like image 200
Senseful Avatar answered Nov 04 '22 15:11

Senseful