[iOS9首先通过urlSchemes打开另一个应用程序将弹出确认alertView。如何在按下取消按钮时获取回叫。
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"app://"]];
正如@Alan指出的那样,以下UIApplicationDelegate
方法可用于确定openURL
UIAlertView
的统计信息:
applicationWillResignActive
applicationDidEnterBackground
is-processing-app-switching
将被调用我想可以在NSUserDefaults
调用之前将基于true
的标志,例如“正在处理应用程序切换”设置为openURL
,并且当它为true时,可以使用applicationDidEnterBackground
确定用户是否选择是否取消。在is-processing-app-switching
中,标志应设置为false。这种解决方法对我来说似乎很难看,但是当您真的想确定UIAlertView
中构建的统计信息时,至少是可行的。
再次感谢张贴者亲自找出解决方案。
使用此方法在执行操作之前进行确认:
- (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];
}
为了控制用户何时触摸“取消”或“呼叫”按钮,我正在使用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"]];