@neoskola/auto-play 0.2.10 → 0.2.12

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -22,8 +22,8 @@ Pod::Spec.new do |s|
22
22
  "cpp/**/*.{hpp,cpp}",
23
23
  ]
24
24
 
25
- # react helpers like RCTConvert
26
- s.public_header_files = Array(s.attributes_hash['public_header_files']) + ["ios/ReactHelpers/*.h"]
25
+ # react helpers like RCTConvert + ObjC utils (ObjCExceptionCatcher etc.)
26
+ s.public_header_files = Array(s.attributes_hash['public_header_files']) + ["ios/ReactHelpers/*.h", "ios/utils/*.h"]
27
27
 
28
28
  s.resource_bundles = {
29
29
  "ReactNativeAutoPlay" => ['ios/Assets/**/*.ttf']
@@ -51,14 +51,17 @@ class AutoPlayInterfaceController: NSObject, CPInterfaceControllerDelegate {
51
51
  _ templateToPush: CPTemplate,
52
52
  animated: Bool
53
53
  ) async throws -> Bool {
54
- // CPNowPlayingTemplate.shared must NEVER be pushed via pushTemplate().
55
- // Apple's CarPlay framework throws an uncatchable NSException (Objective-C)
56
- // when attempting to push this singleton. The Now Playing UI appears
57
- // automatically when MPNowPlayingInfoCenter is configured and playbackState
58
- // is set to .playing.
54
+ // CPNowPlayingTemplate.shared can throw an ObjC NSException when pushed.
55
+ // Swift try/catch cannot catch NSExceptions, so we use an ObjC wrapper.
59
56
  if templateToPush is CPNowPlayingTemplate {
60
- print("[AutoPlay] CPNowPlayingTemplate cannot be pushed skipping. Configure MPNowPlayingInfoCenter instead.")
61
- return true
57
+ // Skip if already in the stack
58
+ let alreadyInStack = interfaceController.templates.contains(where: { $0 is CPNowPlayingTemplate })
59
+ if alreadyInStack {
60
+ print("[AutoPlay] CPNowPlayingTemplate already in stack, skipping push")
61
+ return true
62
+ }
63
+
64
+ return await pushNowPlayingTemplateSafely(animated: animated)
62
65
  }
63
66
 
64
67
  return try await interfaceController.pushTemplate(
@@ -67,6 +70,29 @@ class AutoPlayInterfaceController: NSObject, CPInterfaceControllerDelegate {
67
70
  )
68
71
  }
69
72
 
73
+ /// Pushes CPNowPlayingTemplate using ObjC @try/@catch to prevent crash from NSException.
74
+ private func pushNowPlayingTemplateSafely(animated: Bool) async -> Bool {
75
+ return await withCheckedContinuation { continuation in
76
+ var pushError: NSError?
77
+ let success = ObjCExceptionCatcher.tryBlock({
78
+ self.interfaceController.pushTemplate(
79
+ CPNowPlayingTemplate.shared,
80
+ animated: animated
81
+ ) { success, error in
82
+ if let error = error {
83
+ print("[AutoPlay] CPNowPlayingTemplate push completion error: \(error)")
84
+ }
85
+ continuation.resume(returning: success)
86
+ }
87
+ }, error: &pushError)
88
+
89
+ if !success {
90
+ print("[AutoPlay] CPNowPlayingTemplate push threw ObjC exception: \(pushError?.localizedDescription ?? "unknown")")
91
+ continuation.resume(returning: false)
92
+ }
93
+ }
94
+ }
95
+
70
96
  func setRootTemplate(
71
97
  _ rootTemplate: CPTemplate,
72
98
  animated: Bool
@@ -0,0 +1,12 @@
1
+ #import <Foundation/Foundation.h>
2
+
3
+ /// Provides Objective-C @try/@catch exception handling for Swift code.
4
+ /// Swift's try/catch cannot catch Objective-C NSExceptions, which causes
5
+ /// crashes when Apple frameworks throw them (e.g., CPInterfaceController.pushTemplate
6
+ /// with CPNowPlayingTemplate). This wrapper makes those exceptions catchable.
7
+ @interface ObjCExceptionCatcher : NSObject
8
+
9
+ + (BOOL)tryBlock:(void(NS_NOESCAPE ^_Nonnull)(void))tryBlock
10
+ error:(NSError * _Nullable * _Nullable)error;
11
+
12
+ @end
@@ -0,0 +1,23 @@
1
+ #import "ObjCExceptionCatcher.h"
2
+
3
+ @implementation ObjCExceptionCatcher
4
+
5
+ + (BOOL)tryBlock:(void(NS_NOESCAPE ^)(void))tryBlock error:(NSError **)error {
6
+ @try {
7
+ tryBlock();
8
+ return YES;
9
+ } @catch (NSException *exception) {
10
+ NSLog(@"[ObjCExceptionCatcher] Caught exception: %@ — %@", exception.name, exception.reason);
11
+ if (error) {
12
+ *error = [NSError errorWithDomain:@"ObjCExceptionCatcher"
13
+ code:0
14
+ userInfo:@{
15
+ NSLocalizedDescriptionKey: exception.reason ?: @"Unknown Objective-C exception",
16
+ @"ExceptionName": exception.name ?: @"Unknown"
17
+ }];
18
+ }
19
+ return NO;
20
+ }
21
+ }
22
+
23
+ @end
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@neoskola/auto-play",
3
- "version": "0.2.10",
3
+ "version": "0.2.12",
4
4
  "description": "Android Auto and Apple CarPlay for react-native",
5
5
  "main": "lib/index",
6
6
  "module": "lib/index",