Today Extension "Unable To Load"
我正在使用 Parse 将数据加载到我的 Today Extension 中,然后更新我的 Widget 文本字段。
当我运行我的代码时似乎没有问题,事实上,为了调试我的扩展中的代码,我将该函数复制到我的主应用程序中并运行它,以便我可以看到输出(我知道它可以调试小部件,但它似乎是一个非常混乱和麻烦的过程)。所有的输出都是正确的,所以当它在我的小部件中不起作用时,我完全傻眼了。
我认为它在我的小部件扩展中不起作用,因为我在小部件视图中获得了"无法加载"文本字段。
我使用的函数叫做"update_widget",我在我的Widget "TodayViewController"中的"super.viewDidLoad()"中调用"update_widget"
我是否以错误的方式更新我的小部件?
关于为什么会这样的任何想法?
"TodayViewController.swift" 的代码如下。假设 Parse ID 和查询 ID 是正确的,而不是 "HiFriends"。假设 Day 类已编写并且工作正常。最后请记住,如果我将 "update_widget" 粘贴到我的主应用程序 "AppDelegate.swift" 中,所有输出都是正确的并且不会发生崩溃。深入研究。
另外,如果它有帮助,我注意到在显示"无法加载"之前,小部件会显示"正在加载",我在"update_widget"函数中查询之前会打印它。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 | // // TodayViewController.swift // WidgetExtention // // Created by Vishnu Murale on 6/9/15. // Copyright (c) 2015 Vishnu Murale. All rights reserved. // import UIKit import NotificationCenter import Parse class TodayViewController: UIViewController, NCWidgetProviding { @IBOutlet weak var WidgetText: UILabel! let BlockOrder = ["A","B","C","D","E","F","G"] var Schedule = PFObject(className:"Schedule") let defaults = NSUserDefaults.standardUserDefaults() override func viewDidLoad() { super.viewDidLoad() Parse.setApplicationId("HiFriends", clientKey:"HiFriends") self.update_widget() } func find_Minutes(hour_before : Int, hour_after : Int)->Int{ var num_hours_less = Int(hour_before/100) var num_hours_more = Int(hour_after/100) var diff_hours = num_hours_more-num_hours_less var diff_minutes = hour_after%100 - hour_before%100 return diff_hours*60 + diff_minutes; } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } func substring(origin :String, StartIndex : Int, EndIndex : Int)->String{ var counter = 0 var subString ="" for char in origin{ if(StartIndex <= counter && counter < EndIndex){ subString += String(char) } counter++; } return subString } func widgetPerformUpdateWithCompletionHandler(completionHandler: ((NCUpdateResult) -> Void)!) { } func update_widget() -> Bool{ var Success = false; var Days = [Day]() var query = PFQuery(className:"Schedule") self.WidgetText.text = "Loading" query.getObjectInBackgroundWithId("HiFriends") { (SchedObj: PFObject?, error: NSError?) -> Void in if error == nil && SchedObj != nil { var Widget_Block = [Array<String>]() var Time_Block = [Array<String>]() Widget_Block.removeAll() Time_Block.removeAll() println("Retrived Information Successful") self.Schedule = SchedObj! print(self.Schedule) var Monday = Day(name:"Monday") var Tuesday = Day(name:"Tuesday") var Wednesday = Day(name:"Wednesday") var Thursday = Day(name:"Thursday") var Friday = Day(name:"Friday") if(self.Schedule["MondayBlock"] != nil && self.Schedule["MondayTime"] != nil){ var bO: Array<String> = self.Schedule["MondayBlock"] as! Array<String> var t: Array<String> = self.Schedule["MondayTime"] as! Array<String> Widget_Block.append(bO) Time_Block.append(t) Monday.refreshDay(bO, times: t) } if(self.Schedule["TuesdayBlock"] != nil && self.Schedule["TuesdayTime"] != nil){ var bO: Array<String> = self.Schedule["TuesdayBlock"] as! Array<String> var t: Array<String> = self.Schedule["TuesdayTime"] as! Array<String> Widget_Block.append(bO) Time_Block.append(t) Tuesday.refreshDay(bO, times: t) } if(self.Schedule["WednesdayBlock"] != nil && self.Schedule["WednesdayTime"] != nil){ var bO: Array<String> = self.Schedule["WednesdayBlock"] as! Array<String> var t: Array<String> = self.Schedule["WednesdayTime"] as! Array<String> Widget_Block.append(bO) Time_Block.append(t) Wednesday.refreshDay(bO, times: t) } if(self.Schedule["ThursdayBlock"] != nil && self.Schedule["ThursdayTime"] != nil){ var bO: Array<String> = self.Schedule["ThursdayBlock"] as! Array<String> var t: Array<String> = self.Schedule["ThursdayTime"] as! Array<String> Widget_Block.append(bO) Time_Block.append(t) Thursday.refreshDay(bO, times: t) } if(self.Schedule["FridayBlock"] != nil && self.Schedule["FridayTime"] != nil){ var bO: Array<String> = self.Schedule["FridayBlock"] as! Array<String> var t: Array<String> = self.Schedule["FridayTime"] as! Array<String> Widget_Block.append(bO) Time_Block.append(t) Friday.refreshDay(bO, times: t) } Days.append(Monday) Days.append(Tuesday) Days.append(Wednesday) Days.append(Thursday) Days.append(Friday) var currentDateTime = Monday.getDate_AsString() var Day_Num = Monday.getDayOfWeek_fromString(currentDateTime) var User_Info = [String]() let defaults = NSUserDefaults.standardUserDefaults() if(defaults.objectForKey("ButtonTexts") != nil){ User_Info = defaults.objectForKey("ButtonTexts") as! Array<String> } var Switch_Info = [Bool]() if(defaults.objectForKey("SwitchValues") != nil){ Switch_Info = defaults.objectForKey("SwitchValues") as! Array<Bool> } println("") println("Finding Widget Date ...") var End_Times = [Int]() if(self.Schedule["EndTimes"] != nil){ End_Times = self.Schedule["EndTimes"] as! Array<Int> } var minutes_until_nextblock = 0; var blockName_Widget ="" if(Day_Num >= 0 && Day_Num <= 4){ for i in reverse(0...Widget_Block[Day_Num].count-1){ var dateAfter = Time_Block[Day_Num][i] var CurrTime = Monday.NSDateToStringWidget(NSDate()) var End_Time_String ="" if(i+1 <= Widget_Block[Day_Num].count-1){ End_Time_String = Time_Block[Day_Num][i+1] } println("Date After :" + dateAfter) println("Current Date :" + CurrTime) var hour4 = self.substring(dateAfter,StartIndex: 1,EndIndex: 3) hour4 = hour4 + self.substring(dateAfter,StartIndex: 4,EndIndex: 6) var hour2 = self.substring(CurrTime,StartIndex: 1,EndIndex: 3) hour2 = hour2 + self.substring(CurrTime,StartIndex: 4,EndIndex: 6) var end_time = self.substring(End_Time_String,StartIndex: 1,EndIndex: 3) end_time = end_time + self.substring(End_Time_String,StartIndex: 4,EndIndex: 6) var hour_one = hour4.toInt() var hour_two = hour2.toInt() var hour_after = end_time.toInt() println("Blcok Date hour :") print(hour_one) println("Current Date hour:") print(hour_two) println("After Date hour :") println(hour_after) if(i == Widget_Block[Day_Num].count-1 && hour_two >= hour_one){ var EndTime = End_Times[Day_Num] if(hour_two! - EndTime < 0){ minutes_until_nextblock = self.find_Minutes(hour_two!, hour_after: (EndTime-5)) println("Miuntes until next blok" + String(minutes_until_nextblock)) if(minutes_until_nextblock > 0){ blockName_Widget = Widget_Block[Day_Num][i] } else{ blockName_Widget ="GETTOCLASS" } } else{ println("After School") blockName_Widget ="NOCLASSNOW" } break; } if(hour_two >= hour_one){ minutes_until_nextblock = self.find_Minutes(hour_two!, hour_after: (hour_after!-5)) println("Miuntes unitl next block" + String(minutes_until_nextblock)) if(minutes_until_nextblock > 0){ blockName_Widget = Widget_Block[Day_Num][i] } else{ blockName_Widget ="GETTOCLASS" } break; } } } if(blockName_Widget ==""){ blockName_Widget ="NOCLASSNOW" } println("Found the block name Widget" + blockName_Widget ) if(blockName_Widget =="NOCLASSNOW"){ println("NO Class Now") } else if(blockName_Widget =="GETTOCLASS"){ println("Passing Time") } else{ var firstLunch = Switch_Info[Day_Num] for (date,block) in Days[Day_Num].time_to_block{ var message = block; var block_copy = block; if(count(block_copy) == 2) { if(firstLunch && block_copy.hasSuffix("1")){ message ="Lunch" } else if(!firstLunch && block_copy.hasSuffix("2")){ message ="Lunch" } else{ var counterDigit = 0; for i in block_copy{ if(counterDigit == 0){ block_copy = String(i) } counterDigit++; } } } if(find(self.BlockOrder,block_copy) != nil){ var indexOfUserInfo = find(self.BlockOrder,block_copy)! if(User_Info[indexOfUserInfo] !=""){ message = User_Info[indexOfUserInfo] } } if(block == blockName_Widget){ println("have found widget block" + block) println("Message on Widget" + message) self.WidgetText.text = message break; } } } Success = true; } else { println("Information not received succesfully") } } return Success } |
}
最后错误很简单:我忘记了为了在我的主应用程序和我的小部件之间共享,我需要在 NSUserDefaults 中包含套件名称。小部件不断获取空对象并崩溃。