Skip to content

Commit 7c08d27

Browse files
author
Dimitar Tachev
authored
Merge pull request #66 from aliyksel/master
Handle user pressing cancel button for ios/ add Error for Android
2 parents 55cfe61 + 3aba531 commit 7c08d27

File tree

2 files changed

+12
-5
lines changed

2 files changed

+12
-5
lines changed

src/camera.android.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,9 @@ export let takePicture = function (options?): Promise<any> {
125125
keepAspectRatio: shouldKeepAspectRatio
126126
};
127127
resolve(asset);
128+
} else if (resultCode === android.app.Activity.RESULT_CANCELED) {
129+
// User cancelled the image capture
130+
reject(new Error("cancelled"));
128131
}
129132
});
130133

src/camera.ios.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,19 +12,22 @@ class UIImagePickerControllerDelegateImpl extends NSObject implements UIImagePic
1212
}
1313

1414
private _callback: (result?) => void;
15+
private _errorCallback: (result?) => void;
1516

1617
private _width: number;
1718
private _height: number;
1819
private _keepAspectRatio: boolean;
1920
private _saveToGallery: boolean;
2021

21-
public initWithCallback(callback: (result?) => void): UIImagePickerControllerDelegateImpl {
22+
public initWithCallback(callback: (result?) => void, errorCallback: (result?) => void): UIImagePickerControllerDelegateImpl {
2223
this._callback = callback;
24+
this._errorCallback = errorCallback;
2325
return this;
2426
}
2527

26-
public initWithCallbackAndOptions(callback: (result?) => void, options?): UIImagePickerControllerDelegateImpl {
28+
public initWithCallbackAndOptions(callback: (result?) => void, errorCallback: (result?) => void, options?): UIImagePickerControllerDelegateImpl {
2729
this._callback = callback;
30+
this._errorCallback = errorCallback;
2831
if (options) {
2932
this._width = options.width;
3033
this._height = options.height;
@@ -117,6 +120,7 @@ class UIImagePickerControllerDelegateImpl extends NSObject implements UIImagePic
117120
imagePickerControllerDidCancel(picker): void {
118121
picker.presentingViewController.dismissViewControllerAnimatedCompletion(true, null);
119122
listener = null;
123+
this._errorCallback(new Error("cancelled"));
120124
}
121125
}
122126

@@ -144,13 +148,13 @@ export let takePicture = function (options): Promise<any> {
144148

145149
if (reqWidth && reqHeight) {
146150
listener = UIImagePickerControllerDelegateImpl.new().initWithCallbackAndOptions(
147-
resolve, { width: reqWidth, height: reqHeight, keepAspectRatio: keepAspectRatio, saveToGallery: saveToGallery });
151+
resolve, reject, { width: reqWidth, height: reqHeight, keepAspectRatio: keepAspectRatio, saveToGallery: saveToGallery });
148152
} else if (saveToGallery) {
149153
listener = UIImagePickerControllerDelegateImpl.new().initWithCallbackAndOptions(
150-
resolve, { saveToGallery: saveToGallery, keepAspectRatio: keepAspectRatio });
154+
resolve, reject, { saveToGallery: saveToGallery, keepAspectRatio: keepAspectRatio });
151155
}
152156
else {
153-
listener = UIImagePickerControllerDelegateImpl.new().initWithCallback(resolve);
157+
listener = UIImagePickerControllerDelegateImpl.new().initWithCallback(resolve, reject);
154158
}
155159
imagePickerController.delegate = listener;
156160

0 commit comments

Comments
 (0)