gpt4 book ai didi

objective-c - 父 handleWatchKitExtensionRequest 从未调用过?

转载 作者:行者123 更新时间:2023-11-28 07:07:07 24 4
gpt4 key购买 nike

问之前看了几个问题/答案,也附上了流程,但是好像不行。

我的 watchkit 扩展是用 Swift 编写的,而 AppDelegate 是用 Objective-C 编写的(因为它是旧代码)。

在我的扩展中,我调用了:

@IBAction func toPage2() {
println("to page 2")
WKInterfaceController.openParentApplication(["page":"2"], reply: {(reply, error) -> Void in })
}

在我的 AppDelegate 中,我尝试打印

- (void)application:(UIApplication *)application handleWatchKitExtensionRequest:(NSDictionary *)userInfo reply:(void(^)(NSDictionary *replyInfo))reply
{
NSLog(@"handle watchkit");

NSString *file = [[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"page2"] ofType:@"wav"];
AVAudioPlayer *audio = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:file] error:nil];
[audio play];
...

我也试过 Debug > Attach to process 并选择 iPhone 应用程序,但仍然没有任何反应。谁能给我指明方向?

最佳答案

按照 documentation 中的指定在 handleWatchKitExtensionRequest 中启动后台任务.这确保 iPhone 上的主应用程序在发送回复之前不会挂起(当 iPhone 上的应用程序未激活时)。

iPhone 上主应用的应用委托(delegate)中的代码:

- (void)application:(UIApplication *)application handleWatchKitExtensionRequest:(NSDictionary *)userInfo reply:(void ( ^)( NSDictionary * ))reply
{
__block UIBackgroundTaskIdentifier watchKitHandler;
watchKitHandler = [[UIApplication sharedApplication] beginBackgroundTaskWithName:@"backgroundTask"
expirationHandler:^{
watchKitHandler = UIBackgroundTaskInvalid;
}];

if ( [[userInfo objectForKey:@"request"] isEqualToString:@"getData"] )
{
// get data
// ...
reply( data );
}

dispatch_after( dispatch_time( DISPATCH_TIME_NOW, (int64_t)NSEC_PER_SEC * 1 ), dispatch_get_global_queue( DISPATCH_QUEUE_PRIORITY_DEFAULT, 0 ), ^{
[[UIApplication sharedApplication] endBackgroundTask:watchKitHandler];
} );
}

关于objective-c - 父 handleWatchKitExtensionRequest 从未调用过?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30079605/

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