MBProgressHUD not working in swift: cannot import and use

8.9k views Asked by At

I used cocoapods to install MBProgressHUB and in bridging header I cannot just do

 #import "MBProgressHUD.h"

I changed to

#import "MBProgressHUD/MBProgressHUD.h"

the import is OK but I cannot use it in swift code? anything I do wrong? how can I solve this problem?

4

There are 4 answers

13
JRG-Developer On BEST ANSWER

Try this:

1) Specify use_frameworks! in your Podfile to use frameworks (instead of static libraries).

This is required for adding pods that are written in Swift as dependencies and a good idea in general if your app is written in Swift.

2) Do pod install

This makes sure your project is setup to actually use the above.

3) Add #import <MBProgressHUD/MBProgressHUD.h> in your bridging header (notice the angle brackets- not quotes) and import MBProgressHUD in the Swift class that needs to use it.

That is,

MyApp-Bridging-Header.h :

#import <MBProgressHUD/MBProgressHUD.h>
// ... other imports ...

This exposes the Objective-C files to Swift. Angle brackets indicate this is actually importing a framework.

MyViewController.swift :

import UIKit
import MBProgressHUD
// ... other imports...

class MyViewController: UIViewController {
  // ... yada yada...
}

This actually imports the dependency for use by your view controller.

0
Sanjay Shingarwad On
You can directly drag MBProgressHUD Folder to your swift project, It will create the Bridging header as named "YourAppName-Bridging-Header.h", it means you can import obj-c classes into your swift project.



'import UIKit
import MBProgressHUD

class MyViewController: UIViewController {
  // write your code
}'
This actually imports the dependency for use by your view controller.
1
Renish Dadhaniya On

Create Extension to Easy to use and throughout application

extension UIViewController {

    func showHUD(progressLabel:String){
        DispatchQueue.main.async{
            let progressHUD = MBProgressHUD.showAdded(to: self.view, animated: true)
            progressHUD.label.text = progressLabel
        }
    }

    func dismissHUD(isAnimated:Bool) {
        DispatchQueue.main.async{
            MBProgressHUD.hide(for: self.view, animated: isAnimated)
        }
    }
}

USAGE:

1. SHOW - self.showHUD(progressLabel: "Loading...")

2. HIDE - self.dismissHUD(isAnimated: true)

0
BHAVIK On

You Can Directly Use Like This In Swift 3 After Pod and Bridging File Add.

  var hud = MBProgressHUD()
  hud = MBProgressHUD.showAdded(to: navigationController?.view, animated: 
  true)
  // Set the custom view mode to show any view.
  hud.mode = MBProgressHUDModeCustomView
  // Set an image view with a checkmark.
  let gifmanager = SwiftyGifManager(memoryLimit:20)
  let gif = UIImage(gifName: "miniballs1.gif")
  let imageview = UIImageView(gifImage: gif, manager: gifmanager)
  hud.labelText = NSLocalizedString("Loading", comment: "")
  hud.labelColor = UIColor.red
  imageview.frame = CGRect(x: 0 , y: 0, width: 25 , height: 25)
  hud.customView = imageview
  // Looks a bit nicer if we make it square.
  hud.show(true)