Skip to content

Commit 95b9959

Browse files
[camera] Remove @throw from iOS implementation (#5034)
Using `@throw` in iOS code violates the style guide, so it shouldn't be done in the plugin as mechanism for communicating errors. More importantly, `NSError` is not intended to be used with `@throw`/`@catch`, and is causing issues when compiled with the iOS 17 SDK. This removes all use of `@throw`, and all `@catch (NSError* e)`, in favor of other methods of communicating errors. It explicitly does not try to fix all the other strange things about this code (having an `NSError` out-param in an init method, using Cocoa and NSURL error domains and codes for some reason), and instead preserves existing behavior as much as possible. In practice, none of these codepaths should ever actually happen (they indicate programming errors within the plugin, not unexpected runtime behavior), and all of this code will go away when converting to Pigeon anyway, so there's not much value in trying to unwind this structure further. Fixes flutter/flutter#135195
1 parent 5862201 commit 95b9959

File tree

8 files changed

+106
-94
lines changed

8 files changed

+106
-94
lines changed

packages/camera/camera_avfoundation/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 0.9.13+6
2+
3+
* Fixes incorrect use of `NSError` that could cause crashes on launch.
4+
15
## 0.9.13+5
26

37
* Ignores audio samples until the first video sample arrives.

packages/camera/camera_avfoundation/example/ios/Runner.xcodeproj/project.pbxproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,7 @@
280280
97C146E61CF9000F007C117D /* Project object */ = {
281281
isa = PBXProject;
282282
attributes = {
283-
LastUpgradeCheck = 1300;
283+
LastUpgradeCheck = 1430;
284284
ORGANIZATIONNAME = "The Flutter Authors";
285285
TargetAttributes = {
286286
03BB76672665316900CE5A93 = {

packages/camera/camera_avfoundation/example/ios/Runner.xcodeproj/xcshareddata/xcschemes/Runner.xcscheme

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<Scheme
3-
LastUpgradeVersion = "1300"
3+
LastUpgradeVersion = "1430"
44
version = "1.3">
55
<BuildAction
66
parallelizeBuildables = "YES"

packages/camera/camera_avfoundation/example/ios/RunnerTests/CameraPropertiesTests.m

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ - (void)testFLTGetFLTFlashModeForString {
1919
XCTAssertEqual(FLTFlashModeAuto, FLTGetFLTFlashModeForString(@"auto"));
2020
XCTAssertEqual(FLTFlashModeAlways, FLTGetFLTFlashModeForString(@"always"));
2121
XCTAssertEqual(FLTFlashModeTorch, FLTGetFLTFlashModeForString(@"torch"));
22-
XCTAssertThrows(FLTGetFLTFlashModeForString(@"unkwown"));
22+
XCTAssertEqual(FLTFlashModeInvalid, FLTGetFLTFlashModeForString(@"unknown"));
2323
}
2424

2525
- (void)testFLTGetAVCaptureFlashModeForFLTFlashMode {
@@ -34,27 +34,27 @@ - (void)testFLTGetAVCaptureFlashModeForFLTFlashMode {
3434
- (void)testFLTGetStringForFLTExposureMode {
3535
XCTAssertEqualObjects(@"auto", FLTGetStringForFLTExposureMode(FLTExposureModeAuto));
3636
XCTAssertEqualObjects(@"locked", FLTGetStringForFLTExposureMode(FLTExposureModeLocked));
37-
XCTAssertThrows(FLTGetStringForFLTExposureMode(-1));
37+
XCTAssertNil(FLTGetStringForFLTExposureMode(-1));
3838
}
3939

4040
- (void)testFLTGetFLTExposureModeForString {
4141
XCTAssertEqual(FLTExposureModeAuto, FLTGetFLTExposureModeForString(@"auto"));
4242
XCTAssertEqual(FLTExposureModeLocked, FLTGetFLTExposureModeForString(@"locked"));
43-
XCTAssertThrows(FLTGetFLTExposureModeForString(@"unknown"));
43+
XCTAssertEqual(FLTExposureModeInvalid, FLTGetFLTExposureModeForString(@"unknown"));
4444
}
4545

4646
#pragma mark - focus mode tests
4747

4848
- (void)testFLTGetStringForFLTFocusMode {
4949
XCTAssertEqualObjects(@"auto", FLTGetStringForFLTFocusMode(FLTFocusModeAuto));
5050
XCTAssertEqualObjects(@"locked", FLTGetStringForFLTFocusMode(FLTFocusModeLocked));
51-
XCTAssertThrows(FLTGetStringForFLTFocusMode(-1));
51+
XCTAssertNil(FLTGetStringForFLTFocusMode(-1));
5252
}
5353

5454
- (void)testFLTGetFLTFocusModeForString {
5555
XCTAssertEqual(FLTFocusModeAuto, FLTGetFLTFocusModeForString(@"auto"));
5656
XCTAssertEqual(FLTFocusModeLocked, FLTGetFLTFocusModeForString(@"locked"));
57-
XCTAssertThrows(FLTGetFLTFocusModeForString(@"unknown"));
57+
XCTAssertEqual(FLTFocusModeInvalid, FLTGetFLTFocusModeForString(@"unknown"));
5858
}
5959

6060
#pragma mark - resolution preset tests
@@ -67,7 +67,7 @@ - (void)testFLTGetFLTResolutionPresetForString {
6767
XCTAssertEqual(FLTResolutionPresetVeryHigh, FLTGetFLTResolutionPresetForString(@"veryHigh"));
6868
XCTAssertEqual(FLTResolutionPresetUltraHigh, FLTGetFLTResolutionPresetForString(@"ultraHigh"));
6969
XCTAssertEqual(FLTResolutionPresetMax, FLTGetFLTResolutionPresetForString(@"max"));
70-
XCTAssertThrows(FLTGetFLTFlashModeForString(@"unknown"));
70+
XCTAssertEqual(FLTResolutionPresetInvalid, FLTGetFLTResolutionPresetForString(@"unknown"));
7171
}
7272

7373
#pragma mark - video format tests
@@ -89,7 +89,7 @@ - (void)testFLTGetUIDeviceOrientationForString {
8989
XCTAssertEqual(UIDeviceOrientationLandscapeLeft,
9090
FLTGetUIDeviceOrientationForString(@"landscapeRight"));
9191
XCTAssertEqual(UIDeviceOrientationPortrait, FLTGetUIDeviceOrientationForString(@"portraitUp"));
92-
XCTAssertThrows(FLTGetUIDeviceOrientationForString(@"unknown"));
92+
XCTAssertEqual(UIDeviceOrientationUnknown, FLTGetUIDeviceOrientationForString(@"unknown"));
9393
}
9494

9595
- (void)testFLTGetStringForUIDeviceOrientation {

packages/camera/camera_avfoundation/ios/Classes/CameraProperties.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ typedef NS_ENUM(NSInteger, FLTFlashMode) {
1717
FLTFlashModeAuto,
1818
FLTFlashModeAlways,
1919
FLTFlashModeTorch,
20+
// This should never occur; it indicates an unknown value was received over
21+
// the platform channel.
22+
FLTFlashModeInvalid,
2023
};
2124

2225
/**
@@ -39,6 +42,9 @@ extern AVCaptureFlashMode FLTGetAVCaptureFlashModeForFLTFlashMode(FLTFlashMode m
3942
typedef NS_ENUM(NSInteger, FLTExposureMode) {
4043
FLTExposureModeAuto,
4144
FLTExposureModeLocked,
45+
// This should never occur; it indicates an unknown value was received over
46+
// the platform channel.
47+
FLTExposureModeInvalid,
4248
};
4349

4450
/**
@@ -61,6 +67,9 @@ extern FLTExposureMode FLTGetFLTExposureModeForString(NSString *mode);
6167
typedef NS_ENUM(NSInteger, FLTFocusMode) {
6268
FLTFocusModeAuto,
6369
FLTFocusModeLocked,
70+
// This should never occur; it indicates an unknown value was received over
71+
// the platform channel.
72+
FLTFocusModeInvalid,
6473
};
6574

6675
/**
@@ -100,6 +109,9 @@ typedef NS_ENUM(NSInteger, FLTResolutionPreset) {
100109
FLTResolutionPresetVeryHigh,
101110
FLTResolutionPresetUltraHigh,
102111
FLTResolutionPresetMax,
112+
// This should never occur; it indicates an unknown value was received over
113+
// the platform channel.
114+
FLTResolutionPresetInvalid,
103115
};
104116

105117
/**

packages/camera/camera_avfoundation/ios/Classes/CameraProperties.m

Lines changed: 13 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,7 @@ FLTFlashMode FLTGetFLTFlashModeForString(NSString *mode) {
1616
} else if ([mode isEqualToString:@"torch"]) {
1717
return FLTFlashModeTorch;
1818
} else {
19-
NSError *error = [NSError errorWithDomain:NSCocoaErrorDomain
20-
code:NSURLErrorUnknown
21-
userInfo:@{
22-
NSLocalizedDescriptionKey : [NSString
23-
stringWithFormat:@"Unknown flash mode %@", mode]
24-
}];
25-
@throw error;
19+
return FLTFlashModeInvalid;
2620
}
2721
}
2822

@@ -48,14 +42,11 @@ AVCaptureFlashMode FLTGetAVCaptureFlashModeForFLTFlashMode(FLTFlashMode mode) {
4842
return @"auto";
4943
case FLTExposureModeLocked:
5044
return @"locked";
45+
case FLTExposureModeInvalid:
46+
// This value should never actually be used.
47+
return nil;
5148
}
52-
NSError *error = [NSError errorWithDomain:NSCocoaErrorDomain
53-
code:NSURLErrorUnknown
54-
userInfo:@{
55-
NSLocalizedDescriptionKey : [NSString
56-
stringWithFormat:@"Unknown string for exposure mode"]
57-
}];
58-
@throw error;
49+
return nil;
5950
}
6051

6152
FLTExposureMode FLTGetFLTExposureModeForString(NSString *mode) {
@@ -64,13 +55,7 @@ FLTExposureMode FLTGetFLTExposureModeForString(NSString *mode) {
6455
} else if ([mode isEqualToString:@"locked"]) {
6556
return FLTExposureModeLocked;
6657
} else {
67-
NSError *error = [NSError errorWithDomain:NSCocoaErrorDomain
68-
code:NSURLErrorUnknown
69-
userInfo:@{
70-
NSLocalizedDescriptionKey : [NSString
71-
stringWithFormat:@"Unknown exposure mode %@", mode]
72-
}];
73-
@throw error;
58+
return FLTExposureModeInvalid;
7459
}
7560
}
7661

@@ -82,14 +67,11 @@ FLTExposureMode FLTGetFLTExposureModeForString(NSString *mode) {
8267
return @"auto";
8368
case FLTFocusModeLocked:
8469
return @"locked";
70+
case FLTFocusModeInvalid:
71+
// This value should never actually be used.
72+
return nil;
8573
}
86-
NSError *error = [NSError errorWithDomain:NSCocoaErrorDomain
87-
code:NSURLErrorUnknown
88-
userInfo:@{
89-
NSLocalizedDescriptionKey : [NSString
90-
stringWithFormat:@"Unknown string for focus mode"]
91-
}];
92-
@throw error;
74+
return nil;
9375
}
9476

9577
FLTFocusMode FLTGetFLTFocusModeForString(NSString *mode) {
@@ -98,13 +80,7 @@ FLTFocusMode FLTGetFLTFocusModeForString(NSString *mode) {
9880
} else if ([mode isEqualToString:@"locked"]) {
9981
return FLTFocusModeLocked;
10082
} else {
101-
NSError *error = [NSError errorWithDomain:NSCocoaErrorDomain
102-
code:NSURLErrorUnknown
103-
userInfo:@{
104-
NSLocalizedDescriptionKey : [NSString
105-
stringWithFormat:@"Unknown focus mode %@", mode]
106-
}];
107-
@throw error;
83+
return FLTFocusModeInvalid;
10884
}
10985
}
11086

@@ -120,14 +96,7 @@ UIDeviceOrientation FLTGetUIDeviceOrientationForString(NSString *orientation) {
12096
} else if ([orientation isEqualToString:@"portraitUp"]) {
12197
return UIDeviceOrientationPortrait;
12298
} else {
123-
NSError *error = [NSError
124-
errorWithDomain:NSCocoaErrorDomain
125-
code:NSURLErrorUnknown
126-
userInfo:@{
127-
NSLocalizedDescriptionKey :
128-
[NSString stringWithFormat:@"Unknown device orientation %@", orientation]
129-
}];
130-
@throw error;
99+
return UIDeviceOrientationUnknown;
131100
}
132101
}
133102

@@ -163,13 +132,7 @@ FLTResolutionPreset FLTGetFLTResolutionPresetForString(NSString *preset) {
163132
} else if ([preset isEqualToString:@"max"]) {
164133
return FLTResolutionPresetMax;
165134
} else {
166-
NSError *error = [NSError errorWithDomain:NSCocoaErrorDomain
167-
code:NSURLErrorUnknown
168-
userInfo:@{
169-
NSLocalizedDescriptionKey : [NSString
170-
stringWithFormat:@"Unknown resolution preset %@", preset]
171-
}];
172-
@throw error;
135+
return FLTResolutionPresetInvalid;
173136
}
174137
}
175138

0 commit comments

Comments
 (0)