- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我看到this这样的帖子,显然是在那里提取数据并像下面这样返回到Watch扩展:
- (void)application:(UIApplication *)application handleWatchKitExtensionRequest:(NSDictionary *)userInfo reply:(void ( ^)( NSDictionary * ))reply
{
if ( [[userInfo objectForKey:@"request"] isEqualToString:@"getData"] )
{
// get data
// ...
reply( data );
}
}
但是,当我尝试像这样获取网络数据后在一个块内调用“reply()”时:
__block UIBackgroundTaskIdentifier watchKitHandler;
watchKitHandler = [[UIApplication sharedApplication] beginBackgroundTaskWithName:@"backgroundTask"
expirationHandler:^{
watchKitHandler = UIBackgroundTaskInvalid;
}];
- (void)application:(UIApplication *)application handleWatchKitExtensionRequest:(NSDictionary *)userInfo reply:(void ( ^)( NSDictionary * ))reply
{
NSMutableDictionary *response = [NSMutableDictionary dictionary];
[ClassObject getDataWithBlock:^(BOOL succeeded, NSError *error){
if (succeeded)
{
[response setObject:@"update succeded" forKey:@"updateKey"];
reply(response);
}
else
{
if (error)
{
[response setObject:[NSString stringWithFormat:@"update failed: %@", error.description] forKey:@"updateKey"];
reply(response);
}
else
{
[response setObject:@"update failed with no error" forKey:@"updateKey"];
reply(response);
}
}
}];
}
dispatch_after(dispatch_time( DISPATCH_TIME_NOW, (int64_t)NSEC_PER_SEC * 180), dispatch_get_global_queue( DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
[[UIApplication sharedApplication] endBackgroundTask:watchKitHandler];
});
我收到此错误:
最佳答案
为确保异步数据获取不会立即返回(不调用回复),您可以尝试以下操作:
- (void)application:(UIApplication *)application handleWatchKitExtensionRequest:(NSDictionary *)userInfo reply:(void ( ^)( NSDictionary * ))reply
{
__block UIBackgroundTaskIdentifier watchKitHandler;
watchKitHandler = [[UIApplication sharedApplication] beginBackgroundTaskWithName:@"backgroundTask"
expirationHandler:^{
watchKitHandler = UIBackgroundTaskInvalid;
}];
NSMutableDictionary *response = [NSMutableDictionary dictionary];
dispatch_semaphore_t sema = dispatch_semaphore_create(0);
[ClassObject getDataWithBlock:^(BOOL succeeded, NSError *error){
if (succeeded)
{
[response setObject:@"update succeded" forKey:@"updateKey"];
reply(response);
}
else
{
if (error)
{
[response setObject:[NSString stringWithFormat:@"update failed: %@", error.description] forKey:@"updateKey"];
dispatch_semaphore_signal(sema);
reply(response);
}
else
{
[response setObject:@"update failed with no error" forKey:@"updateKey"];
dispatch_semaphore_signal(sema);
reply(response);
}
}
}];
dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER);
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];
});
}
关于ios - WatchKit在handleWatchKitExtensionRequest中的 block 内返回Reply():,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30137019/
handleWatchKitExtensionRequest 中的操作是否有限制?我尝试从资源包返回一个 UIImage,但我得到一个 nil 回复。 这个有效: -(void)application
我正在测试 iPhone 应用程序上某些代码的执行情况。我遵循 Apple 建议的文档(不使用后台任务,仅使用控制台日志)。但是我在控制台上没有得到任何东西(我想看到字符串“howdy”)。 这是因为
我正在尝试从 watchkit 应用程序启动父 ios 应用程序。我正在使用 url scheme 来启动应用程序。但它看起来像 -(void)application:(UIApplication *
我正在尝试将 2 个 PFQuery 嵌套在 HandleWatchKitExtensionRequest 中,以便我可以在回复中将数据传回我的 Watch Extension。在下面的代码中,第一个
通过上述方法请求 XML 项字典时出现以下错误: > NSLocalizedDescription=The UIApplicationDelegate in the iPhone App never
我正在试验一个 WatchKit 应用程序,它将在 iPhone 上触发音频警报。我已将其设置为触发,每 5 秒重复一次。在 iPhone 上,我有在后台播放短音频 wav 文件的逻辑。即使应用程序最
问之前看了几个问题/答案,也附上了流程,但是好像不行。 我的 watchkit 扩展是用 Swift 编写的,而 AppDelegate 是用 Objective-C 编写的(因为它是旧代码)。 在我
我在我的 iOS 应用程序委托(delegate)中的 handleWatchKitExtensionRequest 中放置了一堆 NSLog 语句。当我从调用 handleWatchKitExten
所以我有一个父应用程序可以登录服务器、创建用户等。然后我使用 WatchApp 中的 handleWatchKitExtensionRequest 来访问父应用程序,但一切都是空的。 我的问题是,ha
我正在尝试实现 Apple Watch 扩展。我需要调用我的 iPhone 应用程序类方法来触发 Web 请求。我在苹果文档中看到了这个方法,我也在尝试同样的方法。但是这个方法没有调用 - (void
我正在尝试将信息从我的 WatchKit 应用程序发送到我的主要父应用程序,据我所知,我应该只能使用 openParentApplication在我的 watchkit 扩展中,handleWatch
情况:我在 Watch 应用程序中使用 openParentApplication 来调用主应用程序中的 handleWatchKitExtensionRequest。这在模拟器中运行良好,并且在 i
得到 nil 或错误代码的不一致结果:当在应用程序 func handleWatchKitExtRequest 中调用 Parse 代码时,没有结果匹配查询。 (使用 Apple iOS SDK 8.
我正在构建一个 watch 扩展程序,它使用 handleWatchKitExtensionRequest 让 iPhone 应用程序刷新其填充共享首选项(应用程序组)的数据。 因此,每次我加载该应用
我正在使用 openParentApplication:和 handleWatchKitExtensionRequest:请求并发送一个 NSNumber 对象,该对象包含来自核心位置 locatio
好的,我正在制作这个 Apple Watch 应用程序,在我的 Watch 应用程序中,我有一个按钮。当您触摸按钮时,它会执行以下操作: [WKInterfaceController openPare
我是一名优秀的程序员,十分优秀!