2014-10-22 56 views
8

我试图得到解析推送通知我的应用程序(所有SWIFT)工作,但在落实,我得到的错误'PFInstallation' does not have a member named 'saveInBackground'解析推送通知 - 斯威夫特安装不工作

这里是我的代码。

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

    Parse.setApplicationId("APP ID HIDDEN", clientKey: "CLIENT ID HIDDEN") 

    // let notificationTypes:UIUserNotificationType = UIUserNotificationType.Alert | UIUserNotificationType.Badge | UIUserNotificationType.Sound 
    //let notificationSettings:UIUserNotificationSettings = UIUserNotificationSettings(forTypes: notificationTypes, categories: nil) 
    var notificationType: UIUserNotificationType = UIUserNotificationType.Alert | UIUserNotificationType.Badge | UIUserNotificationType.Sound 

    var settings: UIUserNotificationSettings = UIUserNotificationSettings(forTypes: notificationType, categories: nil) 
    UIApplication.sharedApplication().registerUserNotificationSettings(settings) 
    UIApplication.sharedApplication().registerForRemoteNotifications() 

    //UIApplication.sharedApplication().registerUserNotificationSettings(notificationSettings) 
    // Override point for customization after application launch. 
    return true 
} 

func application(application: UIApplication, didRegisterUserNotificationSettings notificationSettings: UIUserNotificationSettings!) { 
    UIApplication.sharedApplication().registerForRemoteNotifications() 

} 

func application(application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData) { 

    var currentInstallation: PFInstallation = PFInstallation() 
    currentInstallation.setDeviceTokenFromData(deviceToken) 
    currentInstallation.saveInBackground() 

    println("got device id! \(deviceToken)") 

} 


func application(application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: NSError) { 
    println(error.localizedDescription) 
    println("could not register: \(error)") 
} 

func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject]) { 
    PFPush.handlePush(userInfo) 
} 

当我改变currentInstallation.saveInBackgroundcurrentInstallation.saveEvenutally(),代码编译好..

但是,试图成功地注册了推送通知时,错误控制台说Error: deviceType must be specified in this operation (Code: 135, Version: 1.4.2)

我弹出花了数小时试图弄清楚这一点,没有骰子,任何帮助都很感激。

回答

22

为了谁比谁有这个错误,请确保您导入螺栓框架到您的衔接头文件

这是没有在他们的废话文档概述。

解决了这个问题。

以下是代码。

#import <Parse/Parse.h> 
#import <Bolts/Bolts.h> 

只需将其添加到您的桥接头,然后您就可以走了。由于

+0

这仍然不能解决它:(我做这个+ var currentInstallation:PFInstallation = PFInstallation。currentInstallation()工作 – yoshyosh 2014-12-26 09:50:46

+1

'进口螺栓' 就是这样 – 2015-08-20 10:47:50

7

有效的PFInstallation只能通过[PFInstallation currentInstallation]实例化,因为所需的标识符字段是只读的。 (source

所以不是:

var currentInstallation: PFInstallation = PFInstallation() 

尝试:

var currentInstallation = PFInstallation.currentInstallation() 
+0

好吧,那并没有太大的作用。仍然有错误“'PFInstallation'没有一个名为'saveInBackground'的成员” – 2014-10-22 06:29:21

+0

它获得deviceID很好,只是不会在Parse的一侧注册为安装。它不会收到推送的信息 – 2014-10-22 06:30:12

+0

只有在保存方法正常工作之后,才能收到推送请求。出于某种原因,currentInstallation可能是可选的?选项 - 单击它,看看它是否是'PFInstallation?类型' – Andrew 2014-10-22 06:36:31

4

只写import Bolts你AppDelegate.swift文件

+3

这应该是公认的答案。目前接受的答案不起作用。 – JYeh 2015-08-19 16:40:36

2

除了导入螺栓,我通过改变功能

func application(application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData) { 
    // Store the deviceToken in the current Installation and save it to Parse 
    let installation = PFInstallation.currentInstallation() 
    installation.setDeviceTokenFromData(deviceToken) 
    installation.saveInBackground() 
} 

从上推解析指南定格在我的应用程序相同的错误通知(而不是快速入门指南):https://parse.com/docs/ios/guide#push-notifications