iOS 事件/通知网络活动向上/向下/关闭 [英] iOS event/notification for network activity up/down/off

查看:79
本文介绍了iOS 事件/通知网络活动向上/向下/关闭的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当网络活动从无变为向上时(反之亦然),我希望为我的 iOS 应用程序创建一个事件/回调.类似于 Android 对 onDataActivity() 的处理方式.我不是在谈论可达性,而是在数据真正开始或停止传输时.

I want a event/callback for my iOS app when the network activity goes from none to up (and the other way around). Similar to how Android does with onDataActivity(). I'm not talking about Reachability but when data actually starts or stops transmitting.

该应用不适用于 App Store,也不适用于越狱.我有一个类似的功能,可以通过使用

The app is not for App Store and not for jailbreak. I have got a similar functionality of detecting when the screen goes on/off working by using

CFNotificationCenterAddObserver(CFNotificationCenterGetDarwinNotifyCenter(), //center
      NULL, // observer
      displayStatusChanged, // callback
      CFSTR("com.apple.iokit.hid.displayStatus"), // event name
      NULL, // object
      CFNotificationSuspensionBehaviorDeliverImmediately);

以及其他事件,例如

com.apple.springboard.hasBlankedScreen
com.apple.springboard.lockstate

现在我想知道是否有数据开始或停止传输的事件?或者,如果有人可以向我指出可以通过上述方式监视的所有事件的完整列表的方向.

Now I wonder if there is a event for when data is started or stopped to transmit? Or if someone could point me the direction of a complete list of all events that can be monitored in the way above.

推荐答案

我在越狱的 iOS 5 iPhone 上同时监控了标准 Darwin 通知和 Core Telphony 通知.

I monitored both the standard Darwin notifications and the Core Telphony notifications on a jailbroken iOS 5 iPhone.

我没有看到任何真正满足您要求的通知.

I did not see any notifications that really do what you want.

会发出一些 Core Telephony 通知,但不会在每次传输开始和结束时发出.看起来当数据服务连接时,可能会有一些通知,但同样,它们确实不是您所要求的:

There are a few Core Telephony notifications that come out, but not on every transmit start and end. It looks like when the data service connects, there might be some notifications, but again, they really aren't exactly what you asked for:

kCTIndicatorRadioTransmitNotification
kCTRegistrationDataStatusChangedNotification

如果您想尝试自己监控所有 Core Telephony 通知,您可以使用 Core Telephony 框架和 CT 通知中心:

If you want to try monitoring all the Core Telephony notifications for yourself, you can use the Core Telephony framework, and the CT notification center:

-(void) registerCallback {
   id ct = CTTelephonyCenterGetDefault();
   CTTelephonyCenterAddObserver(ct,   // center
                                NULL, // observer
                                telephonyEventCallback,  // callback
                                NULL,                    // event name (or all)
                                NULL,                    // object
                                CFNotificationSuspensionBehaviorDeliverImmediately);
}    

static void telephonyEventCallback(CFNotificationCenterRef center, void* observer, CFStringRef name, const void* object, CFDictionaryRef userInfo)
{
    //NSLog(@"telephonyEventCallback()");

    NSString* notifyName = (__bridge NSString*)name;
    if ([notifyName isEqualToString:@"kCTMessageReceivedNotification"]) {  // received SMS

    } /* look for other notification names here */
}

在上面的调用中,我将 NULL 传递给 CTTelephonyCenterAddObserver() 调用,该调用注册所有通知.如果您知道要查找的内容,您当然可以传递一个特定通知的名称,例如您为 com.apple.iokit.hid.displayStatus 发布的示例.

In the above call, I pass NULL to the CTTelephonyCenterAddObserver() call, which registers for all notifications. You can of course pass the name of one particular notification if you know what you're looking for, like the example you posted for com.apple.iokit.hid.displayStatus.

关于 john.k.doe 的选项,您可以尝试对该属性使用键值观察,以便在它改变了:

Regarding john.k.doe's option, you might try using Key Value Observing on that property, to get notified when it changes:

UIApplication* app = [UIApplication sharedApplication];
[app addObserver: self forKeyPath: @"networkActivityIndicatorVisible" options: NSKeyValueObservingOptionNew context: nil];

您的观察者回调在哪里:

where your observer callback is:

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
    if ([keyPath isEqualToString:@"networkActivityIndicatorVisible"]) {
        // process here
        NSLog(@"network activity indicator change!");
        BOOL active = [UIApplication sharedApplication].networkActivityIndicatorVisible;
    }
}

我不确定 KVO 是否仍能在后台运行,这可能取决于您的应用如何管理后台运行.

I'm not sure if KVO will still work in the background, and it might depend on how your app manages being backgrounded.

但是,当然,这要求应用在访问网络时实际使用该属性,并非所有应用都这样做.不幸的是,Apple 甚至将该指标设为了 3rd 方开发人员需要控制的内容.在 Android 和 BlackBerry 上,操作系统足够智能,可以知道何时发送/接收.

But, of course, that requires apps to actually use that property when they access the network, which not all of them do. It's unfortunate that Apple even made that indicator something that 3rd-party developers need to control. On Android and BlackBerry, the OS is smart enough to know when it's transmitting/receiving.

所以,这仍然只是你需要的一部分:(

So, this is still only partly what you need :(

这篇关于iOS 事件/通知网络活动向上/向下/关闭的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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