找出用户在iOS显示的UIAlertView上选择了哪个按钮的方法,请求允许应用程序接收推送通知

Posted

技术标签:

【中文标题】找出用户在iOS显示的UIAlertView上选择了哪个按钮的方法,请求允许应用程序接收推送通知【英文标题】:Way to find out which button the user selected on the UIAlertView displayed by iOS ,asking permission to allow the app to receive push notification 【发布时间】:2014-02-10 09:55:51 【问题描述】:

有没有办法与 ios 显示的警报进行交互。例如:如果我的应用程序已经注册了自己的 APNS,在第一次启动时,iOS 会显示一个 UIAlertView(我假设它是一个),给用户两个选择。有没有办法找出用户选择了哪个按钮? 我在应用启动期间显示了两个警报,一个用于 APNS,另一个用于定位服务。有没有办法确定哪个警报用于什么?

【问题讨论】:

***.com/questions/1535403/… 这可能对您有所帮助,但不会像您期望的那样。 谢谢@CoolMonster。我确实看到了。我已经这样做了。我试图看看是否有办法处理按钮单击,就像我们为 UIAlertView 所做的那样,我创建了。 【参考方案1】:

如果您无法直接访问这些警报,我建议您从另一个角度看待这个问题。

以 CoreLocation 为例,您可以查看其[CLLocationManager authorizationStatus]

   kCLAuthorizationStatusNotDetermined = 0, // User wasn't proposed to use location services
   kCLAuthorizationStatusRestricted, // Parental control or something like that
   kCLAuthorizationStatusDenied,    // User didn't allow this application to use services
   kCLAuthorizationStatusAuthorized // User allowed to use his location.

APNS 有[[UIApplication sharedApplication] enabledRemoteNotificationTypes]

来源: https://developer.apple.com/library/ios/documentation/uikit/reference/UIApplication_Class/Reference/Reference.html#//apple_ref/doc/c_ref/UIRemoteNotificationType

https://developer.apple.com/library/ios/documentation/CoreLocation/Reference/CLLocationManager_Class/CLLocationManager/CLLocationManager.html#//apple_ref/doc/c_ref/CLAuthorizationStatus

【讨论】:

【参考方案2】:

没有。无法在操作系统创建的 AlertView 上获取回调。就像 CoolMonster 在他的评论中指出的那样,您可以找出用户为特定的 AlertView 选择了什么,并以此为基础做一些事情。

【讨论】:

谢谢@ElJay。我已经尝试过@CoolMonster 提到的内容。【参考方案3】:

我不知道推送通知,但这是在请求获取用户位置的权限时检测用户选择的一种(丑陋的)方法:

// After asking for permission, the alert is shown to the user. Since he can't do anything
// at this point but select one of the two options we simply wait...
while(([CLLocationManager authorizationStatus] == kCLAuthorizationStatusNotDetermined))

     sleep(1);

if ([CLLocationManager authorizationStatus] == kCLAuthorizationStatusAuthorized)

    NSLog(@"User allowed access to gps");

else

    NSLog(@"User denied access to gps");

【讨论】:

谢谢你,@Gad Markovits。我会试试这个。【参考方案4】:

你可以得到这样的通知类型:

UIRemoteNotificationType notificationTypes = [[UIApplication sharedApplication] enabledRemoteNotificationTypes];

并且可能会创建一些黑客方法来让用户与通知问题进行交互

【讨论】:

谢谢@nerowolfe。我确实尝试过这种方法。我也尝试在“applicationWillResignActive:”中枚举 self.window 的子视图,但我无法将 UIAlertView 视为子视图。 我认为您无法专门捕获此 UIAlertView,因此您只能稍后再查找用户是否不允许您的应用远程通知。 是的,我的想法是一样的,@nerowolfe。这似乎是不可能的。 这个特别的帖子有办法做我想做的事。 ***.com/questions/18702565/…据我所见(__attribute__((visibility("hidden")))),貌似是私有类,所以不能在要提交到应用商店的应用中使用developer.limneos.net/headers/4.0/UIKit.framework/Headers/…【参考方案5】:

虽然可能没有明确的方法来知道用户单击了哪个按钮,但您当然可以通过实现这些 UIApplication 委托方法来确定用户是否授权使用推送通知:

application:didRegisterForRemoteNotificationsWithDeviceToken:(用户接受)

application:didFailToRegisterForRemoteNotificationsWithError:(用户拒绝或无法接受)

【讨论】:

感谢您的建议。我最终使用了“enabledRemoteNotificationTypes:”。正如 Yuri Romanchenko 所建议的那样。

以上是关于找出用户在iOS显示的UIAlertView上选择了哪个按钮的方法,请求允许应用程序接收推送通知的主要内容,如果未能解决你的问题,请参考以下文章

仅在 iOS 上选择 <input> 时键盘不显示

让用户在 iOS 上选择一个电子邮件客户端

在 UIAlertView 上显示 UIActionSheet

是否可以在 iOS 13 上选择退出暗模式?

在 iOS 上选择退出 UISceneDelegate/SwiftUI

是否可以防止在网页上选择图像?