如何在iOS中检测wifi或3G信号强度?

时间:2014-12-24 05:31:19

标签: ios objective-c iphone xcode

我正在使用iOS中的MPMediaplayer框架制作视频播放器,我需要在我的应用程序中播放两个视频。(即当用户拥有强大的网络信号时,我的第一个视频将播放)。 我的第二个视频播放器将在他们的低网络播放。我需要在wifi或3G等播放我的视频...我的第一件事是如何在我的iphone手机和3G速度中检测我的wifi速度。我需要在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];
}

1 个答案:

答案 0 :(得分:2)

您可以播放高分辨率视频用于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可以拒绝您的应用。