Detect airplane mode on iOS

Detect airplane mode on iOS

How can I detect if the phone is in airplane mode? (It's not enough to detect there is no internet connection, I have to be able to distinguish these 2 cases)

Solutions/Answers:

Answer 1:

Try using SCNetworkReachabilityGetFlags (SystemConfiguration framework). If the flags variable handed back is 0 and the return value is YES, airplane mode is turned on.

Check out Apple’s Reachability classes.

Answer 2:

You can add the SBUsesNetwork boolean flag set to true in your Info.plist to display the popup used in Mail when in Airplane Mode.

Answer 3:

For jailbroken tweaks/apps:

@interface SBTelephonyManager : NSObject
+(id)sharedTelephonyManager;
-(BOOL)isInAirplaneMode;
@end

...

bool isInAirplaneMode = [[%c(SBTelephonyManager) sharedTelephonyManager] isInAirplaneMode];

Answer 4:

We can not get this information without using private libraries. Here is some code but it will not work when carrier signal is not available.

UIApplication *app = [UIApplication sharedApplication];
NSArray *subviews = [[[app valueForKey:@"statusBar"] valueForKey:@"foregroundView"] subviews];

NSString *dataNetworkItemView = nil;

for (id subview in subviews) {
    if([subview isKindOfClass:[NSClassFromString(@"UIStatusBarSignalStrengthItemView") class]]) {
            dataNetworkItemView = subview;
            break;
     }
}
double signalStrength = [[dataNetworkItemView valueForKey:@"signalStrengthRaw"] intValue];
 if (signalStrength > 0) {
        NSLog(@"Airplane mode or NO signal");
  }
  else{
        NSLog(@"signal available");
  }
Related:  iOS : Switching between 12/24 hour times from strings

Answer 5:

Since iOS 12 and the Network Framework it’s somehow possible to detect if airplane mode is active.

import Network

let monitor = NWPathMonitor()

monitor.pathUpdateHandler = { path in
    if path.availableInterfaces.count == 0 { print("Flight mode") }
    print(path.availableInterfaces)
}

let queue = DispatchQueue.global(qos: .background)
monitor.start(queue: queue)

path.availableInterfaces is returning an array. For example [en0, pdp_ip0]. If no interface is available is probably on flight mode.

WARNING
If airplane mode and wifi is active then path.availableInterfaces is not empty, because it’s returning [en0]

References