2014-10-22 104 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