3

I'm trying to use MBProgressHUD (https://github.com/jdg/MBProgressHUD) and was able to get it working with my Podfile. However, now the hud does not appear. Has anyone been able to successfully get this working with swift?

Podfile

platform :ios, '8.0'
pod 'AFNetworking'
pod 'MBProgressHUD', '~> 0.8'

MovieDetailViewController.swift

override func viewDidLoad() {
    super.viewDidLoad() // Do any additional setup after loading the view.
    var url: String = "http://api.rottentomatoes.com/api/public/v1.0/movies/"+movieID!+".json?apikey="+apiKey
    NSLog("url = \(url)")
    var request = NSURLRequest(URL: NSURL(string: url))

    // setup HUD; https://github.com/jdg/MBProgressHUD
    var hud = MBProgressHUD()
    hud.show(true)

    NSURLConnection.sendAsynchronousRequest(request, queue: NSOperationQueue.mainQueue() )     
    {
        (response: NSURLResponse!, data: NSData!, error: NSError!) -> Void in

        var err: NSError?
        var object = NSJSONSerialization.JSONObjectWithData(data, options: nil, error: &err) as NSDictionary

        if let yearInt = object["year"] as? Int {
            self.year.text = String(yearInt)
        }
        self.title = object["title"] as? String
        self.movieTitle.text = self.title

        self.synopsis.text = object["synopsis"] as? String
        self.mpaaRating.text = object["mpaa_rating"] as? String
        var ratings = object["ratings"] as NSDictionary
        var posters = object["posters"] as NSDictionary

        // setup background picture
        var posterUrl = posters["thumbnail"] as String
        var image = UIImageView()
        image.setImageWithURL(NSURL(string: posterUrl))
        image.contentMode = UIViewContentMode.ScaleAspectFill
        self.scrollView.backgroundColor = UIColor.clearColor()
        self.scrollView.addSubview(image)

        // stop the hud
        hud.hide(true)
    }

}
AG1
  • 6,648
  • 8
  • 40
  • 57
  • how did you import MBProgressHUD into your projects in the first place? I tried to use that in my project but it seems that I cannot even import it like `code`#import "MBProgressHUD.h"`code`. instead I used `code'#import "MBProgressHUD/MBProgressHUD.h"`code` but I still cannot use it in my project. I used 'use_framworks!' in cocoapods but that should not cause any issue right? – Junchao Gu Jun 10 '15 at 02:01

2 Answers2

3

You are missing the part where you add the HUD to the Window or the current view (self.view).

override func viewDidLoad() {
    super.viewDidLoad() // Do any additional setup after loading the view.
    var url: String = "http://api.rottentomatoes.com/api/public/v1.0/movies/"+movieID!+".json?apikey="+apiKey
    NSLog("url = \(url)")
    var request = NSURLRequest(URL: NSURL(string: url))

    // setup HUD; https://github.com/jdg/MBProgressHUD
    var hud = MBProgressHUD.showHUDAddedTo(self.view, animated: true)

    NSURLConnection.sendAsynchronousRequest(request, queue: NSOperationQueue.mainQueue() )     
    {
        (response: NSURLResponse!, data: NSData!, error: NSError!) -> Void in

        var err: NSError?
        var object = NSJSONSerialization.JSONObjectWithData(data, options: nil, error: &err) as NSDictionary

        if let yearInt = object["year"] as? Int {
            self.year.text = String(yearInt)
        }
        self.title = object["title"] as? String
        self.movieTitle.text = self.title

        self.synopsis.text = object["synopsis"] as? String
        self.mpaaRating.text = object["mpaa_rating"] as? String
        var ratings = object["ratings"] as NSDictionary
        var posters = object["posters"] as NSDictionary

        // setup background picture
        var posterUrl = posters["thumbnail"] as String
        var image = UIImageView()
        image.setImageWithURL(NSURL(string: posterUrl))
        image.contentMode = UIViewContentMode.ScaleAspectFill
        self.scrollView.backgroundColor = UIColor.clearColor()
        self.scrollView.addSubview(image)

        // stop the hud
        MBProgressHUD.hideAllHUDsForView(self.view, animated: true) // Or just call hud.hide(true)
    }

}
chroman
  • 1,534
  • 12
  • 18
3

I have a function like this:

func showLoadingSpinner() {
    let loading = MBProgressHUD.showHUDAddedTo(self.view, animated: true)
    loading.mode = MBProgressHUDModeDeterminate
    loading.labelText = "Loading...";
}

... that I called just before doing the asynchronous call and then when I am done processing the response, I call this function to hide it:

MBProgressHUD.hideHUDForView(self.view, animated: true)

Also make sure you imported the .h file in your bridging-header file, like this:

#import "MBProgressHUD.h"
Maricel
  • 2,089
  • 13
  • 17
  • I used cocoapods but it seems that I cannot just import like `code`#import "MBProgressHUD.h"`code`--the compilation does not pass. but `code`#import "MBProgressHUD/MBProgressHUD.h"`code` worked with compilation but I still not use it in the project--cannot compile if I use MBProgressHUD(). any help? – Junchao Gu Jun 10 '15 at 01:33