今天扩展“无法加载”

时间:2015-06-17 00:44:37

标签: ios swift parse-platform ios8-today-widget

我正在使用Parse将数据加载到我的Today Extension中,然后更新我的Widget文本字段。

当我运行我的代码似乎没有问题,实际上调试我的扩展中的代码我将函数复制到我的主应用程序并运行它所以我可以看到输出(我知道可以调试小部件,但这似乎是一个非常令人困惑和麻烦的过程)。所有输出都是正确的,所以当它在我的Widget中不起作用时,我完全傻了。

我认为它在我的小部件扩展中不起作用,因为我在小部件视图中得到“无法加载”文本字段。

我使用的函数叫做“update_widget”,我在我的Widget“TodayViewController”中的“super.viewDidLoad()”中调用“update_widget”

我是以错误的方式更新我的小部件吗?

关于为什么会出现这种情况的任何想法?

“TodayViewController.swift”的代码如下。假设解析ID和查询ID是正确的而不是“HiFriends”。假设Day类已编写且工作正常。最后请记住,如果我在主应用程序“AppDelegate.swift”中粘贴“update_widget”,则所有输出都是正确的,并且不会发生崩溃。挖掘。

另外如果它有帮助,我注意到在显示“无法加载”之前,小部件显示“加载”,我在“update_widget”函数中查询之前打印。

    //
    //  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
}

}

1 个答案:

答案 0 :(得分:0)

最后错误非常简单:我忘记了为了在我的主应用程序和我的Widget之间共享,我需要在NSUserDefaults中包含套件名称。小部件不断获取空对象并崩溃。