gpt4 book ai didi

ios - WatchKit 应用程序连接问题

转载 作者:行者123 更新时间:2023-11-30 13:29:33 26 4
gpt4 key购买 nike

我正在尝试制作一个简单的 watch 应用程序,显示配套应用程序发送的信息。我在让 WatchKit 应用正确接收信息时遇到一些问题。

在发送方,我有以下代码:

- (void)viewDidLoad {
[super viewDidLoad];

// Prevents UIWebView from displaying under nav bar
self.navigationController.navigationBar.translucent = NO;

_timer = [NSTimer scheduledTimerWithTimeInterval:12.0 target:self selector:@selector(showAlert) userInfo:nil repeats:NO];

_diningLocations = [[NSMutableArray alloc] initWithCapacity:0];

if ([WCSession isSupported]) {
self.session = [WCSession defaultSession];
self.session.delegate = self;
[self.session activateSession];
// The following line works
//[self.session updateApplicationContext:@{@"hello" : @"world"} error:nil];
}

- (void)grabLocationsFromServer {
_query = [PFQuery queryWithClassName:self.tableName];

[MBProgressHUD showHUDAddedTo:self.view animated:YES];

[_query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
if (!error) {
// Do stuff

[self.locationTable reloadData];
[self loadWatchApp];
} else {
// Log details of the failure
NSLog(@"Error: %@ %@", error, [error userInfo]);
[MBProgressHUD hideHUDForView:self.view animated:YES];

}
[MBProgressHUD hideHUDForView:self.view animated:YES];
}];
}

- (void)loadWatchApp {

if(self.session) {

if (self.session.paired && self.session.watchAppInstalled) {
NSError *error;

[self.session updateApplicationContext:@{@"Locations": self.diningLocations} error:&error];
}
}

}

在接收端我有这个简单的代码片段:

func loadTableData(data: [RFDiningLocation]) {
table.setNumberOfRows(data.count, withRowType: "location")

for i in 0..<data.count {
let row = table.rowControllerAtIndex(i) as! RFDiningRowController
// row.label.setText(data[i].name)
row.label.setText("Hello, world!")
}
}

当我在 viewDidLoad 中调用 updateApplicationContext 时,它工作得 100% 正常,但是当我从另一个函数中调用它时,WatchKit 应用程序没有响应。我可以确认调用了 loadWatchApp 以及 updateApplicationContext。谢谢!

最佳答案

我将从 WCSessionDelegate activationDidCompleteWithState 实现 session 函数,然后在回调中如果状态已激活,则调用您的 scrapLocationsFromServer 函数。如果在激活 Watch session 之前调用grabLocationsFromServer,这可能就是您的应用程序上下文未更新的原因。

另外,建议您尽快调用activate session,例如在App Delegate的init中。

在你说这对你有用之前我只是在尝试,所以这就是我所看到的:

  • watch 启动时(初始连接),会调用 activationDidCompleteWithState 但还没有上下文,因此不会调用 didReceiveApplicationContext
  • 根据预期的 applicationContext 观看应用更新仅当我在 sessionReachabilityDidChange 中发送空消息时才会发生。我的理解是,我必须通过发送此初始消息来“唤醒”后台的手机应用程序。手机端可以忽略此消息。

    func sessionReachabilityDidChange(session: WCSession) {
    if session.reachable {
    sendMessage(["InitialMessage": true], replyHandler: nil, errorHandler: nil)
    }
    }

关于ios - WatchKit 应用程序连接问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36732445/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com