IOS event / network activity notification up / down / off

I want an event / callback for my iOS app when network activity goes from one to the other (and vice versa). Similar to how Android works with onDataActivity (). I'm not talking about reachability, but when data actually starts or stops transferring.

The app is not for the App Store, not for jailbreak. I have a similar function to detect when the screen turns on / off using

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

      

along with other events such as

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

      

Now I am wondering if there is an event to start or stop the data to transfer? Or if someone can point me in the direction of a complete list of all events that can be tracked, as described above.

+3


source to share


2 answers


I tracked both standard Darwin notifications and Core Telphony notifications on iPhone iOS 5 with jailbreak.

I haven't seen any notifications that actually do what you want.

Several basic telephony notifications appear, but not every time a transmission starts and ends. It looks like there might be some notifications when the data service connects, but then again, they really aren't exactly what you asked for:

kCTIndicatorRadioTransmitNotification
kCTRegistrationDataStatusChangedNotification

      

If you want to test monitoring of all basic telephony notifications for yourself, you can use CT's Basic Telephony Infrastructure and 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 */
}

      

In the above call, I am passing NULL

to a call CTTelephonyCenterAddObserver()

that registers for all notifications . You can of course pass the name of a single notification if you know what you are looking for, like the example you posted for com.apple.iokit.hid.displayStatus

.



As for john.k.doe , you can try using the Key Value Observing on this property to get notified when it changes:

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

      

where is your observer callback:

- (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;
    }
}

      

I'm not sure if KVO will run in the background and it may depend on how your application is being managed.

But of course this requires applications to actually use this property when accessing the network, which not all of them do. Unfortunately, Apple even made this indicator something that third-party developers should monitor. On Android and BlackBerry, the OS is smart enough to know when it is transmitting / receiving.

So, this is still just what you need :(

+5


source


there is no system notification for this.

if you're talking about monitoring activity in your own application, you obviously have control over that and should be able to copy any calls you know to access the network ...

if you are talking about monitoring activity to / from all other applications and you are ready to assume that they are in strict accordance with Apple guidelines and turn on / off the network activity status indicator in the status bar, you can call



[[UIApplication sharedApplication] isNetworkActivityIndicatorVisible];

      

but this obviously requires you to do it in polling mode, probably best done in a background GCD thread / queue. then you can post your own notification to be visible elsewhere in your application.

+3


source







All Articles