59

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)

Honey
  • 24,125
  • 14
  • 123
  • 212
Caner
  • 49,709
  • 33
  • 153
  • 169
  • 6
    Would you explain **why** you need to distinguish these two cases? What difference does it make if the user has no connection because of coverage or no connection because of airplane mode? – Jasarien Jan 26 '11 at 12:17
  • 14
    Becuase we can distinguish this on other mobile platforms and we would like to have the same functionality accross platforms as much as possible. We show different status messages depending on this and we try to guide user to correct the problem. – Caner Jan 26 '11 at 13:11
  • 8
    I know this is an old question, but just to clarify the need for this 'extraordinarily narrow situation': When an iPhone is in airplane mode, GPS locations are extremely unreliable, but do not cause errors. I subscribed to GPS events and left my app running for 2 hours. No errors, no gps locations, and all checks for 'can receive location events' return YES. However, disabling 3g and wifi manually gave me NO for the internal 'can receive location' checks. There is definitely a need for detecting Airplane mode explicitly as opposed to a general reachability check. – Owen Sep 07 '12 at 09:41
  • 18
    This question shouldn't have been closed. Whoever closed it either didn't understand the question or didn't understand how many other people would be interested in it. It's a valid question and it includes useful answers. Please don't be so quick to close questions. – RobertL Jul 27 '13 at 03:27

5 Answers5

19

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.

Felix
  • 35,041
  • 12
  • 93
  • 141
  • 7
    Hi, the return value is YES and I get zero in both of these situations: 1) Wifi is disabled from settings and there is no sim card in the phone 2) airplane mode. So unfortunately your suggestion doesnt solve my problem. – Caner Jan 26 '11 at 13:10
  • isn't the reachability status the same in both situations? okay, the bluetooth status could be different. – Felix Jan 26 '11 at 13:28
  • Apple's choice of the term "reachability" is unfortunate. It's really closer to "am I connected now?" than "will I be reachable in the future?" An app might care because if you're in Airplane Mode, you won't be reachable in the future. That message you sent won't get a reply, that game invite can't be accepted, etc. – Eliot Gillum Mar 06 '20 at 05:21
5

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.

Zac White
  • 248
  • 2
  • 6
2

For jailbroken tweaks/apps:

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

...

bool isInAirplaneMode = [[%c(SBTelephonyManager) sharedTelephonyManager] isInAirplaneMode];
Clawish
  • 2,814
  • 2
  • 20
  • 28
1

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");
  }
Vikash Rajput
  • 423
  • 3
  • 20
  • iOS 13 broke this, throwing a moderately obscure exception if you try it. We were using (roughly) the above, opened a Tech Support Incident with Apple, and they replied: "We have reviewed your request and have concluded that there is no supported way to achieve the desired functionality given the currently shipping system configurations." – Eliot Gillum Oct 23 '19 at 08:50
1

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]

BilalReffas
  • 6,834
  • 3
  • 43
  • 63
  • This is a good approach, actually. The problem is you can't distinguish the missing interfaces from a device that doesn't have a SIM card and shouldn't have those interfaces. – Eliot Gillum Mar 06 '20 at 05:23