1

enter image description here

Is there any way I can show an image from web url in my already shown UIAlertController's action. The real scenario is I've presented the UIAlertController and side by side I'm fetching the images from url. But now I wanted to update the UIAlertAction after image's are downloaded, but they aren't updating.

Later If i open the UIAlertController than those images get displayed in UIAlertAction.

let alertController = UIAlertController(title: "My App", message: "Select option:", preferredStyle: .actionSheet)

        for url in arrUrl {
            let action = UIAlertAction(title: "url", style: .default) { (action) in

            }

            downloadImage(url) { (image) in
                if image != nil {
                    action.setValue(image, forKey: "image")
                }
            }
            alertController.addAction(action)
        }

func downloadImage(_ strUrl: String, completionHandler: @escaping(_ image: UIImage?) -> ()) {

        SDWebImageManager.shared().loadImage(
            with: URL(string: strUrl),
            options: .highPriority,
            progress: nil) { (image, data, error, cacheType, isFinished, imageUrl) in
                completionHandler(image)

        }
    }
Pratik Sodha
  • 3,679
  • 2
  • 19
  • 38
Rohitax Rajguru
  • 893
  • 2
  • 13
  • 35

1 Answers1

0

Yes, this can be done. You can extend the pattern used at the blog post you referenced (https://medium.com/@maximbilan/ios-uialertcontroller-customization-5cfd88140db8).

Instead of using a UISwitch, you can use a UIImageView. You can save the UIViewController that holds the UIImageView IBOutlet in your presenting view controller.

After fetching an image, you can update the image property of the UIImageView.

Disclaimer:

I'm pretty sure this alertAction.setValue(..., forKey: "contentViewController") business is an unsupported hack as the contentViewController property does not appear to be documented. So, although this may work today, it could break at any time. It could also get rejected when submitted to the AppStore.

Use at your own risk

Daniel
  • 8,794
  • 4
  • 48
  • 71
  • If you check the blog for setting switch too they have used setValue function on alertAction. – Rohitax Rajguru Feb 14 '19 at 13:04
  • Just because it’s mentioned in a blog post does not mean it’s officially supported. Apple’s UIAlertAction documentation makes no mention of this property. The fact that you have to use setValue, and there is no simple documented property, strongly suggests this is a private capability discovered through reverse engineering. – Daniel Feb 14 '19 at 15:38