UIImagePickerController crashing on force touch?
Asked Answered
T

3

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 ?

Thumbscrew answered 25/10, 2015 at 16:1 Comment(0)
F
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.

Fortune answered 23/11, 2015 at 16:15 Comment(3)
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 !Eatables
@Thumbscrew Good catch; I've updated the gist to suppress the warning.Fortune
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 ;)Crispas
R
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) {

    }
}
Recitative answered 24/12, 2015 at 12:25 Comment(6)
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 ?Eatables
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 gist.github.com/antigp/bc88dc1ba1238ceff8e0Recitative
Yes, understood. Thanks.Eatables
This should be the accepted answer while we wait for the fix!Zoologist
This also works, but why stop at UICollectionViewController? Might as well stick the category on UIViewController.Fortune
Yes, you may use it on UIViewController.Recitative
F
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.

Fortune answered 23/11, 2015 at 16:15 Comment(3)
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 !Eatables
@Thumbscrew Good catch; I've updated the gist to suppress the warning.Fortune
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 ;)Crispas
R
4

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

Reconnaissance answered 13/11, 2015 at 10:57 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.