Apple Watch, WatchKit Extension and main application

前端 未结 2 1037
失恋的感觉
失恋的感觉 2020-12-09 00:13

There is main application with logic and we extend app to Apple Watch.

After adding target xCode creates 2 more applications: extension with code and watch kit appl

2条回答
  •  爱一瞬间的悲伤
    2020-12-09 00:56

    At current state of Apple Watch Extension:

    • You can share information between iOS main appliation and WatchKit Extension. Use App Groups and NSUserDefaults to access the shared information objects.

    • You can not execute code from your iOS app which is trigged from actions on the Apple Watch.

    At least not yet.

    EDIT: As of Xcode 6.2 Beta 2

    It is now possible to communicate with the parent iOS app from Apple Watch.

    In WatchKit Extension call the parent application via openParentAppentApplicion. One can pass a value dictionary to the parent application and the parent application can return a value dictionary.

    Watchkit Extension:

    // Call the parent application from Apple Watch
    
    // values to pass
    let parentValues = [
        "value1" : "Test 1",
        "value2" : "Test 2"
    ]
    
    WKInterfaceController.openParentApplication(parentValues, reply: { (replyValues, error) -> Void in
        println(replyValues["retVal1"])
        println(replyVaiues["retVal2"])
    })
    

    iOS App:

    // in AppDelegate.swift
    func application(application: UIApplication!, handleWatchKitExtensionRequest userInfo: [NSObject : AnyObject]!, reply: (([NSObject : AnyObject]!) -> Void)!) {
        // retrieved parameters from Apple Watch
        println(userInfo["value1"])
        println(userInfo["value2"])
    
        // pass back values to Apple Watch
        var retValues = Dictionary()
    
        retValues["retVal1"] = "return Test 1"
        retValues["retVal2"] = "return Test 2"
    
        reply(retValues)
    }
    

提交回复
热议问题