我正在使用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];
}
看一看這個 - http://stackoverflow.com/questions/26675427/wrong-status-for-reachability-in-appdidbecomeactive/26675493#26675493 – Kampai
你似乎暗示信號強度決定了連接速度。你在問什麼?選一個。 –
@JonathonReinhart感謝您的回覆。我需要計算信號強度..如果WiFi有高帶寬,我需要顯示高帶寬度的視頻播放器..如果低帶寬的WiFi,那麼我需要播放低帶寬的視頻播放器..所以這就是爲什麼我需要計算無線或3g信號強度(以MBPS爲單位) – Surya