简体   繁体   English

如何判断用户是否为我的应用关闭了蜂窝数据?

[英]How to tell if the user turned off cellular data for my app?

Although my app is usable without any internet connection, it may exchange data with a web server (in order to show some user statistics). 尽管我的应用无需连接互联网即可使用,但它可能会与网络服务器交换数据(以显示一些用户统计信息)。 So I advertise the app as "needs no internet connection". 因此,我将该应用程序广告宣传为“不需要互联网连接”。 Some users subsequently have turned off cellular data for my app, which should be completely fine. 随后,一些用户为我的应用关闭了蜂窝数据,这应该是完全可以的。 But when my app tries to exchange data, these users are bugged with the "Cellular data is turned off for [App Name]." 但是,当我的应用程序尝试交换数据时,这些用户会迷惑于“ [应用程序名称]的蜂窝数据已关闭”。 dialog. 对话。

This is an annoyance to them and I want to prevent these dialogs and simply skip the whole data exchange thing. 这让他们很烦恼,我想防止出现这些对话框,而只是跳过整个数据交换。

There is Apple's Reachability Sample Code . Apple的可达性示例代码

But although I turned off WiFi for the whole device and cellular data for the app, Reachability confirms a positive internet connection. 但是,尽管我关闭了整个设备的WiFi和该应用程序的蜂窝数据,但Reachability确认了良好的互联网连接。 To be more specific, it reports 更具体地说,它报告

Reachability Flag Status: WR t------ networkStatusForFlags

no matter whether I activated cellular data or not. 无论我是否激活了蜂窝数据。 Of course, when cellular data is turned off, no internet connection is actually available, so the data exchange fails. 当然,当蜂窝数据关闭时,实际上没有Internet连接可用,因此数据交换失败。 But the user is presented with the cellular data dialog anyway. 但是无论如何都会向用户显示蜂窝数据对话框。

Is there any way to detect whether a internet connection is available on iOS 7 and iOS 8, taking into account the cellular data setting for a specific app – all without bugging the user every time again with the cellular data dialog? 考虑到特定应用程序的蜂窝数据设置,是否有任何方法可以检测iOS 7和iOS 8上的互联网连接-无需每次都通过蜂窝数据对话框再次困扰用户?

My app currently comes without any settings panel, so I want to avoid setting up a (second, in-app) switch "don't use cellular data". 我的应用当前没有任何设置面板,因此,我想避免设置一个“第二个应用内”开关“不要使用移动数据”。 Also, I don't want to restrict data exchange to a WiFi connection since it's just a 2 KB of data per session which isn't a big thing for most users. 另外,我不想将数据交换限制为WiFi连接,因为每个会话仅2 KB的数据,对于大多数用户而言,这并不是一件大事。

I think the only supported way in iOS8 is to send a Ping to a known server and bug the user with the alert panel a few times. 我认为iOS8中唯一受支持的方法是将Ping发送到已知的服务器,并用警报面板几次对用户进行调试。 On iOS8, Apple displays the panel only twice, then skips it even if the app is restarted, maybe it will show up a day later again. 在iOS8上,Apple仅显示两次该面板,然后即使重新启动该应用程序也会跳过该面板,也许一天后它会再次出现。 (This is really bad news for ad-supported apps.) (对于广告支持的应用程序,这确实是个坏消息。)

Apple says ( https://devforums.apple.com/message/1059332#1059332 ): 苹果说( https://devforums.apple.com/message/1059332#1059332 ):

Another developer wrote in to DTS and thus I had a chance to investigate this in depth. 另一个开发人员写信给DTS,因此我有机会对此进行了深入研究。 Alas, the news is much as I expected: there is no supported way to detect that your app is in this state. 遗憾的是,消息出乎我的意料:没有支持的方法来检测您的应用程序处于这种状态。 Nor is there a way to make a "no user interaction" network connection, that is, request that the connection fail rather than present UI like this. 也没有办法建立“无用户交互”的网络连接,即请求连接失败,而不是像这样显示用户界面。

The following articles suggest ways to use ping: 以下文章建议使用ping的方法:

http://www.splinter.com.au/how-to-ping-a-server-in-objective-c-iphone/ http://www.splinter.com.au/how-to-ping-a-server-in-objective-c-iphone/

http://elbsolutions.com/projects/reachability-with-simpleping-wrapper/ http://elbsolutions.com/projects/reachability-with-simpleping-wrapper/

Try using this git project. 尝试使用此git项目。

How to install you can see inside the Readme on git . 如何安装,您可以在git的自述文件中看到。 I also used dispatch_once to be sure the Reachability will only be initialized once. 我还使用dispatch_once来确保可到达性仅被初始化一次。 This dispatch type is sometimes very useful! 这种dispatch类型有时非常有用!

Define variable in class 在类中定义变量

BOOL _online = NO;

Initialize the variable 初始化变量

static dispatch_once_t predicate;
    dispatch_once(&predicate, ^{
        Reachability *reach = 
          [Reachability reachabilityWithHostname:gameApiHost];
        reach.reachableBlock = ^(Reachability*reach) {
            NSLog(@"REACHABLE!"); _online = YES; 
        };
        reach.unreachableBlock = ^(Reachability*reach) { 
            NSLog(@"UNREACHABLE!"); _online = NO; 
        };
        [reach startNotifier];
    });

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何弹出“蜂窝数据已关闭”警告信息? - How to pop-up the "Cellular data is Turned Off" warning message? 显示蜂窝数据已关闭警报 - Showing cellular data is turned off alert 检查蜂窝数据是否已关闭 - Checking if Cellular Data has been turned off 当蜂窝数据和Wi-Fi都关闭时,是否可以获得用户位置? - Is it possible to get user location when both cellular data and wi-fi are turned off? IOS:当蜂窝数据打开且 Wi-Fi 关闭时,如何启用 react-native 提取工作? - IOS: How do I enable react-native fetch to work when Cellular Data is turned on and Wi-Fi is off? 当应用关闭时,如何从推送通知中获取数据? - How to fetch data from push notification when app is turned off? iOS如何在同时打开wifi的情况下检测蜂窝数据 - iOS how to detect cellular data is turned on when wifi is turned on at the same time 如何在 iPhone 开机或关机时通知您的应用程序? - How to have your app notified whenever iPhone is turned on or will be turned off? SKStoreReviewController 如何检测用户已在设置中关闭此应用程序 (RTA) 或已达到 3 次限制? - SKStoreReviewController how to detect that user has turned off Rate This App (RTA) in settings or 3 times limit has reached? 当我的应用程序中的位置服务关闭时,如何显示包含设置+取消按钮的UIAlertView? - How to present UIAlertView which contains Settings + Cancel button when location services are turned off in my app?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM