Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WatchKit Complication: get Complication data from extension delegate

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”)
        }
    }

}
like image 494
SRMR Avatar asked Oct 17 '15 19:10

SRMR


2 Answers

// 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.

like image 186
Antonioni Avatar answered Oct 05 '22 22:10

Antonioni


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.

like image 23
Alex Avatar answered Oct 05 '22 22:10

Alex