Bug 20573 - NSInvalidArgumentException when we set the CameraDevice before setting SourceType on UIImagePickerController
Summary: NSInvalidArgumentException when we set the CameraDevice before setting Source...
Alias: None
Product: iOS
Classification: Xamarin
Component: General ()
Version: 7.2.3
Hardware: PC Mac OS
: --- enhancement
Target Milestone: Untriaged
Assignee: Bugzilla
Depends on:
Reported: 2014-06-13 02:18 UTC by Prashant Cholachagudda
Modified: 2014-06-13 15:57 UTC (History)
2 users (show)

Is this bug a regression?: ---
Last known good build:

Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.

Please create a new report on Developer Community or GitHub with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:

Description Prashant Cholachagudda 2014-06-13 02:18:11 UTC
Xamarin.iOS throws an NSInvalidArgumentException when we set the CameraDevice before setting SourceType on UIImagePickerController
Aren't we allowed to set the properties in order?

Sample code that fails: 

    var picker = new UIImagePickerController();
    picker.CameraDevice = UIImagePickerControllerCameraDevice.Front; //Exception here
    picker.SourceType = UIImagePickerControllerSourceType.Camera;

Stack trace:

MonoTouch.Foundation.MonoTouchException: Objective-C exception thrown.  Name: NSInvalidArgumentException Reason: Source type must be UIImagePickerControllerSourceTypeCamera
  at (wrapper managed-to-native) MonoTouch.ObjCRuntime.Messaging:void_objc_msgSend_int (intptr,intptr,int)
  at MonoTouch.UIKit.UIImagePickerController.set_CameraDevice (UIImagePickerControllerCameraDevice value) [0x00010] in /Developer/MonoTouch/Source/monotouch/src/build/compat/UIKit/UIImagePickerController.g.cs:319 
  at FrontCameraTest.FrontCameraTestViewController.ViewDidAppear (Boolean animated) [0x0001a] in /Users/prashantvc/Projects/FrontCameraTest/FrontCameraTest/FrontCameraTestViewController.cs:46 
  at (wrapper managed-to-native) MonoTouch.UIKit.UIApplication:UIApplicationMain (int,string[],intptr,intptr)
  at MonoTouch.UIKit.UIApplication.Main (System.String[] args, System.String principalClassName, System.String delegateClassName) [0x0004c] in /Developer/MonoTouch/Source/monotouch/src/UIKit/UIApplication.cs:38 
  at FrontCameraTest.Application.Main (System.String[] args) [0x00008] in /Users/prashantvc/Projects/FrontCameraTest/FrontCameraTest/Main.cs:17
Comment 1 Sebastien Pouliot 2014-06-13 15:57:29 UTC
> Xamarin.iOS throws an NSInvalidArgumentException

This is a native exception, i.e. it's UIKit telling you it does not like what you're doing, and XI is "re-throwing" it to your code.

> Aren't we allowed to set the properties in order?

On iOS ? That sounds like it. Looking inside the stacktrace we can see that setting the device triggers a check to see if the type is a camera.

Last Exception Backtrace:
0   CoreFoundation                	0x2d61dfce __exceptionPreprocess + 126
1   libobjc.A.dylib               	0x381bccca objc_exception_throw + 34
2   UIKit                         	0x30095f64 -[UIImagePickerController _sourceTypeIsCamera] + 92
3   UIKit                         	0x301950d6 -[UIImagePickerController setCameraDevice:] + 22

With Xamarin.iOS ? same. Keep in mind we are providing bindings, i.e. we cannot change how UIKit (or other Apple frameworks) works. 

Most time they make sense, some time it does not. YMMV.