skip to Main Content

I am trying to use Swift class in objective-c code. I already went through this

and I can’t convert my .mm file to .m file as I am using react native’s new architecture which requires .mm file

I am trying this out for react native’s new architecture which requires .mm file. Check here to learn more

My header file RTNMyCamera.h

#import <React/RCTViewComponentView.h>
#import <UIKit/UIKit.h>
NS_ASSUME_NONNULL_BEGIN

@interface RTNMyCamera : RCTViewComponentView

@end

NS_ASSUME_NONNULL_END

My objective-c++ file RTNMyCamera.mm

#import "RTNMyCamera.h"

#import <react/renderer/components/RTNMyCameraSpecs/ComponentDescriptors.h>
#import <react/renderer/components/RTNMyCameraSpecs/EventEmitters.h>
#import <react/renderer/components/RTNMyCameraSpecs/Props.h>
#import <react/renderer/components/RTNMyCameraSpecs/RCTComponentViewHelpers.h>

#import "myapp-Swift.h"
#import "RCTFabric/React-RCTFabric-umbrella.h"

using namespace facebook::react;

@interface RTNMyCamera () <RCTRTNMyCameraViewProtocol>
@end

@implementation RTNMyCamera {
  UIView *_view;
  MyCamera *myCamera;
}

+ (ComponentDescriptorProvider)componentDescriptorProvider
{
  return concreteComponentDescriptorProvider<RTNMyCameraComponentDescriptor>();
}

- (instancetype)initWithFrame:(CGRect)frame
{
  if (self = [super initWithFrame:frame]) {
    static const auto defaultProps = std::make_shared<const RTNMyCameraProps>();
    _props = defaultProps;

    _view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height)];
    myCamera = [[MyCamera alloc] initWithFrame:_view.bounds];
    [_view addSubview: myCamera];
    
    self.contentView = _view;
  }

  return self;
}

@end

Class<RCTComponentViewProtocol> RTNMyCameraCls(void)
{
  return RTNMyCamera.class;
}

My swift file MyCamera

@objcMembers
class MyCamera: UIView,AVCapturePhotoCaptureDelegate,AVCaptureVideoDataOutputSampleBufferDelegate {
 //all of my business logic to capture photo
}

Entire swift code here

I get getting error saying No type or protocol named 'AVCapturePhotoCaptureDelegate'

Here are few things I tried which did not work

  1. I cannot convert .mm to .m
  2. I tried creating PrefixHeader.pch and added #import <AVFoundation/AVFoundation.h> as well as @protocol AVCapturePhotoCaptureDelegate
  3. In RTNMyCamera.h I added #import <AVFoundation/AVFoundation.h>, <AVCapturePhotoCaptureDelegate,AVCaptureVideoDataOutputSampleBufferDelegate> and @class MyCamera;

Here is sample repo

2

Answers


  1. You need to import AVFoundation into your Swift file that uses AVCapturePhotoCaptureDelegate, not your ObjC++. In MyCamera.swift you want import AVFoundation.


    Looking at your repo, you created a prefix header, but didn’t configure the project to use it.

    // You will also need to set the Prefix Header build setting of one or more of your targets to reference this file.
    

    The build setting you want is in the general pane:

    Apple Clang - Language section, Prefix Header

    It’s called GCC_PREFIX_HEADER.

    Login or Signup to reply.
  2. I think you need to make sure the class implementing AVCapturePhotoCaptureDelegate extends NSObject.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search