iOS 后台不工作 [英] iOS Backgrounding Not Working

查看:26
本文介绍了iOS 后台不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为 iOS 编写 VoIP 应用程序,但当应用程序处于后台时,它停止接听电话.当应用程序再次处于活动状态时,所有排队的消息开始得到处理.

I am in the process of writing a VoIP application for iOS but when App is in background it stops accepting calls. When the app is active again all the queued up messages start getting processed.

以下是我所做的.

在构建应用程序时,我将 IP 语音以及音频和 AirPlay 添加到 plist 文件中.然后我用 NetworkServiceTypeVoIP 标记 websocket 连接,你可以看到 此处.

When building the app I add Voice over IP as well as Audio and AirPlay to the plist file. Then I mark the websocket connection with NetworkServiceTypeVoIP as you can see here.

我没有设置保持活动超时处理程序,因为如果应用程序无法唤醒来接听电话,注册并不重要.任何帮助将不胜感激.

I have not set the keep alive timeout handler because registration doesn't matter if the app won't wake up to answer the call. Any help would be greatly appreciated.

需要注意的是,这是我的第一个 Swift 项目,我对 iOS 平台不是很熟悉.

It should be noted that this is my first Swift project and I'm not very familiar with the iOS platform.

推荐答案

要允许您的应用在后台模式下工作,您需要启用 IP 语音标志 ON(Path : Go to target --> capabilities --> Background模式).如下图.

To allow to work your app in background mode, you need to enable Voice over IP flag ON(Path : Go to target --> capabilities --> Background Modes). like as below.

并在您的项目中添加以下代码以支持后台:

And add following code in your project to support in background:

步骤 1: 将 __block UIBackgroundTaskIdentifier bgTask 声明为全局变量.

Step 1: Declare __block UIBackgroundTaskIdentifier bgTask as global variable.

第 2 步:在 applicationDidEnterBackground 中添加以下代码.

Step 2: To add following code in applicationDidEnterBackground.

  - (void)applicationDidEnterBackground:(UIApplication *)application {

         bgTask = [application beginBackgroundTaskWithExpirationHandler:^{
         bgTask = UIBackgroundTaskInvalid;
          }];

}

第 3 步:一旦应用进入前台模式,就停止后台任务处理程序.

Step 3: Stop background task handler once apps come in foreground mode.

- (void)applicationWillEnterForeground:(UIApplication *)application {
  // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.

  [[UIApplication sharedApplication] endBackgroundTask:bgTask];

}

这篇关于iOS 后台不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆