0

I want to pass data from my iOS App to my watchOS 3 app using WKWatchConnectivityRefreshBackgroundTask

How do I set up code in my watchOS App to handle the data being transferred?

For example in the past I used this iOS code to send a message from the iOS App and if there was no connection send a context:

func sendTable()
{
    let tableInfo: WatchWorkout = PhoneData().buildWatchTableData(Foundation.Date().refDays())
    let archivedTable: Data = NSKeyedArchiver.archivedData(withRootObject: tableInfo)
    if validSession
    {
        sendMessage([Keys.UpdateType : PhoneUpdateType.TableInfo.rawValue, Keys.Workout: archivedTable])
    }
    else
    {
        do
        {
            try updateApplicationContext([Keys.UpdateType : PhoneUpdateType.TableInfo.rawValue, Keys.Workout: archivedTable])
        }
        catch
        {
            print("Phone Session - error sending info: \(error)")
        }
    }
}


func sendMessage(_ message: [String : AnyObject], replyHandler: (([String : AnyObject]) -> Void)? = nil, errorHandler: ((NSError) -> Void)? = nil)
{
    print("Phone Session - phone sent message")
    session!.sendMessage(message,
        replyHandler:
        nil,
        errorHandler:
        {
            (error) -> Void in
            print("Phone Session - Error Message during transfer to Watch: \(error)")
        }
    )
}


func updateApplicationContext(_ applicationContext: [String : AnyObject]) throws
{
    print("Phone Session - phone sent context")
    if ((session) != nil)
    {
        do
        {
            try session!.updateApplicationContext(applicationContext)
        }
        catch let error
        {
            print("Phone Session - OPPS something wrong - context send failed")
            throw error
        }
    }
}

I'm not sure how to code the receipt of this data as a background task on the watch.

Can someone provide some example code or post a link? The only Apple example code is not very helpful: https://developer.apple.com/library/prerelease/content/samplecode/WatchBackgroundRefresh/Introduction/Intro.html

Thanks

Greg

4

2 回答 2

1

Quick Switch示例代码与 watchOS 3 一起更新,包括处理 WatchConnectivity 后台刷新任务的示例。

于 2016-07-24T22:11:32.017 回答
1

@ccjensen 快速切换示例代码不起作用,是吗?它会在我的 iPhone6 iOS10.0 beta3 上崩溃。我已经在上周五发送了反馈。

updateApplicationContext(_:) transferUserInfo(_:) transferCurrentComplicationUserInfo(_:) transferFile(_:metadata:) 在我的情况下,在 iPhone 端调用 永远不会触发handle(_:)监听器。

于 2016-07-25T02:58:32.213 回答