开发者

How can I buy time to fully dismiss a UIAlertView when entering background?

开发者 https://www.devze.com 2023-02-03 11:55 出处:网络
I pop up a UIAlertView to ask the user if they really want to open a URL. If开发者_开发知识库 they choose \"Yes\" my app immediately opens the URL and is sent to the background. When the user reopens

I pop up a UIAlertView to ask the user if they really want to open a URL. If开发者_开发知识库 they choose "Yes" my app immediately opens the URL and is sent to the background. When the user reopens my app (with multitasking) the alert view hasn't disappeared completely and is in a half-transparent state. Presumably the screenshot used in multitasking was taken before the alert view completely disappeared, but just after it began to fade away.

How can I give my app the extra second it needs to completely dismiss the alert view? It seems like I should do something in -applicationDidEnterBackground: or listen in the view controller for the equivalent notification but I'm not sure about the best approach.


You should call - (void)dismissWithClickedButtonIndex:(NSInteger)buttonIndex animated:(BOOL)animated. In iOS 4 alert views are not dismissed automatically when an app moves to the background.


I've heard of this happening--the backgrounding screenshot happens in the middle of the animation of the thing that triggered the backgrounding.

You could use [self performSelector: withObject: afterDelay:] to insert a bit of time between the dismissal of the alert and the backgrounding of the app. withDelay is a float and accepts fractions of seconds, so you can fine tune it pretty nicely.


Just in case anyone else is looking for the code that worked for me, here it is:

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex == 1) { // Ok button
        // URL to be opened
        NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"tel:%@", [self phoneNumber]]];

        [alertView dismissWithClickedButtonIndex:buttonIndex animated:YES];
        [[UIApplication sharedApplication] openURL:url];
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消