Objective-C
iOS

Objective-C インジケーター(くるくる)付きのアラートを表示

カテゴリで実装していますが、
ViewControllerであればコピペで使えるレベルです。

実装

UIViewController+Alert.m
#import "UIViewController+Alert.h"

@implementation UIViewController (Alert)

- (void)showIndicatorAlert:(NSString*)text
{
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"" message:text preferredStyle:UIAlertControllerStyleAlert];
    UIActivityIndicatorView *view = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
    view.center = CGPointMake(60, 30);
    [alertController.view addSubview:view];
    [view startAnimating];
    [self presentViewController:alertController animated:YES completion:nil];
}

@end
呼び出し元
[self showIndicatorAlert:@"Please wait..."];

表示結果

image.png

閉じる方法

別スレッドとかで
... 処理 ...

// 閉じる
dispatch_sync(dispatch_get_main_queue(), ^{
    [self dismissViewControllerAnimated:true completion:nil];
});

Swift版

extension UIAlertController {

    class func showIndicatorAlert(message: String, viewController: UIViewController) {
        let alert = UIAlertController(title: "",
                                      message: message,
                                      preferredStyle: .alert)

        let view = UIActivityIndicatorView(activityIndicatorStyle: .gray)
        view.center = CGPoint(x: 60, y: 30)
        view.startAnimating()
        alert.view.addSubview(view)
        viewController.present(alert,
                               animated: true,
                               completion: nil)
    }
}