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

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

问题描述

我使用Swift和Xcode 6构建了一个iPhone应用程序,并使用Parse框架来处理服务。

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外部保留应用程序变量,只需执行以下操作:

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

let application = UIApplication.shared

希望有所帮助:)

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

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