后台的应用程序如何检查IOS的互联网连接? [英] How App in background can check for internet connection, IOS?

查看:126
本文介绍了后台的应用程序如何检查IOS的互联网连接?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的Iphone应用程序有2种模式,在线模式和离线模式.如果用户以在线模式使用应用程序,则请求将立即提交到服务器.如果用户在离线模式下使用应用程序,则我会将请求存储在本地,以便可以在互联网可用时上传请求.我正在使用可达性课程来检查互联网的可用性.应用程序的可及性很好. 我无法实现以下方案.我已经在用户处于脱机模式下时存储了请求,这意味着用户将通过单击iPhone中的主页"按钮在后台推送应用程序.一段时间后,用户将在其设备中激活wifi或蜂窝连接.当用户启用互联网连接时,后台的我的应用应收到通知,并应开始向服务器提交请求.我如何实现此功能?许多ios应用可能都具有相同的要求.他们是怎么做的?任何的想法?

My Iphone app have 2 mode, online mode, and offline mode. if user is using app in online mode, the request gets submitted to server immediately. if user is using app in offline mode, i will store the request locally, so that i can upload it when internet is available. I am using reachability class to check internet availability. Reachability is working fine with app. I am not able to achieve following scenario. I had stored request as user is working in offline mode, mean while user will push application in background by clicking home button in iPhone. after some time user will activate the wifi or cellular connection in his device. when user enables internet connection, my app which is in background should get notified and should start submitting request to server. How can i achieve this functionality? Many ios app may be having same this requirement. how they are doing it? any idea?

我尝试了以下步骤来实现这一目标,但没有成功.我在这里做错了什么,请帮帮我.

I tried following step to achieve this, but wasn't successful.what I am i doing wrong here, help me.

1)我在applicationDidEnterBackground委托方法中添加了kReachabilityChangedNotification的观察者.但我的应用程序无法通过这种方法收听Kreachabiliychaged通知.

1) I added observer for kReachabilityChangedNotification inside applicationDidEnterBackground delegate method. but my app fails to listen to the Kreachabiliychagednotification, in this method.

2)如果我在applicationdidbecomeActive中添加了kReachabilityChangedNotification的观察者,那么我的应用程序就可以侦听Kreachabiliychagednotification.

2) If i add observer for kReachabilityChangedNotification inside applicationdidbecomeActive, then my app is able to listen for Kreachabiliychagednotification.

当应用程序在后台运行时,为什么IOS不允许收听Kreachabiliychagednotification?

Why IOS doesnt allow to listen for Kreachabiliychagednotification when app is in background?

推荐答案

答案是,除非您的应用程序具有特定用途之一(引用Apple doc),否则您将无法:

The answer is that you can't unless you application will have one of those specific use (quoting Apple doc):

必须在以下位置声明对某些类型的后台执行的支持 通过使用它们的应用程序前进.一个应用宣布支持 服务使用其Info.plist文件.将UIBackgroundModes键添加到 您的Info.plist文件并将其值设置为包含一个或一个的数组 以下更多字符串:

Support for some types of background execution must be declared in advance by the app that uses them. An app declares support for a service using its Info.plist file. Add the UIBackgroundModes key to your Info.plist file and set its value to an array containing one or more of the following strings:

*音频-在播放过程中,该应用向用户播放可听内容 背景. (此内容包括流音频或视频内容 使用AirPlay.)
*位置-该应用程序可让用户随时了解他们的信息 位置,即使它在后台运行.
* voip-应用程序 为用户提供了使用手机拨打电话的功能 网络连接.
* newsstand-content-该应用程序是一个报亭应用程序, 下载和处理杂志或报纸中的内容 背景.
* external-accessory-该应用程序可与硬件附件一起使用 需要通过 外部附件框架.
* bluetooth-central-该应用程序可与 需要定期发送更新的蓝牙配件 通过Core Bluetooth框架安排时间.
* bluetooth-peripheral-该应用程序支持蓝牙通信 通过Core Bluetooth框架的外设模式.

*audio—The app plays audible content to the user while in the background. (This content includes streaming audio or video content using AirPlay.)
*location—The app keeps users informed of their location, even while it is running in the background.
*voip—The app provides the ability for the user to make phone calls using an Internet connection.
*newsstand-content—The app is a Newsstand app that downloads and processes magazine or newspaper content in the background.
*external-accessory—The app works with a hardware accessory that needs to deliver updates on a regular schedule through the External Accessory framework.
*bluetooth-central—The app works with a Bluetooth accessory that needs to deliver updates on a regular schedule through the Core Bluetooth framework.
*bluetooth-peripheral—The app supports Bluetooth communication in peripheral mode through the Core Bluetooth framework.

您只能再次进入前景才能检查可达性.

You can only check for reachability again re-entering foreground.

这篇关于后台的应用程序如何检查IOS的互联网连接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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