Search code examples
ioswifiiphone-privateapihotspottethering

How to detect any enabled hotspot in iOS with private APIs


I want to detect whether the user has enabled hotspot/tethering on an iOS device. I can use private APIs, knowing it wont make it to the app store.

I was trying to go through private APIs list/runtime headers, but there are too many to decide which might be helpful. I'm trying to find where UIApplicationWillChangeStatusBarFrameNotification gets fired from in private APIs (probably called for active call and activated hotspot, etc.)

I tried detect personal hotspot and also used CaptiveNetwork, but it only returns the connected WiFi SSID, not the created hotspot.

With the above code the compiler shows the error

SCDynamicStoreCreate is unavailable: not available on iOS.

So I went into SCDynamicStore.h and changed the following:

SCDynamicStoreRef
SCDynamicStoreCreate    (
    CFAllocatorRef          allocator,
    CFStringRef         name,
    SCDynamicStoreCallBack      callout,
    SCDynamicStoreContext       *context
)

__OSX_AVAILABLE_STARTING(__MAC_10_1,__IPHONE_NA);

to:

SCDynamicStoreRef
SCDynamicStoreCreate    (
    CFAllocatorRef          allocator,
    CFStringRef         name,
    SCDynamicStoreCallBack      callout,
    SCDynamicStoreContext       *context
)           

__OSX_AVAILABLE_STARTING(__MAC_10_1,__IPHONE_6_0);

Now I am able to get the dictionary with the states 1022 and 1023.

By changing the file like this, will I have any problem in archiving the build (or any other problem) other than the fact that we know this code won't make it to the app store?


Solution

  • Without Private APIs

    You can detect active personal hotspot by enumerating network interfaces using C APIs. When hotpost is active and someone is connected to it there will be interface with bridge prefix. On my iPhone 5 it's bridge100. If hotspot is disabled or no one is connected to it that interfaces will not even be in the list.

    C APIs will even return you IP address and subnet mask in that network.

    That's what I'm using in my applications to detect active hotspot.

    To get SSID you need [[UIDevice currentDevice] name] - personal hotspot SSID always matches device name.

    With Private APIs

    You can obtain all the information about personal hotspot using this code:

    SCDynamicStoreRef sc = SCDynamicStoreCreate(NULL, CFSTR("com.apple.wirelessmodemsettings.MISManager"), NULL, NULL);
    NSDictionary* info = (__bridge_transfer NSDictionary*)SCDynamicStoreCopyValue(sc, CFSTR("com.apple.MobileInternetSharing"));
    CFRelease(sc);
    

    info dictionary will look something like this when hotspot is active and has connections:

    {
        Errnum = 0;
        ExternalInterfaces =     (
            "pdp_ip0"
        );
        Hosts =     {
            Current = 1;
            Max = 5;
            MoreAllowed = 1;
            Type =         {
                AirPort = 0;
                Bluetooth = 0;
                Ethernet = 0;
                "USB-Ethernet" = 1;
            };
        };
        InternalInterfaces =     (
            bridge100
        );
        Reason = 0;
        State = 1023;
        Version = 2;
    } 
    

    When hotspot is active but there are no connections:

    {
        Errnum = 0;
        ExternalInterfaces =     (
        );
        Hosts =     {
            Current = 0;
            Max = 5;
            MoreAllowed = 1;
            Type =         {
                AirPort = 0;
                Bluetooth = 0;
                Ethernet = 0;
                "USB-Ethernet" = 0;
            };
        };
        InternalInterfaces =     (
        );
        Reason = 0;
        State = 1023;
        Version = 2;
    }
    

    When hotspot is not active:

    {
        Errnum = 45;
        ExternalInterfaces =     (
        );
        Hosts =     {
            Current = 0;
            Max = 5;
            MoreAllowed = 1;
            Type =         {
                AirPort = 0;
                Bluetooth = 0;
                Ethernet = 0;
                "USB-Ethernet" = 0;
            };
        };
        InternalInterfaces =     (
        );
        Reason = 0;
        State = 1022;
        Version = 2;
    }
    

    State key will be equal to 1023 when hotspot is active regardless of active connections. I don't know whether that value contains bit-flags or not. iOS is actually checking if value is equal to 1023.

    SCDynamicStore APIs are from public SystemConfiguration framework but marked as not available on iOS platform. That means all the headers are there, you just need to copy definitions without __OSX_AVAILABLE_STARTING macro.

    You can even observe changes to that setting - you can specify a key which value you want to observe. Read Apple documentation for implementation details.

    So I went into SCDynamicStore.h and changed the following

    I wouldn't do that. It shouldn't cause any problems but for me changing SDK headers is not a good solution. I suggest the following. Don't include the framework headers. Make your own header where you copy all the methods you need with __OSX_AVAILABLE_STARTING changed or removed.