Detect airplane mode on iOS

限于喜欢 提交于 2019-12-17 03:04:35

问题


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)


回答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.




回答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.




回答3:


For jailbroken tweaks/apps:

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

...

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



回答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");
  }



回答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]



来源:https://stackoverflow.com/questions/4804398/detect-airplane-mode-on-ios

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!