Search code examples
iosswiftreact-nativescenekitarkit

Extern ARSCNView to react native


I have a custom View using ARSCNView, let's say it's something like

import Foundation
import UIKit
import ARKit
import SceneKit

@available(iOS 11.0, *)
class ARSceneView: ARSCNView, ARSessionDelegate, ARSCNViewDelegate {

}

I need to use it in React Native, so I created swift view manager:

import UIKit

@objc(ARSceneViewManager)
class ARSceneViewManager : RCTViewManager {
  override func view() -> UIView! {
    if #available(iOS 11.0, *) {
      return ARSceneView(frame: .zero)
    } else {
      return UIView()
    };
 }

  override static func requiresMainQueueSetup() -> Bool {
      return true
    }
}

and ObjC file:

#import "React/RCTBridgeModule.h"
#import "React/RCTViewManager.h"

@interface RCT_EXTERN_MODULE(ARSceneViewManager, RCTViewManager)

@end

I used it in my App.js file:

const ARSceneView = requireNativeComponent('ARSceneView', ARSceneView);

But it does not seem to appear to screen - I can see only black screen. What am I doing wrong? If I use e.g. UILabel instead, everything works very well.

This is Xcode output:

2021-08-11 11:42:51.521528+0200 ARScreen[22000:1674182] [connection] nw_endpoint_handler_set_adaptive_read_handler [C1 172.20.10.2:8081 ready socket-flow (satisfied (Path is satisfied), viable, interface: bridge100, ipv4, dns)] unregister notification for read_timeout failed
2021-08-11 11:42:51.521615+0200 ARScreen[22000:1674182] [connection] nw_endpoint_handler_set_adaptive_write_handler [C1 172.20.10.2:8081 ready socket-flow (satisfied (Path is satisfied), viable, interface: bridge100, ipv4, dns)] unregister notification for write_timeout failed
2021-08-11 11:42:51.586421+0200 ARScreen[22000:1674025] [native] Running application ARScreen ({
    initialProps =     {
    };
    rootTag = 1;
})
2021-08-11 11:42:51.598740+0200 ARScreen[22000:1674182] [connection] nw_endpoint_handler_set_adaptive_read_handler [C2 172.20.10.2:8081 ready socket-flow (satisfied (Path is satisfied), viable, interface: bridge100, ipv4, dns)] unregister notification for read_timeout failed
2021-08-11 11:42:51.598819+0200 ARScreen[22000:1674182] [connection] nw_endpoint_handler_set_adaptive_write_handler [C2 172.20.10.2:8081 ready socket-flow (satisfied (Path is satisfied), viable, interface: bridge100, ipv4, dns)] unregister notification for write_timeout failed
2021-08-11 11:42:51.661793+0200 ARScreen[22000:1674183] [connection] nw_endpoint_handler_set_adaptive_read_handler [C3 172.20.10.2:8081 ready socket-flow (satisfied (Path is satisfied), viable, interface: bridge100, ipv4, dns)] unregister notification for read_timeout failed
2021-08-11 11:42:51.668304+0200 ARScreen[22000:1674183] [connection] nw_endpoint_handler_set_adaptive_write_handler [C3 172.20.10.2:8081 ready socket-flow (satisfied (Path is satisfied), viable, interface: bridge100, ipv4, dns)] unregister notification for write_timeout failed
2021-08-11 11:42:51.670879+0200 ARScreen[22000:1674183] [connection] nw_endpoint_handler_set_adaptive_read_handler [C4 172.20.10.2:8081 ready socket-flow (satisfied (Path is satisfied), viable, interface: bridge100, ipv4, dns)] unregister notification for read_timeout failed
2021-08-11 11:42:51.671031+0200 ARScreen[22000:1674183] [connection] nw_endpoint_handler_set_adaptive_write_handler [C4 172.20.10.2:8081 ready socket-flow (satisfied (Path is satisfied), viable, interface: bridge100, ipv4, dns)] unregister notification for write_timeout failed
2021-08-11 11:42:52.878308+0200 ARScreen[22000:1674193] [javascript] Running "ARScreen" with {"rootTag":1,"initialProps":{}}
2021-08-11 11:42:52.918248+0200 ARScreen[22000:1674025] Metal GPU Frame Capture Enabled
2021-08-11 11:42:52.918608+0200 ARScreen[22000:1674025] Metal API Validation Enabled
2021-08-11 11:42:53.123875+0200 ARScreen[22000:1674180] [connection] nw_endpoint_handler_set_adaptive_read_handler [C6 172.20.10.2:8081 ready socket-flow (satisfied (Path is satisfied), viable, interface: bridge100, ipv4, dns)] unregister notification for read_timeout failed
2021-08-11 11:42:53.123972+0200 ARScreen[22000:1674180] [connection] nw_endpoint_handler_set_adaptive_write_handler [C6 172.20.10.2:8081 ready socket-flow (satisfied (Path is satisfied), viable, interface: bridge100, ipv4, dns)] unregister notification for write_timeout failed
2021-08-11 11:42:54.571761+0200 ARScreen[22000:1674197] [connection] nw_socket_handle_socket_event [C5:1] Socket SO_ERROR [61: Connection refused]
2021-08-11 11:42:54.573292+0200 ARScreen[22000:1674180] [connection] nw_connection_get_connected_socket [C5] Client called nw_connection_get_connected_socket on unconnected nw_connection
2021-08-11 11:42:54.573526+0200 ARScreen[22000:1674180] TCP Conn 0x2805548f0 Failed : error 0:61 [61]

Solution

  • In info.plist file check if you have RequiredDeviceCapabilities tags:

    <key> UIRequiredDeviceCapabilities </key>
        <array>
            <string> armv7 </string>
            <string> arkit </string>
        </array>
    

    and Privacy–CameraUsageDescription option:

    <key> NSCameraUsageDescription </key>
        <string> This app needs your camera </string>
    

    Also I hope view's NSLayoutConstraints are OK.

    And make sure you didn't forget to feed a scene slot:

    sceneView.scene = SCNScene()