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

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

问题描述

我从没在WatchOS5中工作过,想为AppleWatch开发水平并发症(模块化大),例如"Heart Rate".我的想法是,我将以其他方式显示心率数据.现在,我想将复杂性部署在开发监视上.

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应用程序之前需要付出多少努力.我看到

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应用程序以外的任何内容管理并发症的逻辑,或者我可以在没有视图控制器?是-观看应用程序具有计算限制
  • 我是否通过以下方式控制并发症的出现在资产文件夹中添加一些内容(它有一堆图形插槽)?请参见下文-它是资产文件夹和占位符

修改上面的示例以创建在手表上显示的占位符图像(当您在修改屏幕布局的同时选择复杂功能时)

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天全站免登陆