如何创建apple watchOS5 并发症? [英] How to create apple watchOS5 complication?

查看:27
本文介绍了如何创建apple watchOS5 并发症?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从未在 WatchOS5 中工作过,想为 AppleWatch 开发横向复杂功能(模块化大),例如心率".这个想法是我会以不同的方式显示心率数据.现在我想在开发手表上部署复杂功能.

I've never worked in WatchOS5 and want to develop a horizontal complication (Modular large) for AppleWatch, like "Heart Rate". The idea is that I would display heart rate data in a different way. Right now I want to deploy the complication on development watch.

我创建了一个新项目,并添加了一个复杂化"复选框.我看到这添加了一个带有时间线配置占位符的并发症控制器.

I have created a new project with a checkbox for "complication" added. I see that this added a complications controller with timeline configuration placeholders.

还有一个带有一堆空白屏幕的故事板.我不确定在部署 Apple Watch 应用程序之前需要付出多少努力.我看到 this Apple 文档,但它没有描述如何布局我的复杂功能.某些部分似乎缺少链接.

There is also an storyboard with a bunch of empty screens. I'm not sure as to how much effort I need to put into an apple watch app before I can deploy it. I see this Apple doc, but it does not describe how to layout my complication. Some section seem to have missing links.

  • 我能否只提供一种复杂功能(大型横向 - 模块化大型)
  • 除了管理应用程序之外,我是否需要提供任何 iPhone 应用程序内容?复杂的逻辑,或者我可以在没有视图控制器的情况下逃脱吗?
  • 我是否可以通过向资产文件夹添加一些内容来控制我的复杂功能的外观(它有一堆图形插槽)?

抱歉,一个完整的初学者项目,我还没有看到专门针对 watch OS 5 的横向复杂功能的项目

Sorry for a complete beginner project, I have not seen a project focusing specifically on the horizontal complication for watch OS 5

推荐答案

这是 Apple 的 示例如何与 Apple Watch 应用进行通信.您需要仔细阅读自述文件大约 25 次才能更改该项目中的所有应用组标识符.

Here's an example by Apple of how to communicate with the apple watch app. You need to painstakingly read the readme about 25 times to get all the app group identifiers changed in that project.

  • 手表应用看不到您的主要手机应用资产
  • 您的手表故事板资产进入 WatchKit 目标
  • 以编程方式访问的资产进入手表扩展目标
  • Your main phone app assets are not visible to the watch app
  • Your watch storyboard assets go in WatchKit target
  • Your programmatically accessed assets go into the watch extension target

原始答案:

  • 我可以只提供一种复杂的样式吗(大横向 -模块化大)-
  • 除此之外,我还需要提供任何 iPhone 应用程序内容吗?管理复杂化逻辑,或者我可以在没有视图控制器?是 - 手表应用对它们施加了计算限制
  • 我是否可以通过以下方式控制并发症的出现?在assets文件夹中添加一些东西(它有一堆图形插槽)?见下文 - 既是资产文件夹又是占位符

修改上面的例子,创建一个显示在手表上的占位符图片(当你在修改屏幕布局的同时选择一个复杂功能时)

Modify the example above to create a placeholder image displayed on the watch (when you are selecting a complication while modifying the screen layout)

func getPlaceholderTemplate(for complication: CLKComplication, withHandler handler: @escaping (CLKComplicationTemplate?) -> Void) {
// Pass the template to ClockKit.
if complication.family == .graphicRectangular {

    // Display a random number string on the body.
    let template = CLKComplicationTemplateGraphicRectangularLargeImage()
    template.textProvider = CLKSimpleTextProvider(text: "---")
    let image = UIImage(named: "imageFromWatchExtensionAssets") ?? UIImage()
    template.imageProvider = CLKFullColorImageProvider(fullColorImage: image)

    // Pass the entry to ClockKit.
    handler(template)
}else {
    handler(nil);
    return
}

}

向手表发送小包(不会发送图片!)

sending small packets to the watch (will not send images!)

func updateHeartRate(with sample: HKQuantitySample){

    let context: [String: Any] = ["title": "String from phone"]
    do {
        try WCSession.default.updateApplicationContext(context)
    } catch {
        print("Failed to transmit app context")
    }
}

传输图像和文件:

func uploadImage(_ image: UIImage, name: String, title: String = "") {

    let data: Data? = UIImagePNGRepresentation(image)

    do {
        let fileManager = FileManager.default
        let documentDirectory = try fileManager.url(for: .cachesDirectory,
                                                    in: .userDomainMask,
                                                    appropriateFor:nil,
                                                    create:true)
        let fileURL = try FileManager.fileURL("\(name).png")

        if fileManager.fileExists(atPath: fileURL.path) {
            try fileManager.removeItem(at: fileURL)
            try data?.write(to: fileURL, options: Data.WritingOptions.atomic)
        } else {
            try data?.write(to: fileURL, options: Data.WritingOptions.atomic)
        }

        if WCSession.default.activationState != .activated {
            print("session not activated")
        }
        fileTransfer = WCSession.default.transferFile(fileURL, metadata: ["name":name, "title": title])

    }
    catch {
        print(error)
    }
    print("Completed transfer \(name)")
}

这篇关于如何创建apple watchOS5 并发症?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆