[英]Access userInfo dictionary in Swift
我正在嘗試訪問application:didReceiveRemoteNotification()
的userInfo
詞典,以便我可以根據收到的推送類型/是否收到任何推送來選擇segue。 我已經嘗試了這兩種方式(我敢肯定,在某些情況下我還是不記得)。
if let info = userInfo as? Dictionary<String,String> {
var notificationType = info["notificationType"]
}
if let info: String = userInfo["notificationType"] as? String {
//do stuff
}
我沒有任何錯誤,我什么都沒有。 如果我打印userInfo字典,則它只有一個成員[“ aps”],其中包含向用戶顯示的推送消息,因此即使我可以訪問它,也可以有條件地使用它。
我嘗試按照解析API寫入userData:
let data = ["notificationType" : "coffee"]
push.setData(data)
因此,一個問題是,這似乎沒有設置任何內容,但更大的問題是,我無法獲取任何userInfo數據。
您如何訪問該詞典?
編輯
一些打印語句結果:
println(userInfo["aps"]) => {alert = "You've Been Invited To A Coffee Order";}
println(userInfo) => [aps: {alert = "You've Been Invited To A Coffee Order";}, type: coffee]
println(userInfo[0]) => nil
let info = userInfo as? Dictionary<String,String>
println(info) => nil
因此,似乎只是警報鍵卡在了這個'aps'
數組中? 字典? 我沒有創建它,它只是userInfo
一部分
編輯2
這是我設置數據並發送推送的代碼。 我從此處發送出現在userInfo
的數據:
func notifyUserOfCoffeeOrder(){
var uQuery:PFQuery = PFUser.query()
uQuery.whereKey("username", equalTo: usernameLabel.text)
var pushQuery:PFQuery = PFInstallation.query()
pushQuery.whereKey("user", matchesQuery: uQuery)
var push:PFPush = PFPush()
push.setQuery(pushQuery)
let data = ["type" : "coffee",
"alert" : "You've Been Invited To A Coffee Order"]
push.setData(data)
push.sendPush(nil)
println("push sent")
}
這是AppDelegate.swift的全部功能,在這里我將接收到的數據拉出:
func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject]) {
NSNotificationCenter.defaultCenter().postNotificationName("getMessage", object: nil)
if let info = userInfo["type"] as? String {
self.window = UIWindow(frame: UIScreen.mainScreen().bounds)
var storyboard = UIStoryboard(name: "Main", bundle: nil)
var initialViewController = storyboard.instantiateViewControllerWithIdentifier("coffeeVC") as UIViewController
self.window?.rootViewController = initialViewController
self.window?.makeKeyAndVisible()
}
}
編輯:我知道重建此答案以符合給出的注釋。 請注意,我目前無法使用Apples Push Notification服務,因此無法在與OP相同的環境中進行測試
好像APN(或parse.com)在構建它時在userInfo
各處添加了一些內容。 因此,我最好的建議是使用以下代碼來打印您收到的double字典中的所有內容。 我還在此處提供了代碼,以解開在您在詞典中查找內容時出現的可選內容。
所以這是我修改的示例代碼:
func myApplicationFunc(didReceiveRemoteNotification userInfo: [NSObject : AnyObject]) {
// NSNotificationCenter.defaultCenter().postNotificationName("getMessage", object: nil)
// Default printout of userInfo
println("All of userInfo:\n\( userInfo)\n")
// Print all of userInfo
for (key, value) in userInfo {
println("userInfo: \(key) —> value = \(value)")
}
if let info = userInfo["aps"] as? Dictionary<String, AnyObject> {
// Default printout of info = userInfo["aps"]
println("All of info: \n\(info)\n")
for (key, value) in info {
println("APS: \(key) —> \(value)")
}
if let myType = info["type"] as? String {
// Printout of (userInfo["aps"])["type"]
println("\nFrom APS-dictionary with key \"type\": \( myType)")
// Do your stuff?
}
}
}
var userInfo : [ NSObject: AnyObject] =
[
"aps" : [
"alert" : "Coffee is not the answer",
"type" : "coffee"
],
"anotherType" : 123
]
myApplicationFunc(didReceiveRemoteNotification: userInfo)
此代碼產生以下輸出:
All of userInfo:
[anotherType: 123, aps: {
alert = "Coffee is not the answer";
type = coffee;
}]
userInfo: anotherType —> value = 123
userInfo: aps —> value = {
alert = "Coffee is not the answer";
type = coffee;
}
All of info:
[alert: Coffee is not the answer, type: coffee]
APS: alert —> Coffee is not the answer
APS: type —> coffee
From APS-dictionary with key "type": coffee
您應該能夠在我的application(...)
方法中添加我的代碼,並希望您能看到有關如何解開雙字典以及實際將哪些信息傳輸給您的信息。
PS! 在另一篇文章的答案中,討論了他的setMessage
如何干擾他的setData
,這可能也很有趣,如果有一些代碼會影響您的結果。 還是我的示例代碼,應該列出在userInfo
收到的所有內容
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.