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

时间:2015-09-19 08:13:33

标签: objective-c ios9

iOS9打开另一个应用程序urlSchemes首先会弹出确认alertView。如何按下取消按钮获取callBack。

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

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

3 个答案:

答案 0 :(得分: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];
}

答案 1 :(得分:0)

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

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

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

再次感谢海报自己找出解决方案。

答案 2 :(得分: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"]];