How to detect Wi-Fi or 3G signal strength in iOS?

I am working on a video player using MPMediaplayer environment in iOS, I need to play two videos in my application (for example, my first video will play when the user has a strong network signal). My second video player will play on their low network. I need to play my video on Wi-Fi or 3G etc ... The first thing I learned about my cell phone Wi-Fi speed and 3G speed. I need to get my mobile wifi speed in MBPS. I am trying to write some code. But that doesn't work for me. Thanks in advance.

#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];
}

      

+3


source to share


1 answer


You can play high definition video for Wi-Fi and low definition video over cellular data.

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
}

      



Apple may reject your app if you stream high definition video over 3G network.

+2


source







All Articles