11

With iOS 9, all of my UIImagePickerControllers are now crashing if I do a force touch on the presented images. Error message is :

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '+[NSObject previewingContext:viewControllerForLocation:]: unrecognized selector sent to class 0x1a0752020'

I guess this is an Apple bug, but has anybody a work around ?

rmaddy
  • 314,917
  • 42
  • 532
  • 579
AirXygène
  • 2,409
  • 15
  • 34

3 Answers3

13

Answer is not clear way to fix issuse. And you can get a reject from apple by using Private API.

PUPhotoGridViewController is a simple UICollectionViewController and you can write extension for not implemented method.

extension UICollectionViewController: UIViewControllerPreviewingDelegate {
    public func previewingContext(_ previewingContext: UIViewControllerPreviewing, viewControllerForLocation location: CGPoint) -> UIViewController? {
        return nil
    }

    public func previewingContext(_ previewingContext: UIViewControllerPreviewing, commit viewControllerToCommit: UIViewController) {

    }
}
Dongjin Suh
  • 448
  • 4
  • 12
Antigp
  • 865
  • 6
  • 12
  • I like that you "play" only with public classes - though I did not get rejected by Apple - but what will happen when Apple release a fix ? Wouldn't it be some kind of a conflict between the extension's method and the class method ? Isn't nolanw catch of the exception is more future-safe ? – AirXygène Dec 25 '15 at 17:38
  • If apple write fix by implementing this method in PUPhotoGridViewController it will be overridden. Because PUPhotoGridViewController is SUBclass of UICollectionViewController and all function of base class such as extensions will be overridden. You can try this https://gist.github.com/antigp/bc88dc1ba1238ceff8e0 – Antigp Dec 28 '15 at 16:12
  • Yes, understood. Thanks. – AirXygène Dec 28 '15 at 18:28
  • This should be the accepted answer while we wait for the fix! – hyouuu Jan 12 '16 at 07:21
  • This also works, but why stop at UICollectionViewController? Might as well stick the category on UIViewController. – nolanw Jan 30 '16 at 18:09
  • Yes, you may use it on UIViewController. – Antigp Feb 03 '16 at 14:55
7

Here's a workaround: https://gist.github.com/nolanw/bd0a8997632fe92a9f83 (warning: swizzles a method on a private class, which should probably make you queasy). Stick those files in your project, then call MSDPreventImagePickerCrashOn3DTouch from somewhere (e.g. -applicationDidFinishLaunching:…).

The issue seems to be that a private class named PUPhotosGridViewController calls the UIViewControllerPreviewing method on its superclass, which does not implement that method. The workaround swizzles the offending method and tries to call the original implementation, but it swallows the exception so we don't crash. Hopefully, by doing it this way, if/when it gets fixed then the workaround doesn't affect that fix.

nolanw
  • 475
  • 2
  • 14
  • Waow ! Incredibly enough, this is working just fine. If I want to be picky, this leaves a warning "Undeclared selector msd_previewingContext:viewControllerForLocation:". So many thanks ! – AirXygène Nov 24 '15 at 18:38
  • @AirXygène Good catch; I've updated the gist to suppress the warning. – nolanw Nov 25 '15 at 16:00
  • even though you mention that swizzling a method on a private class should make anyone queasy, I want to add for good measure that people should use the OTHER solution and simply write an extension for `UICollectionViewController` and `UIViewController`. Besides being safer and better practice, its also less lines of code you need to add! And you'll sleep better at night ;) – Ilias Karim Mar 09 '16 at 19:07
4

Apples bug unfortunately. You just have to wait for a fix. https://forums.developer.apple.com/thread/21932

Sebastian
  • 625
  • 3
  • 13