【发布时间】:2015-05-14 22:23:12
【问题描述】:
我在 WatchKit 中有一个按钮,可以像这样向 iPhone 主应用发送通知。
-(IBAction) startSound
{
//turn sound on
NSString *requestString = [NSString stringWithFormat:@"startSound"]; // This string is arbitrary, just must match here and at the iPhone side of the implementation.
NSDictionary *applicationData = [[NSDictionary alloc] initWithObjects:@[requestString] forKeys:@[@"startSound"]];
[WKInterfaceController openParentApplication:applicationData reply:^(NSDictionary *replyInfo, NSError *error) {
//NSLog(@"\nReply info: %@\nError: %@",replyInfo, error);
}];
}
在我的 iPhone 应用程序委托中,我添加了以下代码。
- (void)application:(UIApplication *)application handleWatchKitExtensionRequest:(NSDictionary *)userInfo reply:(void(^)(NSDictionary *replyInfo))reply
{
NSLog(@"handleWatchKitExtensionRequest ...");
NSMutableDictionary *mutDic = [[NSMutableDictionary alloc] init];
//This block just asks the code put after it to be run in background for 10 mins max
__block UIBackgroundTaskIdentifier bgTask;
bgTask = [application beginBackgroundTaskWithName:@"MyTask" expirationHandler:^{
bgTask = UIBackgroundTaskInvalid;
}];
NSString *request = [userInfo objectForKey:@"startSound"];
if ([request isEqualToString:@"startSound"])
{
NSString *soundFilePath = [[NSBundle mainBundle] pathForResource: @"warning" ofType: @"mp3"];
NSURL *fileURL = [[NSURL alloc] initFileURLWithPath: soundFilePath];
myAudioPlayer1 = [[AVAudioPlayer alloc] initWithContentsOfURL:fileURL error:nil];
myAudioPlayer1.numberOfLoops = -1; //inifinite
[myAudioPlayer1 play];
}
reply(nil); //must reply with something no matter what
//once code is all done and the reply has been sent only then end the bg-handler
[application endBackgroundTask:bgTask];
}
然而,当我的应用程序进行 Apple 审核时,它被拒绝了,原因是我的应用程序必须在前台运行才能使声音功能正常工作。我错过了什么?
10.6 - Apple 和我们的客户高度重视简单、精致、富有创意且经过深思熟虑的界面。他们需要做更多的工作,但 值得。苹果设定了很高的标准。如果您的用户界面很复杂或 不太好,可能会被拒
10.6 详情
我们仍然发现您的 Apple Watch 应用需要包含该应用 在 iPhone 的前台运行以播放警报器 声音,这提供了糟糕的用户体验。
后续步骤
请参阅 UIApplicationDelegate 协议参考来实现 此方法并使用它来响应来自 Apple Watch 的请求 应用程序。
因为这个方法很可能在你的应用处于 后台,调用 beginBackgroundTaskWithName:expirationHandler: 实现开始时的方法和 endBackgroundTask: 处理完回复并执行回复后的方法 堵塞。启动后台任务可确保您的应用不 在它有机会发送回复之前被暂停。
【问题讨论】:
标签: ios watchkit apple-watch