直接从 appDelegate 切换选项卡时 iOS NSURLConnection 失败

标签 ios iphone swift nsurlconnection

我有一个带有多个选项卡的应用程序,中间的选项卡包含图像。当用户单击某个 URL 时,我希望我的应用程序跳转到选项卡栏 Controller 内的第二个选项卡,将新图像加载到 View 中。 如果应用程序已经打开,它就可以完美运行。但如果应用程序被杀死并且必须再次打开,则会失败。我认为 NSURL 连接加载速度无法达到应有的速度。

我在应用程序委托(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

我将第二个 View Controller 注册为观察者,以便在它成为应用程序激活的通知时立即加载某个 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
    }
}

最佳答案

您确定在正确的时间调用 UIApplicationDidBecomeActiveNotification 以便 readURLParameters 能够响应吗?

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

关于直接从 appDelegate 切换选项卡时 iOS NSURLConnection 失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31857319/

相关文章:

ios - "instantiateViewControllerWithIdentifier"加载时间比推送时间

swift - 为什么某些值对我的代码不起作用?

ios - AV Foundation Framework 视频质量检查

ios - 带有串行队列的 CMMotionManager

ios - XCode iOS6 CoreData NSFetchedResultsController 无法获取新数据

使用 Open Graph 的 iOS Facebook 发布失败 - 错误代码 102

ios - 预加载 UIWebView

iPhone 事件网络类型(2G、3G、WiFi)

老家伙开发的iPhone应用程序

ios - CoreGraphics - 无法将 UnsafeMutablePointer<UInt32> 替换为 UnsafeMutablePointer<UInt8>