Adding Activity Indicator to UIAlertView

For those using Objective-C. My solution wraps the UIActivityIndicatorView with a bigger view so the margin to the edge of UIAlertView is not so small.

#import "UIAlertView+ActivityIndicator.h"

@implementation UIAlertView (ActivityIndicator)

+ (UIAlertView *)alertViewWithActivityIndicator:(NSString *)title delegate:(id<UIAlertViewDelegate>)delegate;
{
    UIAlertView *a = [[UIAlertView alloc] initWithTitle:title
                                                message:nil
                                               delegate:delegate
                                      cancelButtonTitle:nil
                                      otherButtonTitles:nil];

    UIView *v = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 50, 50)];
    UIActivityIndicatorView *aiv = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
    aiv.translatesAutoresizingMaskIntoConstraints = NO;
    [v addSubview:aiv];
    [v addConstraint:[NSLayoutConstraint constraintWithItem:aiv
                                                  attribute:NSLayoutAttributeCenterX
                                                  relatedBy:NSLayoutRelationEqual
                                                     toItem:v
                                                  attribute:NSLayoutAttributeCenterX
                                                 multiplier:1.0
                                                   constant:0]];
    [v addConstraint:[NSLayoutConstraint constraintWithItem:aiv
                                                  attribute:NSLayoutAttributeCenterY
                                                  relatedBy:NSLayoutRelationEqual
                                                     toItem:v
                                                  attribute:NSLayoutAttributeCenterY
                                                 multiplier:1.0
                                                   constant:0]];
    [aiv startAnimating];
    [a setValue:v forKey:@"accessoryView"];

    return a;
}

@end

For swift 4 Create an alert controller extension

extension UIAlertController {

    private struct ActivityIndicatorData {
        static var activityIndicator = UIActivityIndicatorView(frame: CGRect(x: 0, y: 0, width: 40, height: 40))
    }

    func addActivityIndicator() {
        let vc = UIViewController()
        vc.preferredContentSize = CGSize(width: 40,height: 40)
        ActivityIndicatorData.activityIndicator.color = UIColor.blue
        ActivityIndicatorData.activityIndicator.startAnimating()
        vc.view.addSubview(ActivityIndicatorData.activityIndicator)
        self.setValue(vc, forKey: "contentViewController")
    }

    func dismissActivityIndicator() {
        ActivityIndicatorData.activityIndicator.stopAnimating()
        self.dismiss(animated: false)
    }
}

Create Var a ctivityIndicatorAlert in your view controller and write display and dismiss methods in view controller like below

var activityIndicatorAlert: UIAlertController?

func displayActivityIndicatorAlert() {
    activityIndicatorAlert = UIAlertController(title: NSLocalizedString("Loading", comment: ""), message: NSLocalizedString("PleaseWait", comment: "") + "...", preferredStyle:  UIAlertController.Style.alert)
    activityIndicatorAlert!.addActivityIndicator()
    var topController:UIViewController = UIApplication.shared.keyWindow!.rootViewController!
    while ((topController.presentedViewController) != nil) {
        topController = topController.presentedViewController!
    }
    topController.present(activityIndicatorAlert!, animated:true, completion:nil)
}

func dismissActivityIndicatorAlert() {
    activityIndicatorAlert!.dismissActivityIndicator()
    activityIndicatorAlert = nil
}

You can also add UIViewcontroller extension For globally available above all methods.

extension UIViewController {
    private struct activityAlert {
        static var activityIndicatorAlert: UIAlertController?
    }
    //completion : ((Int, String) -> Void)?)
    func displayIPActivityAlert(_ onCancel : (()-> Void)?) {
        activityAlert.activityIndicatorAlert = UIAlertController(title: NSLocalizedString("Loading...", comment: ""), message: nil , preferredStyle: UIAlertController.Style.alert)
        activityAlert.activityIndicatorAlert!.addActivityIndicator()
        var topController:UIViewController = UIApplication.shared.keyWindow!.rootViewController!
        while ((topController.presentedViewController) != nil) {
            topController = topController.presentedViewController!
        }

        activityAlert.activityIndicatorAlert!.addAction(UIAlertAction.init(title:NSLocalizedString("Cancel", comment: ""), style: .default, handler: { (UIAlertAction) in
                self.dismissIPActivityAlert()
                onCancel?()
        }))
        topController.present(activityAlert.activityIndicatorAlert!, animated:true, completion:nil)
    }

    func dismissIPActivityAlert() {
        activityAlert.activityIndicatorAlert!.dismissActivityIndicator()
        activityAlert.activityIndicatorAlert = nil
    }
}

try this code!!

var alert: UIAlertView = UIAlertView(title: "Title", message: "Please wait...", delegate: nil, cancelButtonTitle: "Cancel")


var loadingIndicator: UIActivityIndicatorView = UIActivityIndicatorView(frame: CGRect(x: 50, y: 10, width: 37, height: 37)) as UIActivityIndicatorView
loadingIndicator.center = self.view.center
loadingIndicator.hidesWhenStopped = true
loadingIndicator.activityIndicatorViewStyle = UIActivityIndicatorViewStyle.Gray
loadingIndicator.startAnimating();

alert.setValue(loadingIndicator, forKey: "accessoryView")
loadingIndicator.startAnimating()

alert.show();

I hope I helped you