如何控制何时在 iOS 中提示用户推送通知权限 [英] How to control when to prompt user for push notification permissions in iOS

查看:44
本文介绍了如何控制何时在 iOS 中提示用户推送通知权限的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用 Swift 和 Xcode 6 以及 Parse 框架为 iPhone 构建了一个应用程序来处理服务.

I've built an application for iPhone using Swift and Xcode 6, and the Parse framework to handle services.

在遵循有关如何设置推送通知的 Parse 教程时,说明建议我将推送通知放在 App Delegate 文件中.

While following the Parse tutorials on how to set up push notifications, the instructions advised that I put the push notifications in the App Delegate file.

这是我添加到 App Delegate 文件中的代码...

This is the code that I have added to the App Delegate file...

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?
    var pushNotificationsController: PushNotificationController?


    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {

         // Register for Push Notifications
        self.pushNotificationsController = PushNotificationController()

        if application.respondsToSelector("registerUserNotificationSettings:") {
            println("registerUserNotificationSettings.RegisterForRemoteNotificatios")
            let userNotificationTypes: UIUserNotificationType = (.Alert | .Badge | .Sound)
            let settings:UIUserNotificationSettings = UIUserNotificationSettings(forTypes: userNotificationTypes, categories: nil)
            application.registerUserNotificationSettings(settings)
            application.registerForRemoteNotifications()
        }

        return true;
    }

    func application(application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData) {
        println("didRegisterForRemoteNotificationsWithDeviceToken")
        let installation = PFInstallation.currentInstallation()
        installation.setDeviceTokenFromData(deviceToken)
        installation.saveInBackground()
    }
}

因此,当应用程序第一次启动时,会提示用户授予这些权限.

So what happens is that as soon as the application is launched for the first time, the user is prompted to grant these permissions.

我想要做的是,仅在发生特定操作后(即在对应用程序功能进行演练期间)才提示这些权限,因此我可以提供更多关于为什么我们希望他们这样做的背景信息允许推送通知.

What I want to do, is only prompt for these permissions after a certain action has taken place (ie, during a walkthrough of the features of the app) so I can provide a little more context on why we would want them to allow push notifications.

是否只是在相关的 ViewController 中复制以下代码那么简单,我希望在其中提示用户?

Is it as simple as just copying the below code in the relevant ViewController where I will be expecting to prompt the user?

// In 'MainViewController.swift' file

func promptUserToRegisterPushNotifications() {
        // Register for Push Notifications
        self.pushNotificationsController = PushNotificationController()

        if application.respondsToSelector("registerUserNotificationSettings:") {
            println("registerUserNotificationSettings.RegisterForRemoteNotificatios")
            let userNotificationTypes: UIUserNotificationType = (.Alert | .Badge | .Sound)
            let settings:UIUserNotificationSettings = UIUserNotificationSettings(forTypes: userNotificationTypes, categories: nil)
            application.registerUserNotificationSettings(settings)
            application.registerForRemoteNotifications()
        }
}

func application(application: UIApplication,    didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData) {
        println("didRegisterForRemoteNotificationsWithDeviceToken")
        let installation = PFInstallation.currentInstallation()
        installation.setDeviceTokenFromData(deviceToken)
        installation.saveInBackground()
}

谢谢!

推荐答案

答案很简单.如果您希望在其他时间提示用户,例如在按下按钮时,只需将有关请求的代码移动到该函数中(或从其他地方调用 promptUserToRegisterPushNotifications()).

The answer is simple. If you want the user to be prompted some other time, for instance on a button press then simply move the code regarding the request into that function (or call promptUserToRegisterPushNotifications() from somewhere else).

要在 AppDelegate 之外获取 application 变量,只需执行以下操作:

To get a hold of the application variable outside the AppDelegate, simply do this:

let application = UIApplication.shared

希望有帮助:)

这篇关于如何控制何时在 iOS 中提示用户推送通知权限的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆