0

my problem is I am working with Alamofire and I added AlamofireActivityIndicator from : AlamofireNetworkActivityIndicator GitHub

my question is how to add a view to which viewController that user is there and doing networking request , that view shows indicator instead of showing indicator in status bar !

I have a problem and I changed framework codes to :

Original code from AlamofireNetworkActivityIndicator framework :

private var activityIndicatorState: ActivityIndicatorState = .notActive {
    didSet {
        switch activityIndicatorState {
        case .notActive:
            isNetworkActivityIndicatorVisible = false
            invalidateStartDelayTimer()
            invalidateCompletionDelayTimer()
        case .delayingStart:
            scheduleStartDelayTimer()
        case .active:
            invalidateCompletionDelayTimer()
            isNetworkActivityIndicatorVisible = true
        case .delayingCompletion:
            scheduleCompletionDelayTimer()
        }
    }
}

and I've been added two method that creates and remove a red view 50*50 center of screen , I customize it later that will show indicator

fileprivate func myIndicatorView (superView:UIView) {
    let view = UIView.init()
    view.tag = 2018
    view.backgroundColor = .red
    superView.addSubview(view)
    view.frame = CGRect(x: UIScreen.main.bounds.midX-25,
                        y: UIScreen.main.bounds.midY-25,
                        width: 50,
                        height: 50)
}

fileprivate func removeIndicatorView (superView:UIView) {
    superView.subviews.forEach{ (myView) in
        if myView.tag == 2018 {
            myView.removeFromSuperview()
        }
    }
}

, but now my question is here :

private var activityIndicatorState: ActivityIndicatorState = .notActive {
    didSet {
        switch activityIndicatorState {
        case .notActive:
            self.myIndicatorView(superView: <#T##UIView#>)
            isNetworkActivityIndicatorVisible = false
            invalidateStartDelayTimer()
            invalidateCompletionDelayTimer()
        case .delayingStart:
            scheduleStartDelayTimer()
        case .active:
            self.removeIndicatorView(superView: <#T##UIView#>)
            invalidateCompletionDelayTimer()
            isNetworkActivityIndicatorVisible = true
        case .delayingCompletion:
            scheduleCompletionDelayTimer()
        }
    }
}

Question :

how to define ViewControllers view instead of <#T##UIView#> ! ?? ? this framework usage is you type one line in appDelegate and it observer for every networking request :

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
    NetworkActivityIndicatorManager.shared.isEnabled = true
    return true
}
Mohammad Reza Koohkan
  • 1,656
  • 1
  • 16
  • 36

3 Answers3

2

Instead of trying to reach the specific controller, you should broadcast a message using the Notification Pattern.

Add following line to send "Add Loader Message"

NotificationCenter.default.post(name: Notification.Name(rawValue: "AddLoader", object: nil)

Add following to dismiss:

NotificationCenter.default.post(name: Notification.Name(rawValue: "HideLoader", object: nil)

And Now in every controller ( easier if you have a Super View controller ):

  • In viewWillAppear:

NotificationCenter.default.addObserver(self, selector: #selector(yourShowSelector), name: Notification.Name(rawValue: "AddLoader"), object: nil) NotificationCenter.default.addObserver(self, selector: #selector(yourHide Selector), name: NSNotification.Name(rawValue: "HideLoader"), object: nil)

  • In viewWillDisappear

    NotificationCenter.default.removeObserver(self)

CZ54
  • 5,488
  • 1
  • 24
  • 39
1

your question not clear . but i think you want to fetch your last controller from where user is doing networking request.

Please use this code to fetch last viewcontroller from anywhere from app

extension UIApplication {

  class func topViewController(_ base: UIViewController? = UIApplication.shared.keyWindow?.rootViewController) -> UIViewController? {
      if let nav = base as? UINavigationController {
         return topViewController(nav.visibleViewController)
      }
      if let tab = base as? UITabBarController {
         if let selected = tab.selectedViewController {
            return topViewController(selected)
         }
      }
      if let presented = base?.presentedViewController {
        return topViewController(presented)
      }
      return base
   }
}
Mohammad Reza Koohkan
  • 1,656
  • 1
  • 16
  • 36
1

So if you are using CocoaPods then you can use SVProgressHUD easily in your project to show activity indicator. Check the below link

https://github.com/SVProgressHUD/SVProgressHUD

First, add the following line to your Podfile:

pod 'SVProgressHUD'

Second, install SVProgressHUD into your project:

pod install

add the following line to your Podfile:

use_frameworks!

Thats it, now you can do import SVProgressHUD ans use its methods.

Check the image below for reference.

enter image description here

After completing the above process successfully just do the following:

  1. do import SVProgressHUD
  2. use like SVProgressHUD.show() before making any network call
  3. use like SVProgressHUD.dismiss() on the completion callback.
vivekDas
  • 1,248
  • 8
  • 12