Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/96.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 使用Watch Connectivity调用updateApplicationContext的最佳位置?_Ios_Swift_Watchkit_Apple Watch_Watchconnectivity - Fatal编程技术网

Ios 使用Watch Connectivity调用updateApplicationContext的最佳位置?

Ios 使用Watch Connectivity调用updateApplicationContext的最佳位置?,ios,swift,watchkit,apple-watch,watchconnectivity,Ios,Swift,Watchkit,Apple Watch,Watchconnectivity,一些好的博客文章详细介绍了手表的连接,并使用了一些简单的应用程序示例,当你点击iPhone上的UI时,这些示例会向手表发送数据 我的应用程序只是列出iPhone应用程序中的数据,所以我不需要立即发送数据,我只是想在应用程序加载或进入后台时发送它…为此,我在didFinishLaunching和didEnterBackground中进行了updateApplicationContext…但是我手表界面控制器中的数据源代理非常注意被触发…特别是只加载了一眼模拟器和从不在设备上运行。是否有更好的时间和

一些好的博客文章详细介绍了手表的连接,并使用了一些简单的应用程序示例,当你点击iPhone上的UI时,这些示例会向手表发送数据

我的应用程序只是列出iPhone应用程序中的数据,所以我不需要立即发送数据,我只是想在应用程序加载或进入后台时发送它…为此,我在
didFinishLaunching
didEnterBackground
中进行了
updateApplicationContext
…但是我手表界面控制器中的数据源代理非常注意被触发…特别是只加载了一眼模拟器和从不在设备上运行。是否有更好的时间和地点来推送信息

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
    WatchSessionManager.sharedManager.startSession()      
    do {
         try WatchSessionManager.sharedManager.updateApplicationContext(["peopleDict" : peopleDict])                                        
    } catch {
        print(error)
    }
     return true
}

func applicationDidEnterBackground(application: UIApplication) {     
     do {
         try WatchSessionManager.sharedManager.updateApplicationContext(["peopleDict" : peopleDict])                                      
     } catch {
         print(error)
     }
}
下面是我的
WatchSessionManager
我曾经在我的
extensionelegate
applicationdidfishunching
中调用
activiateSession

import WatchConnectivity

protocol DataSourceChangedDelegate {
    func dataSourceDidUpdate(dataSource: DataSource)
}


class WatchSessionManager: NSObject, WCSessionDelegate {

    static let sharedManager = WatchSessionManager()
    private override init() {
        super.init()
    }

    private var dataSourceChangedDelegates = [DataSourceChangedDelegate]()

    private let session: WCSession = WCSession.defaultSession()

    func startSession() {
        session.delegate = self
        session.activateSession()
    }

    func addDataSourceChangedDelegate<T where T: DataSourceChangedDelegate, T: Equatable>(delegate: T) {
        dataSourceChangedDelegates.append(delegate)
    }

    func removeDataSourceChangedDelegate<T where T: DataSourceChangedDelegate, T: Equatable>(delegate: T) {
        for (index, indexDelegate) in dataSourceChangedDelegates.enumerate() {
            if let indexDelegate = indexDelegate as? T where indexDelegate == delegate {
                dataSourceChangedDelegates.removeAtIndex(index)
                break
            }
        }
    }
}

// MARK: Application Context
// use when your app needs only the latest information
// if the data was not sent, it will be replaced
extension WatchSessionManager {

    // Receiver
    func session(session: WCSession, didReceiveApplicationContext applicationContext: [String : AnyObject]) {

        dispatch_async(dispatch_get_main_queue()) { [weak self] in
            self?.dataSourceChangedDelegates.forEach { $0.dataSourceDidUpdate(DataSource(data: applicationContext))}
        }

    }
}
导入WatchConnectivity
协议数据源更改数据源{
func数据源IDUpdate(数据源:数据源)
}
类WatchSessionManager:NSObject,wcSessionLegate{
静态let sharedManager=WatchSessionManager()
私有重写init(){
super.init()
}
私有变量datasourcechangedelegates=[datasourcechangedelegate]()
私有let会话:WCSession=WCSession.defaultSession()
func startSession(){
session.delegate=self
会话.激活会话()
}
func addDataSourceChangedDelegate(委托人:T){
dataSourceChangedDelegates.append(委托)
}
func removeDataSourceChangedDelegate(委托人:T){
对于dataSourceChangedDelegates.enumerate()中的(index,indexDelegate){
如果让indexDelegate=indexDelegate作为?T,其中indexDelegate==委托{
dataSourceChangedDelegates.removeAtIndex(索引)
打破
}
}
}
}
//标记:应用程序上下文
//当应用程序只需要最新信息时使用
//如果数据未发送,则会被替换
扩展监视会话管理器{
//接受者
func会话(会话:WCSession,didReceiveApplicationContext applicationContext:[字符串:AnyObject]){
dispatch_async(dispatch_get_main_queue()){[weak self]in
self?.datasourcechangedelegates.forEach{$0.datasourcediddupdate(DataSource(数据:applicationContext))}
}
}
}

As
updateApplicationContext
只存储最新的应用程序上下文,您可以随时更新它。这只手表只能得到最新的数据。没有包含旧上下文的队列

在监视端,激活会话和设置WCSessionDelegate的最安全位置是
ExtensionDelegate
init
方法:

class ExtensionDelegate: NSObject, WKExtensionDelegate {

    override init() {
        super.init()
        WatchSessionManager.sharedManager.startSession()
    }
    ...
}

您的一瞥不会更新,因为当显示一瞥时,
applicationdFinishLaunching
没有被调用(因为只有一瞥启动时手表应用程序没有启动)

谢谢,也许我误解了API。。。我认为updateApplicationContext会将数据发送到手表,当手表上的会话被激活时,dataSourceDelegate会观察更新的上下文并使其可用?换一种方式说,我怎样才能让更新的信息进入我的视线?我在ExtensionLegate中执行activateSession。不,您不会误解API的工作原理。您在ExtensionLegate中的什么位置激活会话?在
ApplicationIDFinishLaunching
中,还是像我在回答中建议的那样重写
init
?我将我的WatchSessionManager类添加到我的问题中,我更新了我的回答,以便它从ExtensionLegate的init方法调用您的WatchSessionManager。这仍然不起作用吗?所以将startSession从ApplicationIDFinishLaunching中取出,并仅将其放入init中?