Apple Watch:这是让Apple Watch App抓取新数据的好方法吗?
Apple Watch: Is this a good way to allow apple watch app to grab new data?
目前,我正在构建一个类似于邮件应用程序的苹果手表应用程序。我正在将 API 密钥从我的 iOS 应用程序发送到我的 Apple Watch 应用程序,并使用 API 密钥从我的 Apple Watch 发出请求。
这就是我将 API 密钥发送到我的 Apple Watch 应用程序的方式。
NSDictionary *applicationDict = @{@"apiKey" : apiKey };
if ([WCSession defaultSession].reachable) {
[[WCSession defaultSession] sendMessage:applicationDict replyHandler:^(NSDictionary *replyHandler) {
} errorHandler:^(NSError *error) {
}];
} else {
[session updateApplicationContext:applicationDict error:nil];
}
这就是我将 API 键存储在我的 Apple Watch 应用的用户默认设置中的方式。我保存 API 键。然后,我使用 API 键发出请求。如果用户退出 iPhone 应用程序,我将删除默认值中的 API 键。
// receive apiKey if watch app is in the foreground
func session(_ session: WCSession, didReceiveApplicationContext applicationContext: [String : Any]) {
if let apiKeyString = message[@"apiKey"] as? String{
defaults.set(apiKeyString, forKey:kApiKey)
syncMail(apiKey: apiKeyString)
}
}
// receive apiKey if watch app is in the background
func session(_ session: WCSession, didReceiveApplicationContext applicationContext: [String : Any]) {
if let apiKeyString = applicationContext[@"apiKey"] as? String{
defaults.set(apiKeyString, forKey:kApiKey)
syncMail(apiKey: apiKeyString)
}
}
这似乎很适合我。但是,我不确定这是否是最好的方法。我想知道是否有任何方法可以改进我的方法?任何提示或建议表示赞赏。
这只有在您的手表连接到您的 phone 时才有效。您可以使用 updateapplicationContext 来保证将 apikey 传递给 Apple Watch。
您传递的似乎是您登录时生成的令牌,因此您应该将其保存在钥匙串中。
目前,我正在构建一个类似于邮件应用程序的苹果手表应用程序。我正在将 API 密钥从我的 iOS 应用程序发送到我的 Apple Watch 应用程序,并使用 API 密钥从我的 Apple Watch 发出请求。
这就是我将 API 密钥发送到我的 Apple Watch 应用程序的方式。
NSDictionary *applicationDict = @{@"apiKey" : apiKey };
if ([WCSession defaultSession].reachable) {
[[WCSession defaultSession] sendMessage:applicationDict replyHandler:^(NSDictionary *replyHandler) {
} errorHandler:^(NSError *error) {
}];
} else {
[session updateApplicationContext:applicationDict error:nil];
}
这就是我将 API 键存储在我的 Apple Watch 应用的用户默认设置中的方式。我保存 API 键。然后,我使用 API 键发出请求。如果用户退出 iPhone 应用程序,我将删除默认值中的 API 键。
// receive apiKey if watch app is in the foreground
func session(_ session: WCSession, didReceiveApplicationContext applicationContext: [String : Any]) {
if let apiKeyString = message[@"apiKey"] as? String{
defaults.set(apiKeyString, forKey:kApiKey)
syncMail(apiKey: apiKeyString)
}
}
// receive apiKey if watch app is in the background
func session(_ session: WCSession, didReceiveApplicationContext applicationContext: [String : Any]) {
if let apiKeyString = applicationContext[@"apiKey"] as? String{
defaults.set(apiKeyString, forKey:kApiKey)
syncMail(apiKey: apiKeyString)
}
}
这似乎很适合我。但是,我不确定这是否是最好的方法。我想知道是否有任何方法可以改进我的方法?任何提示或建议表示赞赏。
这只有在您的手表连接到您的 phone 时才有效。您可以使用 updateapplicationContext 来保证将 apikey 传递给 Apple Watch。
您传递的似乎是您登录时生成的令牌,因此您应该将其保存在钥匙串中。