如何将 iOS 应用程序扩展到 tvOS

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32489992/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 07:34:31  来源:igfitidea点击:

How to extend iOS app to tvOS

iosapple-tvtvos

提问by BlackM

I have an iOS app that I need to extend to tvOS. All the information that I found are explaining how to start from scratch! Is there any way to extend my app to tvOS or I should start a new project with it?

我有一个需要扩展到 tvOS 的 iOS 应用程序。我找到的所有信息都在解释如何从头开始!有什么方法可以将我的应用程序扩展到 tvOS 还是我应该用它开始一个新项目?

Update1:My question is: How to extend my existing project to support tvOS without building it from scratch?

更新 1:我的问题是:如何扩展我现有的项目以支持 tvOS,而无需从头开始构建?

Update2:Jess Bower point on Apple's website:

UPDATE2:在苹果的网站杰斯鲍尔点:

Empower customers to enjoy their favorite apps on both iOS and the new Apple TV with a single purchase by enabling universal purchase for your app on the App Store.

通过在 App Store 上为您的应用程序启用通用购买,让客户只需一次购买即可在 iOS 和新的 Apple TV 上享受他们喜爱的应用程序。

Which means that we need to create a new bundle on our existing project and enable "universal" purchase so it will count as one app on App Store.

这意味着我们需要在我们现有的项目上创建一个新的捆绑包并启用“通用”购买,以便它在 App Store 上算作一个应用程序。

回答by Aaron Wasserman

The tvOS SDK is based off of iOS, but is not interchangeable. Unlike when the first iPad was released, the new Apple TV will not be capable of running iOS apps.

tvOS SDK 基于 iOS,但不可互换。与第一代 iPad 发布时不同,新的 Apple TV 将无法运行 iOS 应用程序。

The AppStore for the TV will only include apps built specifically for tvOS.

适用于电视的 AppStore 将仅包含专为 tvOS 构建的应用程序。

For any iOS developers looking to create apps for Apple TV, I'd recommend checking out the new documentation page: https://developer.apple.com/library/content/documentation/General/Conceptual/AppleTV_PG/index.html#//apple_ref/doc/uid/TP40015241-CH12-SW1

对于任何希望为 Apple TV 创建应用程序的 iOS 开发人员,我建议查看新的文档页面:https: //developer.apple.com/library/content/documentation/General/Conceptual/AppleTV_PG/index.html#/ /apple_ref/doc/uid/TP40015241-CH12-SW1

Specifically, check out the Inherited iOS Frameworkssection to give you a sense of what will work out of the box from your existing iOS projects.

具体来说,请查看继承的 iOS 框架部分,让您了解现有 iOS 项目中哪些可以开箱即用。

回答by Maciek Czarnik

In Xcode 7.1 (which introduces tvOS SDK) you can add a tvOS target as any other (File -> New -> Target... -> tvOS -> ...) and it supports both Objective-C and Swift, so yes - it's possible to share code between your iOS and tvOS app, you just need to check your source target membership and enable it on your tvOS target. To extend the purchases across iOS and tvOS app we should use Universal Purchases.

在 Xcode 7.1(引入了 tvOS SDK)中,您可以像添加任何其他目标一样添加 tvOS 目标(文件 -> 新建 -> 目标... -> tvOS -> ...),它同时支持 Objective-C 和 Swift,所以是的- 可以在您的 iOS 和 tvOS 应用程序之间共享代码,您只需要检查您的源目标成员资格并在您的 tvOS 目标上启用它。要扩展跨 iOS 和 tvOS 应用程序的购买,我们应该使用通用购买。

回答by richy

Took me a little while to find all the things needed to change but this list should cover it.

我花了一点时间才找到需要更改的所有内容,但此列表应该涵盖它。

  1. click iOS target and duplicate
  2. change base sdk of new tvOS target to tvOS latest
  3. make copy of info.plist and have tvOS point to that one
  4. make all the tvOS icons and launch images
  5. set TARGETED_DEVICE_FAMILY to 3 for the tvOS build settings
  6. add any new tvOS specific versions of code e.g. without shouldAutorotate, prefersStatusBarHidden etc.
  1. 单击 iOS 目标并复制
  2. 将新 tvOS 目标的基础 sdk 更改为最新的 tvOS
  3. 复制 info.plist 并让 tvOS 指向那个
  4. 制作所有 tvOS 图标并启动图像
  5. 将 tvOS 构建设置的 TARGETED_DEVICE_FAMILY 设置为 3
  6. 添加任何新的 tvOS 特定版本的代码,例如没有 shouldAutorotate、prefersStatusBarHidden 等。

回答by Simon Tillson

I also believe that adding a new target for tvOS is the way to go, especially if you have lots of objective-c or swift code to share between projects.

我也相信为 tvOS 添加一个新目标是可行的方法,特别是如果您有大量的 Objective-c 或 swift 代码要在项目之间共享。

For those instances where there might be some tvOS-unsupported types in your shared code, I have used these preprocessor symbols to provide alternate code snippets for tvOS:

对于那些共享代码中可能存在某些 tvOS 不受支持的类型的实例,我使用这些预处理器符号为 tvOS 提供替代代码片段:

#if TARGET_OS_IOS
// iOS-specific code
#elif TARGET_OS_TV
// tvOS-specific code
#endif

回答by Vacca

Just to list out some limitations and challenges:
1. There is no persistent local storage for apps on Apple TV. Data must be stored on iCloud.

2. The maximum size of an Apple TV app is limited to 200MB. On-demand resources (app contents that are hosted on the App Store) should be used. Benefits are smaller app size and lazy loading of app resources.

3. The UI is drastically different. Human Interface Guidelines must be followed as per the doc.

4. Creating a Client-Server App using JavaScript and TVML framework.

5. Controlling the UI touch focus. UIFocusEnvironment controls focus-related behavior for a branch of the view hierarchy. UIViewController conforms to UIFocusEnvironment protocol.

6. Creating Parallax Artwork You have to create a LSR image with Xcode and then use terminal to create a LCR image. A UIImage object can display a LCR image correctly.

仅列出一些限制和挑战:
1. Apple TV 上的应用程序没有持久的本地存储。数据必须存储在 iCloud 上。

2. Apple TV 应用程序的最大大小限制为 200MB。应使用按需资源(托管在 App Store 上的应用程序内容)。好处是更小的应用程序大小和应用程序资源的延迟加载。

3. 用户界面完全不同。必须按照文档遵循人机界面指南

4. 使用 JavaScript 和 TVML 框架创建客户端-服务器应用程序。

5.控制UI触摸焦点。UIFocusEnvironment 控制视图层次结构分支的焦点相关行为。UIViewController 符合 UIFocusEnvironment 协议。

6. 创建视差图 您必须使用 Xcode 创建 LSR 图像,然后使用终端创建 LCR 图像。UIImage 对象可以正确显示 LCR 图像。

回答by Dijam

  1. A new target has to be added for tvOS. There are two ways to do that

    • Add a new target through File > New > File...> tvOS Target.
    • Duplicate an existing iOS target and change TARGETED_DEVICE_FAMILY to 3 and "Supported Platforms" to tvOS in "Build Settings"
  2. Pods need to be added to the tvOS target using pod install. There could be a different list of pods that you can/want to use in tvOS. Pods for different targets can be separated in Podfile using:

    target 'iOS TARGET NAME' do
    pod 'podname', :git => 'https://github.com/name.git'
    end
    
    target 'tvOS TARGET NAME' do
    pod 'podname', :git => 'https://github.com/name.git'
    end
    
  3. Most Pods at the moment do not support tvOS. For those Pods, here are the steps to make them work in your project:

    • Clone the git repo on your local disk
    • If a version of the pod is being used in another target (iOS target), change the name, otherwise CocoaPods will complain: e.g. RestKit --> RestKitTV and use :path In Podfile to point to the location of the cloned repo:

      pod 'RestKitTV', :path => 'Other/RestKitTV'
      
    • Update the podspec file in the cloned repo:

      • Modify the name to be compatible with the new name
      • Change the platform to tvOS or add tvOS to the list of supported platforms

         Pod::Spec.new do |s|
         ..
         s.platform = :tvos
         ..
         end
        

        OR

         Pod::Spec.new do |s|
         ..
         s.tvos.deployment_target = '9.0'
         s.tvos.exclude_files = 'framework/Source/Mac', ....
         s.tvos.frameworks   = ['OpenGLES', 'CoreMedia', 'QuartzCore']
         ..
         end
        
  4. Add files to the target:

    • Add source code (.m files) to "Compile Sources" of "Build Phases" for the target
    • Add images to "Copy Bundle Resources"
    • Add frameworks to "Link Binary with Libraries". Note that not all frameworks are compatible with tvOS
  5. Use TARGET_OS_TV and TARGET_OS_IOS macros to separate tvOS non-compatible code

    #if !TARGET_OS_TV
        *iOS only code*
    #else
        *tvOS only code*
    #end
    
  1. 必须为 tvOS 添加一个新目标。有两种方法可以做到这一点

    • 通过 File > New > File...> tvOS Target 添加新目标。
    • 复制现有的 iOS 目标并将 TARGETED_DEVICE_FAMILY 更改为 3,并将“支持的平台”更改为“构建设置”中的 tvOS
  2. Pod 需要使用pod install. 您可以/想要在 tvOS 中使用不同的 pod 列表。可以使用以下命令在 Podfile 中分离不同目标的 Pod:

    target 'iOS TARGET NAME' do
    pod 'podname', :git => 'https://github.com/name.git'
    end
    
    target 'tvOS TARGET NAME' do
    pod 'podname', :git => 'https://github.com/name.git'
    end
    
  3. 目前大多数 Pod 不支持 tvOS。对于这些 Pod,以下是使它们在您的项目中工作的步骤:

    • 克隆本地磁盘上的 git repo
    • 如果某个版本的 pod 正在另一个目标(iOS 目标)中使用,请更改名称,否则 CocoaPods 会报错:例如 RestKit --> RestKitTV 并在 Podfile 中使用 :path 指向克隆的 repo 的位置:

      pod 'RestKitTV', :path => 'Other/RestKitTV'
      
    • 更新克隆的 repo 中的 podspec 文件:

      • 修改名称以与新名称兼容
      • 将平台更改为 tvOS 或将 tvOS 添加到支持的平台列表中

         Pod::Spec.new do |s|
         ..
         s.platform = :tvos
         ..
         end
        

        或者

         Pod::Spec.new do |s|
         ..
         s.tvos.deployment_target = '9.0'
         s.tvos.exclude_files = 'framework/Source/Mac', ....
         s.tvos.frameworks   = ['OpenGLES', 'CoreMedia', 'QuartzCore']
         ..
         end
        
  4. 将文件添加到目标:

    • 将源代码(.m 文件)添加到目标的“构建阶段”的“编译源”
    • 将图像添加到“复制捆绑资源”
    • 将框架添加到“Link Binary with Libraries”。请注意,并非所有框架都与 tvOS 兼容
  5. 使用 TARGET_OS_TV 和 TARGET_OS_IOS 宏来分离 tvOS 不兼容的代码

    #if !TARGET_OS_TV
        *iOS only code*
    #else
        *tvOS only code*
    #end
    

回答by Danoli3

+Simon-Tillson answer is correct, however I had some backwards compatibility issues with iOS 8.1 and below SDK's where TARGET_OS_IOS was not defined (for older Xcode versions)

+Simon-Tillson 答案是正确的,但是我在未定义 TARGET_OS_IOS 的 iOS 8.1 及以下 SDK 中遇到了一些向后兼容性问题(对于较旧的 Xcode 版本)

The following code fixes that and works the same for iOS 9.0/9.1 SDK + and previous 8.1 and less SDKS.

以下代码修复了该问题,并且对 iOS 9.0/9.1 SDK + 和之前的 8.1 及更低版本的 SDKS 也同样有效。

#if TARGET_OS_IOS || (TARGET_OS_IPHONE && !TARGET_OS_TV)
// iOS-specific code
#elif TARGET_OS_TV
// tvOS-specific code
#endif

回答by Satoshi Nakajima

In case of my project, I simply added a new target to the existing iOS project, and modified some code appropriately (using #if os(tvOS/iOS) in a few areas). I am now able to run the same app either on iOS devices or Apple TV.

对于我的项目,我只是向现有的 iOS 项目添加了一个新目标,并适当修改了一些代码(在一些区域使用 #if os(tvOS/iOS))。我现在可以在 iOS 设备或 Apple TV 上运行相同的应用程序。

The only framework missing in tvOS was WebKit (which was necessary to render rich text), and I needed to come up with an alternative mechanism.

tvOS 中唯一缺少的框架是 WebKit(它是呈现富文本所必需的),我需要想出一种替代机制。

I am going to open source this project soon (before the end of October), so that other people can take a look.

近期(10月底之前)我要开源这个项目,让其他人可以看看。

回答by Pavlos

Don't forget to change the Base SDK into TVos 9.x in the build settings. It's necessary for the Tv simulator to show up

不要忘记在构建设置中将 Base SDK 更改为 TVos 9.x。电视模拟器需要出现