I want to check if an alert is already present on my window or not. The alert is that of GPS (sumthing like "your app" will like to use your current location with Don't Allow and Allow buttons). I want to set some flag if this alert is present on screen. If anyone knows it, then please help me in getting this solved.
for (UIWindow* window in [UIApplication sharedApplication].windows) {
NSArray* subviews = window.subviews;
if ([subviews count] > 0)
if ([[subviews objectAtIndex:0] isKindOfClass:[UIAlertView class]])
return YES;
}
return NO;
this will help...
If you are developing for ios4.2 or later than you can authorizationStatus of CLLocationManager class.
For this you will need to check [CLLocationManager authorizationStatus] variable if its value is kCLAuthorizationStatusNotDetermined then it the alert will be shown.
In iOS 5 or later their is one option through which use can reset the location warning in that case also the status will be kCLAuthorizationStatusNotDetermined. So if your application is running and user switches to setting to reset that property than you will need to implmenet the following delegate method of CLLocationManagerDelegate.
- (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status
{
switch (status)
{
case kCLAuthorizationStatusNotDetermined:
//If this is the case than alert will be shown
break;
case kCLAuthorizationStatusDenied:
break;
case kCLAuthorizationStatusRestricted:
break;
case kCLAuthorizationStatusAuthorized:
break;
default:
break;
}
}
Thanks,
Related
Im on an application that receive data from server, the problem is when user connect to cellular data (Not 3G or WIFI), it take ages to receive data.
i had implemented this code from this Answer but im not sure if it is effective or not, sometimes it's giving me an accurate type, and sometimes it don't.
here is my code:
- (void)newtworkType {
NSArray *subviews = [[[[UIApplication sharedApplication] valueForKey:#"statusBar"] valueForKey:#"foregroundView"]subviews];
NSNumber *dataNetworkItemView = nil;
for (id subview in subviews) {
if([subview isKindOfClass:[NSClassFromString(#"UIStatusBarDataNetworkItemView") class]]) {
dataNetworkItemView = subview;
break;
}
}
switch ([[dataNetworkItemView valueForKey:#"dataNetworkType"]integerValue]) {
case 0:
NSLog(#"No wifi or cellular");
break;
case 1:
NSLog(#"2G");
break;
case 2:
NSLog(#"3G");
break;
case 3:
NSLog(#"4G");
break;
case 4:
NSLog(#"LTE");
break;
case 5:
NSLog(#"Wifi");
break;
default:
break;
}}
is this the best i can do??, i tried Apple Reachability example, but it can determine if reachabilityForInternetConnection or just reachabilityForLocalWiFi but that not helpfull in my case.
Thanks in advance.
if using iOS 7+ then you can get information from CoreTelephony framework following method :
CTTelephonyNetworkInfo *telephonyInfo = [CTTelephonyNetworkInfo new];
NSLog(#"Current Radio Access Technology: %#", telephonyInfo.currentRadioAccessTechnology);
Possibles values defined which you will get are as follows :
CTRadioAccessTechnologyGPRS, CTRadioAccessTechnologyEdge ,CTRadioAccessTechnologyWCDMA , CTRadioAccessTechnologyLTE etc
Make sure that the Status bar is not hidden in your application.
if it's not visible it will always return No wifi or cellular because your code reads the text in the Status bar thats all.
this is the best way to solve your problem, just make the Status bar not hidden then the application will get the text about the network type.
case 1, with the NSLog(#"2G"); is the case where the phone is on regular cellular data, not 3G, not 4G, and not WiFi.
What you should do is insert code below the NSLog for 2G to prevent the data transfer.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Determining if user has denied CoreLocation permission
How would I go about detecting if a user says no to "use my default location" in an iOS app?
I would like to present them with a different view controller depending on their choice.
thanks
For that, you need to implement below delegate method:
- (void)locationManager:(CLLocationManager*)manager didFailWithError:(NSError*)error
{
if([error code]== kCLErrorDenied)
self.locationDenied = YES;
switch ([error code]) {
// "Don't Allow" on two successive app launches is the same as saying "never allow". The user
// can reset this for all apps by going to Settings > General > Reset > Reset Location Warnings.
case kCLErrorDenied:
[appDelegate showAllowGPSLocationView];
default:
break;
}
self.locationDefined = NO;
}
You can create method "showAllowGPSLocationView" in AppDelegate. And show view to user that, you need to access GPS location.
Hope it will resolve your issue.
Happy Coding!
-(void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status {
if (status == kCLAuthorizationStatusDenied) {
// denied
}
else if (status == kCLAuthorizationStatusAuthorized) {
// allowed
}
}
Implement CLLocationManagerDelegate delegate
For detailed explanation refer here.Worked for me.Hope it helps..
I made a function for that that solves the problem in two ways: first checks if location services are enabled (first location setting on device) and second checks if user authorized your app.
- (bool)locationAvailable
{
if (!([CLLocationManager locationServicesEnabled]) || ( [CLLocationManager authorizationStatus] == kCLAuthorizationStatusDenied))
return FALSE;
else
return TRUE;
}
You can try like below:
#if __IPHONE_OS_VERSION_MIN_REQUIRED > __IPHONE_4_2
if ([CLLocationManager locationServicesEnabled] && ([CLLocationManager authorizationStatus] == kCLAuthorizationStatusAuthorized))
#else
if ([CLLocationManager locationServicesEnabled])
#endif
I want to check whether the UIALertView is present on the screen or not, though I have done it by using the following method:
-(BOOL) isAlertShowing
{
for (UIWindow* window in [UIApplication sharedApplication].windows) {
NSArray* subviews = window.subviews;
if ([subviews count] > 0)
if ([[subviews objectAtIndex:0] isKindOfClass:[UIAlertView class]])
return YES;
}
return NO;
}
but I came to know that it is undocumented one. So, please tell me authenticate way of doing it.
In an app I submitted (and is approved), I have used a similar approach (see iPhone: detecting if a UIAlert/UIActionSheet are open)...
I don't see why you think it's not a valid method - I'd try it.
My application is a VOIP telephony toolbox.
I have a series of UISwitch controls, which the user can use to change their settings, for example if they want to alter their caller id settings.
When the user changes the setting I need to make a call to the Telephony platform over its Restful API. If the Restful call fails, then I would like to reset the switch back to its previous setting. eg If the user turns caller ID on, and it fails because of a connection failure, I would like the switch to revert back to off.
I implemented this in my switchChangedValue method, however it creates a nasty loop. When a failure happens I set the UISwitch to its previous setting, but it in turn calls the switchChangedValue method again, which fails and so on looping
Here is part of my switchChangedValue method, any ideas welcome.
//Check if its a valid response from the XSI server
if ([bs getHTTPResponseCode] >= 200 && [bs getHTTPResponseCode] < 300) {
//This is the successful case
}
else
{
// I throw an alert here
//Id really like to change the UISwitch back if it goes wrong but it causes a bad loop.
if (buttonstate == false){
[switchbutton setOn:YES animated:YES];
//This invokes my switchChangedValue again
}
else if (buttonstate == true){
[switchbutton setOn:NO animated:YES];
//This invokes my switchChangedValue again
} else{
NSLog(#"Something went bad");
}
[bs release];
You might try something like this:
Declare this in your header:
BOOL _fireAPICall;
Set it to YES whenever the particular class you're in is initialized:
- (id)init {
if (self = [super init]) {
...
_fireAPICall = YES;
...
}
return self;
}
Then:
if (_fireAPICall) {
if ([bs getHTTPResponseCode] >= 200 && [bs getHTTPResponseCode] < 300) {
// success
} else {
// failure
_fireAPICall = NO;
[switchbutton setOn:!buttonstate animated:YES];
}
} else {
_fireAPICall = YES;
// handle case where switch is turned off if necessary
}
This is assuming that you're not making an API call when the user manually turns the switch off, though - is that the case?
Updated above!
When a user double clicks the home button and swipe right, some audio controls shows.
How do I get use them?
I've searched but havn't found anything that helps me.
Try this
- (void) remoteControlReceivedWithEvent: (UIEvent *) receivedEvent {
switch (receivedEvent.subtype) {
case UIEventSubtypeRemoteControlTogglePlayPause:
[self playTapped];
break;
case UIEventSubtypeRemoteControlPreviousTrack:
[self previousTapped];
break;
case UIEventSubtypeRemoteControlNextTrack:
[self nextTapped];
break;
default:
break;
}
PlayTapped is the method for playing the music.
nextTapped is the method for the next song to be played.
previousTapped is for playing the previous track.
All the best