iOS9 打开其他应用先弹出alertView,获取取消按钮callBack

iOS9 open other app first pop up alertView,get the cancel button callBack

iOS9首先通过urlSchemes打开另一个应用程序将弹出确认alertView.How以在按下取消按钮时获得回调。

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

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

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

- (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];
}

正如@Alan 自己指出的那样,可以使用以下 UIApplicationDelegate 方法来确定 openURL UIAlertView:

的统计数据
  • 显示警报视图:applicationWillResignActive
  • 用户选择打开URL,导致实际应用切换:applicationDidEnterBackground
  • 通知视图被取消或应用程序被切回:is-processing-app-switching 将被调用

我想像 "is-processing-app-switching" 这样基于 NSUserDefaults 的标志可以在 openURL 调用之前设置为 true,当它为真时 applicationDidEnterBackground 可以用于确定用户是否选择取消。在 is-processing-app-switching 中,标志应设置为 false。这种解决方法对我来说似乎很难看,但是当你真的想确定 UIAlertView 中构建的统计数据时,至少是可行的。

再次感谢发帖者自己想出解决办法

在拨打电话的情况下,为了控制用户何时触摸“取消”或“呼叫”按钮,我使用 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"]];