我使用 Swift 和 Xcode 6 以及 Parse 框架来处理服务,为 iPhone 构建了一个应用程序。
在遵循有关如何设置推送通知的 Parse 教程时,说明建议我将推送通知放在 App Delegate 文件中。
这是我添加到应用程序委托文件中的代码...
@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()
}
}
因此,一旦应用程序第一次启动,就会提示用户授予这些权限。
我想做的只是在发生某个操作后(即在演练应用程序的功能期间)提示这些权限,这样我就可以提供更多背景信息来说明为什么我们希望它们允许推送通知。
是否像在相关 ViewController 中复制以下代码一样简单,我希望在其中提示用户?
// 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()
}
thanks!