WatchKit Complication: get Complication data from extension delegate

耗尽温柔 提交于 2019-12-18 12:05:36

问题


I have all the data I need in my WatchKit Extension (passed from the iOS app).

I used the data in the WatchKit InterfaceController to fill in a table, which works perfectly.

I'm trying to figure out the best way to get that same data in my WatchKit ComplicationController.

Currently, in the InterfaceController, the data gets passed in using didReceiveUserInfo:

func session(session: WCSession, didReceiveUserInfo userInfo: [String : AnyObject]) {

    if let beachValue = userInfo["Surf"] as? String {

        places.append(Place(dataDictionary: ["Surf" : surfValue]))

    } else {
        print("Something went wrong")
    }

}

Do I need to call this same WCSession method in my ComplicationController and do the whole data grab again, or is there any easier way for me to access this same data for use in the ComplicationController?

Any help appreciated. Thanks!

EDIT:

My table function:

func makeTable() {

    // Per SO
    let myDelegate = WKExtension.sharedExtension().delegate as! ExtensionDelegate
    let accessVar = myDelegate.places
    self.rowTable.setNumberOfRows(accessVar.count, withRowType: "rows")

    for (index, evt) in accessVar.enumerate() {

        if let row = rowTable.rowControllerAtIndex(index) as? TableRowController {

            row.mLabel.setText(evt.evMat)

        } else {
            print(“No”)
        }
    }

}

回答1:


// Get the complication data from the extension delegate.
let myDelegate = WKExtension.sharedExtension().delegate as! ExtensionDelegate
var data : Dictionary = myDelegate.myComplicationData[ComplicationCurrentEntry]!

Above from Apple's Doc is just an example on storing the data you need for your complication in your extension delegate seeing as how you can access it easily as a singleton. The reference to "myComplicationData" is an example of a Dictionary and is not a parameter in the ExtensionDelegate by default.

Either set up your own class as a singleton which holds data for your watch like so:

// Access by calling:
// Model.sharedModel.modelVal1
class Model {
    static let sharedModel = Model()
    var modelVal1: Float!
    var modelVal2: String!
}

Or use the extension delegate as your singleton and add your properties to its class like below. This will allow you to access whatever variables you create in your ExtensionDelegate.

// ExtensionDelegate.swift
class ExtensionDelegate: NSObject, WKExtensionDelegate {
    var dataVar1: Float!
    var dataVar2: String!
    var myDictionary: [String: String]!
}


// ComplicationController.swift
import WatchKit

class ComplicationController: NSObject, CLKComplicationDataSource {

    func someMethod() {
        let myDelegate = WKExtension.sharedExtension().delegate as! ExtensionDelegate
        // Here is an example of accessing the Float variable 
        let accessVar = myDelegate.dataVar1
        let myDict = myDelegate.myDictionary
    }
}

Using either way will help keep your data in one spot so you can always access it from any class in your watch extension.




回答2:


Well, what I have done in my app is to set up another singelton class to be responsible for fetching and holding of data for both my Watch app and complication. But that doesnt look like the best way for me. Unfortunately I do not get the Apple code

var data : Dictionary = myDelegate.myComplicationData[ComplicationCurrentEntry]!

at all. I dont understand where this myComplicationData comes from.



来源:https://stackoverflow.com/questions/33190685/watchkit-complication-get-complication-data-from-extension-delegate

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!