[iOS9首先打开其他应用,然后弹出alertView,获取取消按钮的回叫

问题描述 投票:3回答:3

[iOS9首先通过urlSchemes打开另一个应用程序将弹出确认alertView。如何在按下取消按钮时获取回叫。

[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"app://"]];

http://i.stack.imgur.com/L1t0b.png

objective-c ios9
3个回答
0
投票

正如@Alan指出的那样,以下UIApplicationDelegate方法可用于确定openURL UIAlertView的统计信息:

  • 显示警告视图:applicationWillResignActive
  • 用户选择打开的URL,它会导致实际的应用切换:applicationDidEnterBackground
  • 警告视图被取消或应用被切换回:is-processing-app-switching将被调用

我想可以在NSUserDefaults调用之前将基于true的标志,例如“正在处理应用程序切换”设置为openURL,并且当它为true时,可以使用applicationDidEnterBackground确定用户是否选择是否取消。在is-processing-app-switching中,标志应设置为false。这种解决方法对我来说似乎很难看,但是当您真的想确定UIAlertView中构建的统计信息时,至少是可行的。

再次感谢张贴者亲自找出解决方案。


0
投票

使用此方法在执行操作之前进行确认:

- (void)openAppAfterConfirmation
{
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"Open App?"
                                                                   message:@"OK to open app, Cancel to "
                                                            preferredStyle:UIAlertControllerStyleAlert];

    UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"Cancel"
                                                     style:UIAlertActionStyleCancel
                                                   handler:^(UIAlertAction * _Nonnull action) {

                                                       //do the CANCEL stuff here
                                                   }];

    UIAlertAction *confirm = [UIAlertAction actionWithTitle:@"Confirm"
                                                      style:UIAlertActionStyleDefault
                                                    handler:^(UIAlertAction * _Nonnull action) {

                                                        //do the CONFIRM stuff here
                                                        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"app://"]];
                                                    }];

    [alert addAction:cancel];
    [alert addAction:confirm];

    [self presentViewController:alert animated:YES completion:nil];
}

0
投票

为了控制用户何时触摸“取消”或“呼叫”按钮,我正在使用CallKit框架中的CXCallObserver来检测呼叫更改。

还添加了一个观察者来捕获应用程序,该应用程序在从调用请求返回后变得活跃。

- (void)viewDidLoad {
    [super viewDidLoad];

    CXCallObserver *callObserver = [[CXCallObserver alloc] init];
    // If queue is nil, then callbacks will be performed on main queue
    [callObserver setDelegate:self queue:nil];
    // Don't forget to store reference to callObserver, to prevent it from being released
    self.callObserver = callObserver;

    // Add an observer on application become active and set a selector to perform
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(handleAppBecomeActive:)
                                                 name:UIApplicationDidBecomeActiveNotification
                                              object :nil];

}

这里我们委托呼叫状态的更改。

#pragma mark - CXCallObserverDelegate Delegate
- (void)callObserver:(CXCallObserver *)callObserver callChanged:(CXCall *)call {
    if (call.outgoing) {
        // Set flag to true
        [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"on-going-call"];
    }

}

[handleAppBecomeActive选择器,当观察者对以下内容做出反应时执行:

UIApplicationDidBecomeActiveNotification

- (void)handleAppBecomeActive {
    // Get flag
    bool onGoingCall = [[NSUserDefaults standardUserDefaults] boolForKey:"on-goin-call"];

    if (onGoingCall) {
        [[NSUserDefaults standardUserDefaults] setBool:NO forKey:@"on-going-call"];
        // The user chose the Call action, do something
    }
}

不太花哨,但是它可以帮助检测用户在调用方法时选择了什么动作:

[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"tel://111111111"]];
© www.soinside.com 2019 - 2024. All rights reserved.