Sharing code between iOS and tvOS
To share code between our iOS and tvOS app, we will need to let Xcode know the Swift files that need to be shared across different targets. This is easy to do by simply checking the targets a Swift file should be part of when compiling and building the product:
Select one of the Swift files inside the ShoppingList
app folder, and you should see ShoppingList
and ShoppingListTV
under Target Membership
inside File Inspector
on the right side of the screen. Check the ShoppingListTV
target. Now, go through the remaining Swift files inside the ShoppingList iOS app, except for AppDelegate.swift
, and check the ShoppingListTV
target, as we will share all of this code with the tvOS app. Once you are done, try building the app. You should encounter a bunch of errors, as our code will not work with tvOS just out-of-the-box. We will need to tweak some lines of code, as some APIs available in iOS are not available in tvOS. One such example is the usage of prefersLargeTitles...