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

标题: ios - 如何在 iOS 中检测 wifi 或 3G 信号强度? [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-12 13:25
标题: ios - 如何在 iOS 中检测 wifi 或 3G 信号强度?

我正在使用 iOS 中的 MPMediaplayer 框架开发视频播放器,我需要在我的应用程序中播放两个视频。(即当用户有强网络信号时,我的第一个视频将播放)。 我的第二个视频播放器将在他们的网络低时播放。我需要在 wifi 或 3G 等中播放我的视频......我的第一件事是如何检测我的 iphone 手机中的 wifi 速度和 3G 速度。我需要以 MBPS 为单位获得移动 wifi 速度。我正在尝试编写一些代码。但这对我不起作用。提前致谢。

#import "Reachability.h"

@interface NetworkViewController ()

@end

@implementation NetworkViewController

- (void)viewDidLoad
{
    [super viewDidLoad];    
    self.view.backgroundColor = [UIColor whiteColor];
    [self getDataCounters];

    networkLabel = [[UILabel alloc]initWithFrame:CGRectMake(20, 100, 300, 40)];
    networkLabel.textColor = [UIColor blackColor];
    networkLabel.backgroundColor = [UIColor whiteColor];
    networkLabel.userInteractionEnabled = NO;
    networkLabel.text = myNewString;

    [self.view addSubview:networkLabel];
}

- (NSArray *)getDataCounters
{
    BOOL success;
    struct ifaddrs *addrs;

    const struct ifaddrs *cursor;
    const struct if_data *networkStatisc;

    int WiFiSent = 0;
    int WiFiReceived = 0;
    int WWANSent = 0;
    int WWANReceived = 0;

    NSString *name=[[NSString alloc]init];
    success = getifaddrs(&addrs) == 0;

    if (success)
    {
         cursor = addrs;

         while (cursor != NULL)
         {
             name=[NSString stringWithFormat"%s",cursor->ifa_name];            
             NSLog(@"ifa_name %s == %@\n", cursor->ifa_name,name);

             // names of interfaces: en0 is WiFi ,pdp_ip0 is WWAN

             if (cursor->ifa_addr->sa_family == AF_LINK)
             {
                 if ([name hasPrefix"en"])
                 {
                      networkStatisc = (const struct if_data *) cursor->ifa_data;   
                      WiFiSent+=networkStatisc->ifi_obytes;   
                      WiFiReceived+=networkStatisc->ifi_ibytes;
                      NSLog(@"WiFiSent %d ==%d",WiFiSent,networkStatisc->ifi_obytes);
                      NSLog(@"WiFiReceived %d ==%d",WiFiReceived,networkStatisc->ifi_ibytes);
                      NSLog(@"wifi data is  %.2f",(float)WiFiReceived/1048576);

                    myNewString = [NSString stringWithFormat"%2f", (float)WiFiReceived/1048576];   
                    networkLabel.text = myNewString;
                }

                if ([name hasPrefix"pdp_ip"])
                {
                     networkStatisc = (const struct if_data *) cursor->ifa_data;     
                     WWANSent+=networkStatisc->ifi_obytes;
                     WWANReceived+=networkStatisc->ifi_ibytes;
                     NSLog(@"WWANSent %d ==%d",WWANSent,networkStatisc->ifi_obytes);
                     NSLog(@"WWANReceived %d ==%d",WWANReceived,networkStatisc->ifi_ibytes);
                }
            }

            cursor = cursor->ifa_next;
        }

        freeifaddrs(addrs);
    }

    return [NSArray arrayWithObjects:[NSNumber numberWithInt:WiFiSent], [NSNumber  numberWithInt:WiFiReceived],[NSNumber numberWithInt:WWANSent],[NSNumber numberWithInt:WWANReceived], nil];
}



Best Answer-推荐答案


您可以播放 wifi 的高分辨率视频和蜂窝数据的低分辨率视频。

Reachability *reachability = [Reachability reachabilityForInternetConnection];
[reachability startNotifier];

NetworkStatus status = [reachability currentReachabilityStatus];

if(status == NotReachable) 
{
    //No internet
}
else if (status == ReachableViaWiFi)
{
    //WiFi    Play high resolution video
}
else if (status == ReachableViaWWAN) 
{
    //3G    Play low resolution video
}

如果您将通过 3g 网络传输高分辨率视频,Apple 可以拒绝您的应用。

关于ios - 如何在 iOS 中检测 wifi 或 3G 信号强度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27631996/






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