Surya
Surya

Reputation: 39

How to Detect wifi or 3G signal strength in iOS?

I am working on video player using MPMediaplayer framework in iOS, I need to play two videos in my app.(i.e. My first video will play when user has strong Network signal). My second video player will be played when their low network. I need to play my video in wifi or 3G etc... My First thing is how to detect my wifi speed in my iphone mobile and 3G speed also. I need to get mobile wifi speed in MBPS. I am was trying to write some code. But it is not working 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];
}

Upvotes: 2

Views: 2225

Answers (1)

abhishekkharwar
abhishekkharwar

Reputation: 3529

You can play high resolution video for wifi and low resolution video on 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 can reject your app if you will stream high resolution video over 3g Network.

Upvotes: 2

Related Questions