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];
}
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.
Related
I want to check connection to a local wifi with bonjour using [Reachability reachabilityForLocalWifi] but it is now deprecated in iOS 10. Apple suggested to use [Reachability reachabilityWithAddress:] but it still returns "not reachable" even when i'm connected to a local wifi.
update:
Here is my code using reachabilityWithAddress which always returns "not reachable" for local wifi.
+(BOOL)checkWifiConnectionIfOn{
BOOL isOn;
struct sockaddr_in localWifiAddress;
bzero(&localWifiAddress, sizeof(localWifiAddress));
localWifiAddress.sin_len = sizeof(localWifiAddress);
localWifiAddress.sin_family = AF_INET;
// IN_LINKLOCALNETNUM is defined in <netinet/in.h> as 169.254.0.0.
localWifiAddress.sin_addr.s_addr = htonl(IN_LINKLOCALNETNUM);
Reachability* returnValue = [Reachability reachabilityWithAddress:(struct sockaddr_in *) &localWifiAddress];
if (returnValue != NULL && [returnValue currentReachabilityStatus] != NotReachable) //Reachable
{
//
isOn = YES;
}
else{ // Not Reachable
NSString *log = [NSString stringWithFormat:#"WifiController checkWifiConnectionIfOn Wifi OFF"];
[Logger writeDeviceLog:log logType:ERROR_LOG];
isOn = NO;
}
return isOn;
}
Since you know after June 1, app in iOS should support only-IPV6. I find a way that can change an ipv4 ip address to an ipv6 ip address. But, I cann't judge a network environment is only-IPV6. I already open a NAT64 WIFI by my MACBOOK, also use flight mode to make sure it's the only-IPV6. I just find there is an ipv6 ip address at first. Then work the code again, i will get both an ipv4 and an ipv6 address. I have no idea about it. Anyone have any idea?
There are my codes for fetch ip address:
- (NSMutableDictionary *)localIPAddressFetcher {
NSMutableDictionary *addressDic = [NSMutableDictionary dictionary];
struct ifaddrs *myaddrs, *temp_addr;
struct sockaddr_in *s4;
struct sockaddr_in6 *s6;
int status = 0;
char buf[64];
status = getifaddrs(&myaddrs);
if (status == 0) {
for (temp_addr = myaddrs; temp_addr != NULL; temp_addr = temp_addr->ifa_next) {
if (temp_addr->ifa_addr == NULL) continue;
if ((temp_addr->ifa_flags & IFF_UP) == 0) continue;
if (temp_addr->ifa_flags & IFF_LOOPBACK) continue;
if (temp_addr->ifa_addr->sa_family == AF_INET) {
s4 = (struct sockaddr_in *)(temp_addr->ifa_addr);
if (inet_ntop(temp_addr->ifa_addr->sa_family, (void *)&(s4->sin_addr), buf, sizeof(buf)) != NULL) {
if (![[NSString stringWithUTF8String:temp_addr->ifa_name] isEqualToString:#"lo0"]) {
NSString *address = [NSString stringWithUTF8String:buf];
if ([[NSString stringWithUTF8String:temp_addr->ifa_name] isEqualToString:#"en0"]) {
[addressDic setObject:address forKey:#"ipv4_wifi"];
} else {
[addressDic setObject:address forKey:[NSString stringWithUTF8String:temp_addr->ifa_name]];
}
}
}
} else if (temp_addr->ifa_addr->sa_family == AF_INET6) {
s6 = (struct sockaddr_in6 *)(temp_addr->ifa_addr);
if (inet_ntop(temp_addr->ifa_addr->sa_family, (void *)&(s6->sin6_addr), buf, sizeof(buf)) != NULL) {
if (![[NSString stringWithUTF8String:temp_addr->ifa_name] isEqualToString:#"lo0"]) {
NSString *address = [NSString stringWithUTF8String:buf];
if ([[NSString stringWithUTF8String:temp_addr->ifa_name] isEqualToString:#"en0"]) {
[addressDic setObject:address forKey:#"ipv6_wifi"];
} else {
[addressDic setObject:address forKey:[NSString stringWithUTF8String:temp_addr->ifa_name]];
}
}
}
}
}
}
freeifaddrs(myaddrs);
return addressDic;}
I want to know is it possible in iOS to get the data usage by specific WIFI even when app is not running. Is there any API which can be used for this to get result?
To know the total WIFI I used following code:
- (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);
}
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);
}
NSLog(#"%#",[NSArray arrayWithObjects:[NSNumber numberWithInt:(WiFiSent)/1000000], [NSNumber numberWithInt:(WiFiReceived)/1000000],[NSNumber numberWithInt:(WWANSent)/1000000],[NSNumber numberWithInt:(WWANReceived)/1000000], nil]);
return [NSArray arrayWithObjects:[NSNumber numberWithInt:(WiFiSent)/1000000], [NSNumber numberWithInt:(WiFiReceived)/1000000],[NSNumber numberWithInt:(WWANSent)/1000000],[NSNumber numberWithInt:(WWANReceived)/1000000], nil];
}
Please help me with this.
I'm checking my users data consumption using the method below, taken from here. It's working well most of the time, but for some users it's returning a negative value. In other words the WWANReceived is negative.
How can that be? Is there a fix?
+ (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];
// getifmaddrs
success = getifaddrs(&addrs) == 0;
if (success)
{
cursor = addrs;
while (cursor != NULL)
{
name = [NSString stringWithFormat:#"%s",cursor->ifa_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;
}
if ([name hasPrefix:#"pdp_ip"])
{
networkStatisc = (const struct if_data *) cursor->ifa_data;
WWANSent += networkStatisc->ifi_obytes;
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];
}
Maybe you've already solved this I was just testing the same example and came upon this.
You have to change the type from int to long long to handle bigger values. What you're seeing is integer overflow that happens after 2GB of traffic.
I'm calling a objective C method in objective C++ class where I create a label also hiding the status bar but the status bar is hiding fine while the label was not getting created and shown! If I call that method anywhere else other than this objective C++ class it works fine without any issue
The Objective C++ class is below
void audioRouteChangeListenerCallback (void *inUserData, AudioSessionPropertyID inPropertyID, UInt32 inPropertyValueSize, const void *inPropertyValue )
{
// ensure that this callback was invoked for a route change
if (inPropertyID != kAudioSessionProperty_AudioRouteChange) return;
{
// Determines the reason for the route change, to ensure that it is not
// because of a category change.
CFDictionaryRef routeChangeDictionary = (CFDictionaryRef)inPropertyValue;
CFNumberRef routeChangeReasonRef = (CFNumberRef)CFDictionaryGetValue (routeChangeDictionary, CFSTR (kAudioSession_AudioRouteChangeKey_Reason) );
SInt32 routeChangeReason;
CFNumberGetValue (routeChangeReasonRef, kCFNumberSInt32Type, &routeChangeReason);
if (routeChangeReason == kAudioSessionRouteChangeReason_OldDeviceUnavailable) {
//Handle Headset Unplugged
NSLog(#"PluggedOut");
}
else if (routeChangeReason == kAudioSessionRouteChangeReason_NewDeviceAvailable)
{
//Handle Headset plugged in
NSLog(#"Something Plugged In");
audiotest *test = [[audiotest alloc] init];
ArmorController *armorcontroller =[[ArmorController alloc]init];
NSLog(#"%d", [test Checkheadphonestatus]);
if([test Checkheadphonestatus] == 1)
{
[armorcontroller deviceconnectedalert];
}
else if([test Checkheadphonestatus] == 0)
{
NSLog(#"Device not connected");
}
}
}
The objective C method which not executing properly inside objective C++ class is below
- (void) deviceconnectedalert
{
[[UIApplication sharedApplication] setStatusBarHidden:YES withAnimation:UIStatusBarAnimationFade];
self.titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 20, 320, 30)];
[self.titleLabel setText:#"Device Connected Successfully!"];
self.titleLabel.backgroundColor =[UIColor redColor];
[self.view addSubview:self.titleLabel];
}