2014-12-24 60 views
3

我正在使用iOS中的MPMediaplayer框架的視頻播放器,我需要在我的應用程序中播放兩個視頻(即,當用戶具有強大的網絡信號時,我的第一個視頻將播放)。 我的第二個視頻播放器將播放時,他們的低網絡。我需要在WiFi或3G等播放我的視頻...我的第一件事是如何檢測我的iPhone手機和3G速度我的無線網絡速度也。我需要以MBPS獲得移動無線網速。我正在嘗試編寫一些代碼。但它不適合我。提前致謝。如何檢測iOS中的wifi或3G信號強度?

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

看一看這個 - http://stackoverflow.com/questions/26675427/wrong-status-for-reachability-in-appdidbecomeactive/26675493#26675493 – Kampai

+1

你似乎暗示信號強度決定了連接速度。你在問什麼?選一個。 –

+0

@JonathonReinhart感謝您的回覆。我需要計算信號強度..如果WiFi有高帶寬,我需要顯示高帶寬度的視頻播放器..如果低帶寬的WiFi,那麼我需要播放低帶寬的視頻播放器..所以這就是爲什麼我需要計算無線或3g信號強度(以MBPS爲單位) – Surya

回答

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可以拒絕您的應用程序。

+0

感謝您的回覆..但我需要獲得設備帶寬..所以PLZ指導我如何做這件事?基於設備帶寬我需要播放視頻播放器。如果我得到高帶寬,那麼需要播放高帶寬視頻player.if我得到低帶寬,然後我需要播放低帶寬 – Surya

+0

有沒有這樣的API或庫提供的Apple。您已經通過網絡服務來了解您設備的互聯網連接帶寬。 – abhishekkharwar

+0

基於webservices的webservices是否有可能獲得移動wifi帶寬? – Surya