简体   繁体   中英

Wait UIAlertView Button

I have this code in my Application:

if (self.uiSwtichState == TRUE)
        {
            if ([CLLocationManager locationServicesEnabled])
            {
                [self.textString stringByAppendingString:[[[SMSLocationModel alloc] init] getLocation]];

            }
        }
            MFMessageComposeViewController *sms = [[MFMessageComposeViewController alloc] init];
sms.messageComposeDelegate = self;
sms.body = self.textString;
sms.recipients = self.arrayOfNumbers;
[self presentModalViewController:sms animated:YES];

this code is into a method called "send". As you can imagine, the method getLocation get the current user location and, the first time the Application run, a UIAlertView appear on the screen asking the user if he want to allow the application to get his location. The problem is that when this UIAlertView appear, she is immediately replaced by the sms Vie Controller (MFMessageComposeViewController) and in this way the user doesn't have enough time for answer YES or NO in the UIAlertView.

There is a way for wait that the user press a button in the UIAlertView and THEN present the ModalViewController?

Thanks!

you could use the CLLocationManagers delegate method to wait until the authorization state changes. Save the sms viewController to an instance variable if you are not authorized to use location services. then ask the location manager to authorize you. If the authorization state changes present that view controller.

// somewhere else
self.locationManager.delegate = self;

- (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status {
    NSLog(@"New Authorization State: %d", status);
    if (self.viewControllerToPresentAfterLocationDidChange) {
        if (status == kCLAuthorizationStatusAuthorized) {
            // set location of viewController
            [self presentViewController:self.viewControllerToPresentAfterLocationDidChange animated:YES completion:NULL];
        }
        else {
            // user did not authorize you to use location services
        }
        self.viewControllerToPresentAfterLocationDidChange = nil;
    }
}


- (IBAction)buttonPressed:(id)sender {
    UIViewController *vc = [[UIViewController alloc] init];
    if ([CLLocationManager authorizationStatus] == kCLAuthorizationStatusAuthorized) {
        // authorized already, no alert will appear.

        // set location of vc
        [self presentViewController:vc animated:YES completion:NULL];
    }
    else if ([CLLocationManager authorizationStatus] == kCLAuthorizationStatusNotDetermined){
        self.viewControllerToPresentAfterLocationDidChange = vc;
        // access location so alert will show
    }
    else {
        // not authorized or restricted.
    }
}

Yes, you can implement the AlertViewDelegate .
See UIAlertViewDelegate Documentation for further information.

Then you can use the alertView:clickedButtonAtIndex: method and present your MessageComposer when the user has clicked the alert view.

Maybe you can use this delegate:

- (void)didPresentAlertView:(UIAlertView *)alertView{
}

For example, initializing a boolean which can prevent of presenting this modal view controller. Then if dismissed change the boolean to allow presenting this view controller.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM