直接从appDelegate切换制表符时,iOS NSURLConnection失败

时间:2015-08-06 13:38:12

标签: ios iphone swift nsurlconnection

我有一个带有多个标签的应用,中间的一个包含图片。当用户点击某个URL,将新图像加载到视图中时,我希望我的应用程序跳转到标签栏控制器内的第二个选项卡。 如果应用程序已经打开,它可以很好地工作。但如果应用程序被杀死并且必须再次打开它就会失败。我认为NSURL连接无法尽快加载。

我在app delegate中使用此代码来切换选项卡:

func application(application: UIApplication, openURL url: NSURL, sourceApplication: String?, annotation: AnyObject?) -> Bool {

    NSLog("Calling Application Bundle ID: %@", sourceApplication!)

    if (url.scheme != nil){
        scheme = url.scheme!
    }

    if (url.query != nil){
        query = url.query
    }

    let storyboard = UIStoryboard(name: "Main", bundle: nil)
    let statusViewController = storyboard.instantiateViewControllerWithIdentifier("statuscontrollerid") as! StatusController

    let tabController = self.window!.rootViewController as! TabBarController
    tabController.selectedIndex = 1

我注册第二个视图控制器作为观察者,一旦它成为应用程序变为活动状态的通知,就会加载某个URL。

SecondViewController的代码:

override func viewWillAppear(animated: Bool) {
    //loadDefaultURLS()
    NSNotificationCenter.defaultCenter().addObserver(self, selector: "readURLParameters", name: UIApplicationDidBecomeActiveNotification, object: nil)
}

override func viewWillDisappear(animated: Bool) {
    NSNotificationCenter.defaultCenter().removeObserver(self)
}


func readURLParameters(){

    var appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
    if appDelegate.scheme != nil {
        self.scheme = appDelegate.scheme
    }

    if appDelegate.query != nil{
        let urlstring = NSURL(string:appDelegate.query)
        self.graphsURLlist.append(urlstring!)
        //self.pageImages.addObject(NSNull)
        self.pageImages.addObject(UIImage(named: "needToSynchronize.png")!)

        self.pageControl.numberOfPages++
        dispatch_group_enter(group)
        self.downloadImagesfromUrlArray()
        appDelegate.scheme = nil
        appDelegate.query = nil
    }
}

1 个答案:

答案 0 :(得分:0)

您确定在合适的时间调用UIApplicationDidBecomeActiveNotification以便readURLParameters能够回复吗?

我想等到viewDidLoad中的SecondViewController,然后从那里调用readURLParameters方法。