OGeek|极客世界-中国程序员成长平台

标题: iOS - 在应用程序中跟踪飞行模式状态(我想在 appstore 构建中使用它) [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-11 17:00
标题: iOS - 在应用程序中跟踪飞行模式状态(我想在 appstore 构建中使用它)

在我的应用程序中,我想知道飞行模式的当前状态。我浏览了很多链接,有些人说我们可以使用可达性来了解飞行模式状态,但没有得到正确的结果。和通过使用 Private Framework: Importing RadioPreferences.h 了解飞行模式状态的方法相同,但是有人说使用这个我们不能将应用程序提交到 Appstore。但我必须将应用提交到应用商店

以下是我关注的一些引用链接 Reachability airplane mode (3G) vs. Wifi Using Private Framework: Importing RadioPreferences.h



Best Answer-推荐答案


您好,我创建了自己的检查方法

#import <Foundation/Foundation.h>
#import "AppDelegate.h"
@interface InternetConnectionCheck : NSObject

+(BOOL)CheckConnection ;

@end


#import "InternetConnectionCheck.h"
#import <SystemConfiguration/SystemConfiguration.h>

@implementation InternetConnectionCheck

static InternetConnectionCheck *singletonObject = nil;


+ (id) sharedInstance
{
    if (! singletonObject) {
        singletonObject = [[InternetConnectionCheck alloc] init];
    }
    return singletonObject;
}


// Initialize class object
- (id)init
{
    if (! singletonObject) {
        singletonObject = [super init];
    }
    return singletonObject;
}

+(BOOL)CheckConnection{
    NSString * str = [[InternetConnectionCheck sharedInstance] newtworkType];
    if ( [[InternetConnectionCheck sharedInstance] connectedToInternet]==YES) {
        NSLog(@"INTERNET AVAILABLE VIA %@", str);
        return YES;
    }else{
        NSString * msg = [NSString stringWithFormat"Device is Connected Via %@ But We Unable to reach",str];
        UIAlertView * alert =[[UIAlertView alloc]initWithTitle"Error" message:msg delegate:nil cancelButtonTitle"Ok" otherButtonTitles: nil];
        [alert show];
        return NO;
    }
    return NO;
}


-(NSString * )newtworkType {
    NSArray *subviews = [[[[UIApplication sharedApplication] valueForKey"statusBar"] valueForKey"foregroundView"]subviews];
    NSNumber *dataNetworkItemView = nil;

    for (id subview in subviews) {
        if([subview isKindOfClass:[NSClassFromString(@"UIStatusBarDataNetworkItemView") class]]) {
            dataNetworkItemView = subview;
            break;
        }
    }

    NSString * connectedBy =@"None";
    switch ([[dataNetworkItemView valueForKey"dataNetworkType"]integerValue]) {
        case 0:
            NSLog(@"No wifi or cellular");
            connectedBy=@"No wifi or cellular";
            break;

        case 1:
            NSLog(@"2G");
            connectedBy= @"2G";
            break;

        case 2:
            NSLog(@"3G");
            connectedBy= @"3G";
            break;

        case 3:
            NSLog(@"4G");
            connectedBy= @"4G";
            break;

        case 4:
            NSLog(@"LTE");
            connectedBy= @"LTE";
            break;

        case 5:
            NSLog(@"Wifi");
            connectedBy=@"Wifi";
            break;


        default:
            break;
    }
    return connectedBy;
}

- (BOOL)connectedToInternet
{
    NSString *urlString = @"http://www.google.com/";
    NSURL *url = [NSURL URLWithString:urlString];
    NSURLRequest *request = [NSURLRequest requestWithURL:url
                                                                                             cachePolicy:NSURLRequestUseProtocolCachePolicy
                                                                                         timeoutInterval:5.0];

    NSHTTPURLResponse *response;
    NSURLResponse * resp;

    [self sendSynchronousRequest:request returningResponse:&resp error:nil];

    if ([resp isKindOfClass:[NSHTTPURLResponse class]]) {
        response = (NSHTTPURLResponse*)resp;
    }

    return ([response statusCode] == 200) ? YES : NO;

}

- (NSData *)sendSynchronousRequestNSURLRequest *)request returningResponseNSURLResponse **)response errorNSError **)error
{

    NSError __block *err = NULL;
    NSData __block *data;
    BOOL __block reqProcessed = false;
    NSURLResponse __block *resp;

    [[[NSURLSession sharedSession] dataTaskWithRequest:request completionHandler:^(NSData * _Nullable _data, NSURLResponse * _Nullable _response, NSError * _Nullable _error) {
        resp = _response;
        err = _error;
        data = _data;
        reqProcessed = true;
    }] resume];

    while (!reqProcessed) {
        [NSThread sleepForTimeInterval:0];
    }

    *response = resp;
    return data;
}

@end

并像使用它一样

 // Check Connection.
    BOOL Y =  [InternetConnectionCheck CheckConnection];
    NSLog(@"bool %s", Y ? "true" : "false");

要在模拟器上进行测试,您可以查看 my Blog

关于iOS - 在应用程序中跟踪飞行模式状态(我想在 appstore 构建中使用它),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38450494/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (https://ogeek.cn/) Powered by Discuz! X3.4