13

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?

Junchao Gu
  • 1,815
  • 6
  • 27
  • 43

4 Answers4

29

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.

JRG-Developer
  • 12,454
  • 8
  • 55
  • 81
  • can you explain step 3? for 1, yes, I did but I thought it was only for swift based pods... – Junchao Gu Jun 10 '15 at 01:54
  • and for datetools and sdwebimage I did not use anchor but they still worked – Junchao Gu Jun 10 '15 at 01:56
  • Please see revised answer. :] – JRG-Developer Jun 10 '15 at 02:00
  • yes, the revised answer worked, would you mind explaining this a bit to me? – Junchao Gu Jun 10 '15 at 02:04
  • Added more commentary. Cheers. – JRG-Developer Jun 10 '15 at 02:18
  • I think the anchor bracket is not necessary--cause it just works in my project. I just did not know until now to import the header in swift as well – Junchao Gu Jun 10 '15 at 04:07
  • use their new lib SVProgressHUD much better with last code updates – Anton Jun 10 '15 at 07:17
  • When you're importing a framework/libraries, you should use < > brackets. It *may* work now, but will it continue to work in the future? Who knows...? See also http://stackoverflow.com/questions/1044360/import-using-angle-brackets-and-quote-marks – JRG-Developer Jun 11 '15 at 19:37
  • Basically, if it's added to the framework *or* header search path in your build settings (which Cocoapods does automatically for you), I recommend using < > brackets to import it. :] – JRG-Developer Jun 11 '15 at 19:39
  • Good information, but I was wondering if the last step necessary: import MBProgressHUD, since you have already imported it in the bridging header? I had this puzzling me that sometime you can use it directly without importing in every swift file (like Parse framework), but sometimes you have to import in every swift file to make it work (otherwise causing unidentified error). Can you shed some light upon this if you know why? Thanks – JDG Aug 18 '15 at 23:27
  • @JDG, with latest version of CocoaPods, you actually don't have to include `#import ` in your bridging header (latest CocoaPods does something similar automatically for you now). However, you still must include `import MBProgressHUD` in your Swift file to use the external code in your class. – JRG-Developer Aug 18 '15 at 23:32
  • @JRG-Developer Thanks for the quick reply! I understand that part. But that means I have to add a lot of imports to my existing project (such as adding import Parse, import ParseUI, etc.) to every single file of my project. That is of course a lot comparing to just add one line in a bridging header file. Any work around? – JDG Aug 18 '15 at 23:34
  • @JDG, I understand the import is required, but I'm not 100% sure why this is. I imagine this blog post from CocoaPods may help determine why: http://blog.cocoapods.org/Pod-Authors-Guide-to-CocoaPods-Frameworks/ If you figure out the technical reason, please feel free to share it. :) – JRG-Developer Aug 18 '15 at 23:45
2

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)
BHAVIK
  • 890
  • 7
  • 35
1
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

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)

Renish Dadhaniya
  • 10,642
  • 2
  • 31
  • 56