Which Delegate method trigger on click Dont't Allow for CLLocationManager - ios

Using CLLocationManager It show pop up message
with Allow "AppName" to access your location even when you are not using the app?
with two options Don't Allow and Allow On click Allow it triggers didUpdateLocations When click Don't Allow what is the delegate method it calls?

if user deny the permission didFailWithError has called and error type also
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
// [manager stopUpdatingLocation];
NSLog(#"error%#",error);
switch([error code])
{
case kCLErrorNetwork: // general, network-related error
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Location Error!" message:#"Can't access your current location! Please check your network connection or that you are not in airplane mode!" delegate:nil cancelButtonTitle:#"Ok" otherButtonTitles:nil, nil];
[alert show];
}
break;
case kCLErrorDenied:{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Location Error!" message:#"Location Access has been denied for app name!" delegate:nil cancelButtonTitle:#"Ok" otherButtonTitles:nil, nil];
// alert.tag=500;
[alert show];
}
break;
default:
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Location Error!" message:#"Can't access your current location!" delegate:nil cancelButtonTitle:#"Ok" otherButtonTitles:nil, nil];
[alert show];
}
break;
}
}

Related

Reduce alert view coding

All.
In my iOS app.
On many pages I am having Too many alerts and also with many Network conditions.
With too many alert texts I am fed up.
And Every time I have to put the same code.
Can I declare this Code in Some Helper Class ?
Or Reuse this Code ?
-(BOOL)checkInternetAndlocationServices {
if(IS_INTERNET) {
if([CLLocationManager locationServicesEnabled] &&
[CLLocationManager authorizationStatus] != kCLAuthorizationStatusDenied){
return YES;
}
else
{
NSLog(#"Location services are disabled.");
UIAlertView *alert=[[UIAlertView alloc] initWithTitle:#"Location services are off." message:#"This app requires an Location services." delegate:self cancelButtonTitle:#"OK" otherButtonTitles:#"Location Services", nil];
[alert setTag:NO_LOCATIONSERVICES];
[alert show];
return NO;
}
}
else
{
UIAlertView *alert=[[UIAlertView alloc] initWithTitle:#"Internet connection is off." message:#"This app requires an internet connection and locations services, please enable internet connection and location services." delegate:self cancelButtonTitle:#"OK" otherButtonTitles:#"Settings", nil];
[alert setTag:NO_INTERNET];
[alert show];
return NO;
}
}
Thanks.
Please edit this question, if you found it useful..
Thanks for giving good approaches, still any other ways, examples are most welcome.
You could make a helper class and use class methods to show alert.
You could also make a UIAlertView category and make a class method for showing alert.(Edit)
#implementation UIAlertView (MyAlert)
+(void) showAlertWithTitle:(NSString *)title message:(NSString *)message {
[[[UIAlertView alloc] initWithTitle:title message:message delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil] show];
}
#end
You could define a macro in .pch file or some helper header file for showing alert.#define Alert(title,msg,target) [[[UIAlertView alloc] initWithTitle:title message:msg delegate:target cancelButtonTitle:#"OK" otherButtonTitles:nil, nil] show]
Alert(#"This is Title",#"This is message",self);
You can make an NSObject class and write method whether Instance or Class method like this and pass only the message and the delegate whether needs to set nil or self like this:-
+(void)showAlertViewWithAlertMessage:(NSString*)alertMessage withDelegate:(id)delegate
{
UIAlertView *alert=[[UIAlertView alloc] initWithTitle:#"Title" message:alertMessage delegate:delegate cancelButtonTitle:OK_TAP otherButtonTitles: nil];
[alert show];
}
You can use it like this:-
[Classname showAlertViewWithAlertMessage:#"your message" withDelegate:nil];

Order of executing methods

I have a problem about order of executing methods.
if (indexPath.row == 2) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil
message:#"Data will be downloaded"
delegate:self
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[alert show];
if([app getData]){
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil
message:#"Data is downloaded."
delegate:self
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[alert show];
}
}
When I run this code snippet, I want to first show an alert view. However, it calls the getData method before showing alert view. Once the getData method is completed, alertView comes to the window.
How can I correct this?
The function is called asynchronously therefore appData gets called before the first alert view is visible. Change your code to this:
if (indexPath.row == 2) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil
message:#"Data will be downloaded"
delegate:self
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[alert show];
}
The below method will be called when the user presses OK on your first alert, but it means that while the first alert is visible, your code for appData would not start.
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if([app getData]){
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil
message:#"Data is downloaded."
delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[alert show];
}
}
Note: Remember to add UIAlertViewDelegate in your view controller
I don't have Xcode with me, but I'll take a stab at it anyway. Your problem is because the alert won't show until the main loop iterates. And it won't iterate until your getData method is executed since you're doing this on the main thread. So you need to fork.
Try wrapping your if() in something like this:
dispatch_async(dispatch_get_main_queue(),
^ {
if([app getData]){
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil
message:#"Data is downloaded."
delegate:self
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[alert show];
}
});
It's because the run loop must continue running so [UIAlertView show] is asynchronous and will not block the current thread. If the main thread was blocked then no user interface events could be delivered.
If you want something to occur after the first alert view is dismissed then do it within the alertView:clickedButtonAtIndex: delegate method.

is possible to remove previous alert view?

i have create two UIAlertView views in one method. Code like below
-(void) alert{
UIAlertView *alert_1 = [[UIAlertView alloc] initWithTitle:#"Message" message:#“Alert 1” delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alert_1 show];
UIAlertView *alert_2 = [[UIAlertView alloc] initWithTitle:#"Message" message:#“Alert 2” delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alert_2 show];
}
after call this method. iPhone app will appear popup for 2 times.
first appear is alert_1, disappear alert_1 and appear alert_2
after user press ok button in alert_2 thn appear alert_1
should be remove alert_1 when appear alert_2
is possible to remove previous alert view?
Send message - (void)dismissWithClickedButtonIndex:(NSInteger)buttonIndex animated:(BOOL)animated to alert1.
UIAlertView *alert_1 = [[UIAlertView alloc] initWithTitle:#"Message" message:#“Alert 1” delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alert_1 show];
[alert_1 dismissWithClickedButtonIndex:0 animated:YES];
UIAlertView *alert_2 = [[UIAlertView alloc] initWithTitle:#"Message" message:#“Alert 2” delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alert_2 show];
I am not clear about what your requirement is. But from what I understood you want alert_2 to popup first and when clicked on the "OK" button you want to dismiss that alert view and popup alert_1
- (void) alertview
{
alert_1 = [[UIAlertView alloc] initWithTitle:#"Alert 1" message:#"Message" delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil, nil];
[alert_1 setTag:1];
alert_1.delegate = self;
alert_2 = [[UIAlertView alloc] initWithTitle:#"Alert 2" message:#"Message" delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alert_2 setTag:2];
alert_2.delegate = self;
[alert_2 show];
}
- (void) alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex
{
if (alertView.tag ==2)
{
[alert_1 show];
}
}
Please note to declare your alert views in your .h file
Get UIAlertView by its tag or #Property and use this [myAlertView dismissWithClickedButtonIndex:-1 animated:YES];

Why is Core Location manager not working on iOS 6 simulator (using OSX Mountain Lion)

I was following the Core Data tutorial for iOS from this link
and my location manager does not appear to be picking up any events. I enabled location services on the simulator and when I launch the app it asks for permission to use my location and I say "ok". Any idea why it's not picking anything up?
My location manager delegate method shown below returns the following error:
2013-02-15 18:05:16.653 Locations[8280:c07] errorError Domain=kCLErrorDomain Code=0 "The operation couldn’t be completed. (kCLErrorDomain error 0.)"
I even tried Debug --> simulate location.
I am using OSX Mountain Lion btw.
Thanks!
//location manager delegate method for fail
- (void)locationManager:(CLLocationManager *)manager
didFailWithError:(NSError *)error {
NSLog(#"Reached loc mngr delegate method 2...error occurred");
[manager stopUpdatingLocation];
NSLog(#"error%#",error);
switch([error code])
{
case kCLErrorNetwork: // general, network-related error
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Error" message:#"please check your network connection or that you are not in airplane mode" delegate:self cancelButtonTitle:#"Ok" otherButtonTitles:nil, nil];
[alert show];
}
break;
case kCLErrorDenied:{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Error" message:#"user has denied to use current Location " delegate:self cancelButtonTitle:#"Ok" otherButtonTitles:nil, nil];
[alert show];
}
break;
default:
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Error" message:#"unknown network error" delegate:self cancelButtonTitle:#"Ok" otherButtonTitles:nil, nil];
[alert show];
}
break;
}
addButton.enabled = NO;
}

iOS Incompatible block pointer types issue

I have an implementation problem with a project using MKStoreKit. I am trying to implement an UIAlertView with various purchase options.
Here is the code where I do various things and call up UIAlertView:
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{
if(FALSE == payWallFlag)
{
// Display Alert Dialog
UIAlertView *message = [[UIAlertView alloc] initWithTitle:#"Subscription Options"
message:#"You do not have an active subscription. Please purchase one of the options below."
delegate:self
cancelButtonTitle:#"Cancel"
otherButtonTitles:nil];
[message addButtonWithTitle:#"7 Day Subscription $0.99"];
[message show];
return FALSE;
} else if(TRUE == payWallFlag)
{
// Load content
}
}
This is the physical alertView with the code which I am trying to call:
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
NSString *title = [alertView buttonTitleAtIndex:buttonIndex];
if([title isEqualToString:#"Cancel"])
{
NSLog(#"Cancel Button was selected.");
}
else if([title isEqualToString:#"7 Day Subscription $0.99"])
{
NSLog(#"7 Day Subscription button pressed.");
//Buy a 7 day subscription
if([SKPaymentQueue canMakePayments]) {
[[MKStoreManager sharedManager] buyFeature:kFeatureAId onComplete:^(NSString* purchasedFeature)
{
NSLog(#"Purchased: %#", purchasedFeature);
// Send an alert to the user
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Purchase Successful"
message:#"Thank you. You have successfully purchased a 7 Day Subscription."
delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[alert autorelease];
[alert show];
// Show the user the content now
payWallFlag = TRUE;
return TRUE;
}
onCancelled:^
{
// Send an alert to the user
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Purchase Failed"
message:#"Unfortunately you have cancelled your purchase of a 7 Day Subscription. Please try again."
delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[alert autorelease];
[alert show];
// Block the content again
payWallFlag = FALSE;
}];
}
else
{
NSLog(#"Parental control enabled");
// Send an alert to the user
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Purchase Failed"
message:#"Unfortunately Parental Controls are preventing you from purchasing a subscription. Please try again."
delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[alert autorelease];
[alert show];
// Block the content again
payWallFlag = FALSE;
}
}
}
The issue is I get the following Xcode error message in the UIAlertView:
Incompatible block pointer types sending 'int (^)(NSString *)' to parameter of type 'void (^)(NSString *)'
It appears the problems are: onComplete:^(NSString* purchasedFeature) and onCancelled:^ but I have no idea how to fix this.
You should not return TRUE; from that block, because then the compiler assumes that block returns an int, while it should return void (hence incompatible block types).
...onComplete:^(NSString* purchasedFeature) {
NSLog(#"Purchased: %#", purchasedFeature);
// Send an alert to the user
UIAlertView *alert = [[UIAlertView alloc] ...];
[alert autorelease];
[alert show];
// Show the user the content now
payWallFlag = TRUE;
return TRUE; // <--- Remove this line.
}...
For the second block (the onCancelled one), you probably missed the NSString* parameter, or whatever it expects.

Resources