Ios 在CareKit中显示视频。

Ios 在CareKit中显示视频。,ios,swift,carekit,Ios,Swift,Carekit,任何熟悉Carekit的人都可以指导我如何在care卡的说明部分显示视频。可以使用OCKCarePlanActivity类通过imageURL添加照片,但不能使用视频。 谢谢 下面的代码显示了一个开放式(故事板创建的)UIViewController如何创建一个简单的活动,将其添加到护理计划存储中,并显示标准的OCKCareCardViewController。(此结构主要来自“开始CareKit开发”)。视图控制器只有一个按钮,用于显示标准护理卡: 护理卡是完全标准的,如下所示。但是,如果单

任何熟悉Carekit的人都可以指导我如何在care卡的说明部分显示视频。可以使用OCKCarePlanActivity类通过imageURL添加照片,但不能使用视频。
谢谢

下面的代码显示了一个开放式(故事板创建的)UIViewController如何创建一个简单的活动,将其添加到护理计划存储中,并显示标准的OCKCareCardViewController。(此结构主要来自“开始CareKit开发”)。视图控制器只有一个按钮,用于显示标准护理卡:

护理卡是完全标准的,如下所示。但是,如果单击事件按钮,它将显示一个自定义视图控制器

自定义视图控制器(如下所示)内置于故事板中。自定义干预很简单——它只要求用户单击组成场景的树按钮。(您可以使用视频播放器视图,而不是三个按钮)

几乎所有代码都在OpeningViewController类中。这个类如下所示

//
//  OpeningViewController.swift
//


/*
 Context:
    This app demonstrates how to display a custom UIViewController when
    the user taps on an event in a CareCard.

    This app manages just one, hard-coded intervention activity. When
    the user taps on an event-button (usually an open circle) on the
    main Care Card then the app displays a custom UIViewController
    with three buttons for the user to tap.

 Purpose:
    This root view controller for the app shows a button that allows
    the user to request a standard OCKCareCardViewController.

    Additionally:
        1.  In the init for this view controller an instance of
            OCKCarePlanStore is created.
            (Note: this is poor design, ordinarily you would probably
            have a singleton model for the Care Plan Store. But as long
            as you don't re-initialize this view it will serve for demo
            purposes).
        2.  During viewDidLoad a convenience function is called that
            will:
            A.  Check to see if an activity with an identifier of
                "TapThreeButtons" exists
            B.  if not, it builds an activity scheduled to begin three
                days ago.
            C.  The new intervention activity is added to the store.
        3.  In the ShowCareCard IBAction:
            A.  an instance of OCKCareCardViewController is built
            B.  the delegate for the new Care Card is set to self.
            C.  the new Care Card is presented modally
        4.  An extension is used to make this ViewController conform
            to OCKCareCardViewControllerDelegate.
            A.  implements the optional method
                careCardViewController(VC:
                                didSelectButtonWithInterventionEvent:)
                THIS IS WHERE A CUSTOM DISPLAY CAN BE DISPLAYED TO
                RESPOND TO THE TAPPING OF AN EVENT.
            B.  Returns "false" from
                careCardViewController:
                               shouldHandleEventCompletionForActivity:
                This prevents CareKit from automatically processing an
                event when an event button is tapped.
 */

import UIKit
import CareKit

struct Global {
    static let tapButtonsIdentifier = "TapThreeButtons"
}

class OpeningViewController: UIViewController {

    var store: OCKCarePlanStore

    required init( coder aDecoder: NSCoder )
    {

        // 1. get a URL for the Document directory, then a URL for
              the CarePlanStore
        let fileManager = FileManager.default
        guard let documentDirectory
            = fileManager.urls( for: .documentDirectory,
                                in: .userDomainMask ).last
            else {
                let msg = "*** Error: FileManager is not returning the document directory URL *** "
                fatalError( msg )
        }
        let storeURL =
            documentDirectory
               .appendingPathComponent("TapInterventionStore")

        // 2. create the store directory if it does not already exist
        if !fileManager.fileExists( atPath: storeURL.path ) {
            try! fileManager.createDirectory(at: storeURL,
                                     withIntermediateDirectories: true,
                                     attributes: nil )
        }
        // 3. set an instance constant to point to the store
        store = OCKCarePlanStore(persistenceDirectoryURL: storeURL )
        super.init( coder: aDecoder )!
    }

    @IBAction func ShowCareCard(_ sender: UIButton) {
        let careCardViewController = createCareCardViewController()
        careCardViewController.delegate = self
        let embeddedCareCard =
            UINavigationController(rootViewController:
                careCardViewController)
        // present the CareCard modally
        self.present( embeddedCareCard, 
                      animated: true, 
                      completion: nil )
    }

    override func viewDidLoad() {
        super.viewDidLoad()

        // if no "tapThreeTimes" activity exists in CarePlanStore then
        // create one
        createActivity()
    }

    // MARK: Convenience Methods

    fileprivate func createActivity()
    {
        //  query the store to see if there already exists an activity
        //  with this identifier
        store.activity(forIdentifier: Global.tapButtonsIdentifier )
        {
            [ unowned self ]
            ( success, foundActivity, error) in
            // check that the query terminated successfully
            guard success else {
                let msg = "A query of CarePlanStore for the \( Global.tapButtonsIdentifier ) intervention activity produced an error."
                fatalError( msg )
            }
            // check to see if the activity is in CarePlanStore
            if foundActivity == nil {
                // instatiate a new activity
                let newTapButtonsActivity 
                    = self.buildNewTapButtonsActivity()
                // 4. save the new Activity instance, using the completion to check that the save worked
                self.store.add( newTapButtonsActivity )
                {
                    (success, error) in
                    guard success else {
                        let msg = "An error was returned while saving a new \( Global.tapButtonsIdentifier ) intervention activity."
                        fatalError( msg )
                    }
                }           // ends new activity closure 
            }           // ends check that activity might be nil
        }           // ends closure on query for activity in store
    }

    fileprivate func buildNewTapButtonsActivity() 
                                         -> OCKCarePlanActivity 
    {

        struct Lets {
            static let title = "Push Three Buttons"
            static let text = "Tap one of the circles below and you'll be asked to tap each of three buttons"
            static let instructions = "When you tap on an event (those circles on your Care Card) the system will show you a screen with three buttons. Tap all three. They can be tapped in any sequence but try not to tap the same button twice."
        }

        // create events by starting three days ago, 
        // with 2 events per day
        let gregorianCalendar = Calendar( identifier: .gregorian )
        guard let threeDaysAgo = 
            gregorianCalendar.date(byAdding: .day, 
                                   value: -3, 
                                   to: Date() ) 
        else {
            let msg = "Could not calculate date for three-days-before-today."
            fatalError( msg )
        }
        let startDate = 
            gregorianCalendar.dateComponents([.year, .month, .day ],
                                             from: threeDaysAgo )
        let thriceADay = 
            OCKCareSchedule.dailySchedule(withStartDate: startDate,
                                          occurrencesPerDay: 2)
        let activity = 
            OCKCarePlanActivity(
                identifier:       Global.tapButtonsIdentifier,
                 groupIdentifier: nil,
                 type:            .intervention,
                 title:           Lets.title,
                 text:            Lets.text,
                 tintColor:       nil,
                 instructions:    Lets.instructions,
                 imageURL:        nil,
                 schedule:        thriceADay,
                 resultResettable: true,
                 userInfo:        nil )
        return activity
    }

    fileprivate func createCareCardViewController() 
                          -> OCKCareCardViewController
    {
        let viewController =
            OCKCareCardViewController(carePlanStore: self.store )
        // Setup the controllers title
        viewController.title = 
            NSLocalizedString("Tap Test App",
            comment: "This app shows CareKit users how to add a custom interface to Care Card")

        return viewController

    }
}

extension OpeningViewController: OCKCareCardViewControllerDelegate {
    /**
     prevents CareKit from automatically coloring-in an event symbol when tapped
     - parameter viewController: the view controller that displays the events
     - parameter shouldHandleEventCompletionForActivity: the activity associated with the tapped event
     */
    func careCardViewController( 
             _ viewController: OCKCareCardViewController,
             shouldHandleEventCompletionFor interventionActivity:
                 OCKCarePlanActivity )
        -> Bool
    {
        return false
    }

    /**
     modally presents a custom UIViewController when the user taps an event button on the Care Card
     - parameter viewController: the OCKCareCardViewController master/detail that presents the event
     - parameter didSelectRowWithInterventionActivity: the user selected OCKCarePlanActivity tapped by the user
     - Note in this implementation the new detail view controller will be an instance of ZCCareCardDetailViewController showing the "medication" and an associated medication image
     */
    func careCardViewController( _ viewController: OCKCareCardViewController,
                                 didSelectButtonWithInterventionEvent interventionEvent: OCKCarePlanEvent )
    {
        // In this simplistic app, assume that a user-tap on an already completed event always
        // means that the user just wants to "clear" the event
        if interventionEvent.state == .completed {
            store.update( interventionEvent,
                          with: interventionEvent.result,
                          state: .notCompleted )
            {
                ( success, event, error ) in
                if success {
                    print( "toggled the event's state")
                } else {
                    print( "failed to toggle event's state")

                }
            }
        } else {

            // 1.  Create the custom detail view controller
            let storyBoard = UIStoryboard( name: "Main", bundle: nil )
            let tapThreeButtonsViewController =
                storyBoard.instantiateViewController(
                    withIdentifier: Global.tapButtonsIdentifier ) as!
                        TapThreeButtonsViewController

            // 2 set up the data model for the ViewController
            tapThreeButtonsViewController.carePlanEvent 
                = interventionEvent
            tapThreeButtonsViewController.store = store

            // 3.  Embed in a UINavigationController
            let embeddedViewController = 
                UINavigationController( 
                    rootViewController: tapThreeButtonsViewController )
            // 4.  modally present the custom detail view controller
            viewController.present(embeddedViewController, 
                                   animated: true, 
                                   completion: nil )
        }
    }
}
自定义视图控制器的代码不是特别有趣(或者,就此而言,调试得非常好)!无论如何,我都要展示它,因为我认为这就是如何保留自定义干预产生的细节数据

    //
    //  TapThreeButtonsViewController.swift
    //  CustomCareCard
    //
    /*
     Context:
        This app is meant to show how a custom view controller can be
        displayed in the middle of a CareKit app.

     Purpose:
        This class defines the custom view controller built in the
        storyboard. It has these elements:
            1.  A Done navigation button to indicate that the intervention
                activity has been completed.
            2.  A Cancel navigation button to indicate that the user is not
                interested in completing the activity.
            3.  A label giving the user instructions to tap on three
                buttons (kind of silly, really, but the point here is to
                show how to display a custom interface not to solve a real
                medical problem).
            4.  Three buttons labeled "Button One", "Button Two", 
                "Button Three" for the user to tap.

        The only significant content in this view controller comes in the
"doneButton" action. The code there shows you how to save a basic "value"
to record the user's actions. Here the "value" is equal to the total number
of times the threee buttons were tapped. Additionally, the code will save
the number of times each individual button was tapped, using the "userInfo"
property of the OCKCarePlanEventResult object.  Detail data of this sort
could be useful for formulating OCKInsightViewController reports.
     */


    import UIKit
    import CareKit

    class TapThreeButtonsViewController: UIViewController {

        // MARK: data model

        public var carePlanEvent: OCKCarePlanEvent!
        public var store: OCKCarePlanStore!


        fileprivate var tapCount = [ "Button One":   0,
                                     "Button Two":   0,
                                     "Button Three": 0
                                   ]

        // MARK: actions


        /**
         when the user taps any of the three main buttons this logic adds to the count of that button
         - parameter sender: the UIButton that was tapped (Button One, Button Two or Button Three)
         */
        @IBAction func tapButton(_ sender: UIButton) {
            let buttonName = sender.currentTitle!
            tapCount[ buttonName ]! += 1
        }

        /**
         saves the total number of button taps as the event "value", but also saves the counts-for-each-button in "userInfo"
         - parameter sender: the UIButtonItem that initiated the action
         */
        @IBAction func doneButton(_ sender: UIBarButtonItem!) {
            var totalTaps = 0
            var codableTapCount: Dictionary< String, NSNumber > = [ : ]
            // CAREKIT reminder: "values" are stored as strings. The
            //         userInfo dictionary requires that the
            //         the "value" follow NSCoder requirements.
            for (button, taps ) in tapCount {
                totalTaps += taps
                codableTapCount[ button ] = NSNumber( value: taps )
            }
            let results = 
                OCKCarePlanEventResult(
                    valueString: totalTaps.description,
                    unitString: "taps",
                    userInfo: codableTapCount )
            store.update( carePlanEvent, 
                          with: results, 
                          state: .completed ) {
                // use completion event simply to check that the Care
                // Plan Store updated successfully
                ( success, event, error ) in
                if !success {
                    fatalError("The Care Plan Store was not updated with the most-recent results from the Tap Buttons test.")
                }
            }
            self.dismiss(animated: true)
        }

        /**
         Dismisses the custom view controller without making changes to the Care Plan Store
         */
        @IBAction func cancelButton(_ sender: AnyObject) {
            self.dismiss(animated: true)
        }    
    }
//
//点击ThreeButtonSViewController.swift
//客户卡
//
/*
背景:
此应用程序旨在展示如何使用自定义视图控制器
显示在CARIKIT应用程序的中间。
目的:
此类定义在中构建的自定义视图控制器
故事板。它具有以下要素:
1.完成导航按钮,指示干预
活动已经完成。
2.一个“取消导航”按钮,用于指示用户未登录
对完成活动感兴趣。
3.指示用户点击三个按钮的标签
按钮(真的有点傻,但这里的重点是
演示如何显示自定义界面,而不是解决实际问题
医疗问题)。
4.三个按钮标记为“按钮一”,“按钮二”,
“按钮三”供用户点击。
此视图控制器中唯一重要的内容是
“doneButton”动作。那里的代码向您展示了如何保存基本“值”
记录用户的操作。这里的“值”等于总数
三个按钮被点击的次数。此外,代码将保存
使用“用户信息”点击每个按钮的次数
OCKCarePlanEventResult对象的属性。这类详细数据
可能有助于制定InsightViewController报告。
*/
导入UIKit
进口护理包
类TapThreeButtonsViewController:UIViewController{
//马克:数据模型
公共var carePlanEvent:OCKCarePlanEvent!
公共var商店:OCKCarePlanStore!
fileprivate var tapCount=[“按钮一”:0,
“按钮二”:0,
“按钮三”:0
]
//马克:行动
/**
当用户点击三个主按钮中的任何一个时,该逻辑会增加该按钮的计数
-参数发送器:点击的UIButton(按钮一、按钮二或按钮三)
*/
@iAction func tapButton(\发送方:UIButton){
让buttonName=sender.currentTitle!
点击次数[按钮名称]!+=1
}
/**
将按钮点击总数保存为事件“值”,但也将每个按钮的计数保存在“用户信息”中
-参数发送者:启动操作的UIButtonItem
*/
@iAction func doneButton(uu发送方:UIBarButtonItem!){
var totalTaps=0
var codableTapCount:Dictionary=[:]
//CAREKIT提醒:“值”存储为字符串
//userInfo字典要求
//“值”遵循NSCoder要求。
用于tapCount中的(按钮、轻触){
总抽头+=抽头
codableTapCount[按钮]=NSNumber(值:抽头)
}
让结果=
奥克卡莱文特雷索(
valueString:totalTaps.description,
unitString:“点击”,
userInfo:codableTapCount)
store.update(carePlanEvent,
结果,,
状态:。已完成){
//仅使用完成事件来检查
//计划存储已成功更新
(成功、事件、错误)
如果!成功{
fatalError(“未使用点击按钮测试的最新结果更新护理计划存储。”)
}
}
self.disclose(动画:true)
}
/**
在不更改保养计划存储的情况下解除自定义视图控制器
*/
@iAction func cancelButton(\uSender:AnyObject){
self.disclose(动画:true)
}    
}

下面的代码显示了一个开放式(故事板创建的)UIViewController如何创建一个简单的活动,将其添加到护理计划商店,并显示标准的OCKCareCardViewController。(此结构主要来自“开始CareKit开发”)。视图控制器只有一个按钮,用于显示标准护理卡:

护理卡是完全标准的,如下所示。但是,如果单击事件按钮,它将显示一个自定义视图控制器

自定义视图控制器(如下所示)内置于故事板中。海关情报