英文:
iOS Share Extension not working with react-native 0.61.5
问题
我正尝试在Share Extension中使用已注册的(AppRegistry.registerComponent
)React Native模块(使用XCode添加)。在早期版本的React Native中,我们可以在“Build Phases > Link Binaries with Libraries”中手动链接所需的库并使其运行。但是从v0.60版本开始,这不再可行。
这个问题类似于这个问题,但是针对旧版本的React Native,其解决方案不适用于最新版本。
更多详细信息,请参阅GitHub问题。
index.share.js
import { AppRegistry } from 'react-native';
import ShareExtension from './ShareExtension';
AppRegistry.registerComponent('ShareExtension', () => ShareExtension);
ShareVIewController.m > loadView
- (void)loadView {
RCTBridge *bridge = [[RCTBridge alloc] initWithDelegate:self launchOptions:nil];
RCTRootView *rootView = [[RCTRootView alloc] initWithBridge:bridge
moduleName:@"ShareExtension"
initialProperties:nil];
self.view = rootView;
}
这里有一个包含所有这些设置以重现此问题的存储库:GitHub Repo
英文:
I'm trying to use a registered (AppRegistry.registerComponent
) react-native module within a Share Extension (added using XCode). On earlier versions of react-native, we could just link the necessary libraries manually inside Build Phases > Link Binaries with Libraries
and get this up and running. But from v0.60 onwards this is no longer possible.
This issue is similar to this one but on an older version of react-native and its solution doesn't apply to the latest version.
More details in this GitHub Issue
index.share.js
import {AppRegistry} from 'react-native';
import ShareExtension from './ShareExtension';
AppRegistry.registerComponent('ShareExtension', () => ShareExtension);
ShareVIewController.m > loadView
- (void)loadView {
RCTBridge *bridge = [[RCTBridge alloc] initWithDelegate:self launchOptions:nil];
RCTRootView *rootView = [[RCTRootView alloc] initWithBridge:bridge
moduleName:@"ShareExtension"
initialProperties:nil];
self.view = rootView;
}
Here's a repo with all this setup to reproduce this issue: GitHub Repo
答案1
得分: 0
您已经切换到CocoaPods来管理您的依赖项。在检查了您手动链接的ShareExtension包之后,它试图引用您列出的依赖项:
#import <React/RCTBridge.h>
#import <React/RCTRootView.h>
但这些实际上位于您的开发Pods文件夹内,基本上是在不同的范围内。
解决这个问题有许多方法。
我发现解决这个问题最简单的方法是为它创建一个小的存储库,并像任何其他依赖项一样调用它。
您可以为其创建一个Git存储库。我以https://github.com/react-native-community/react-native-push-notification-ios 为模型创建了我的存储库。
您还可以查看以下内容,了解如何调用本地CocoaPods目录的方法:
https://stackoverflow.com/questions/44842837/podfile-path-of-local-pod-relative-to-projectpath-possible
只需创建自己的podspec以引用ShareExtension内的本地文件,可能如下所示:
require "json"
package = JSON.parse(File.read(File.join(File.dirname(__FILE__), "package.json")))
Pod::Spec.new do |s|
# NPM package specification
s.name = 'Share Extension'
s.version = package['version']
s.summary = package['description']
s.description = package['description']
s.license = package['license']
s.author = package['author']
s.homepage = package['homepage']
s.source = { :git => "https://github.com/react-native-community/react-native-share-extension", :tag => "v#{s.version}" }
s.source_files = "ios/*.{h,m}"
s.platform = :ios, "9.0"
s.dependency "React"
end
以上为翻译内容,不包含其他内容。
英文:
You've switched to cocoapods for your dependencies. After inspecting the ShareExtension package you have manually linked, it's trying to reference the dependencies you have listed
#import <React/RCTBridge.h>
#import <React/RCTRootView.h>
But these are actually located inside your development pods folder, basically in a different scope.
> There's a number of ways to resolve this problem.
The easiest thing I've found to do to resolve this would be to make your own little repo for it and call it like any other dependency.
You could make a git repo for it. I model mine after https://github.com/react-native-community/react-native-push-notification-ios
You can also check out the following for how to call a local cocoapods directory:
https://stackoverflow.com/questions/44842837/podfile-path-of-local-pod-relative-to-projectpath-possible
Just make your own podspec to reference the local files inside ShareExtension that might look something like this:
require "json"
package = JSON.parse(File.read(File.join(File.dirname(__FILE__), "package.json")))
Pod::Spec.new do |s|
# NPM package specification
s.name = 'Share Extension'
s.version = package['version']
s.summary = package['description']
s.description = package['description']
s.license = package['license']
s.author = package['author']
s.homepage = package['homepage']
s.source = { :git => "https://github.com/react-native-community/react-native-share-extension", :tag => "v#{s.version}" }
s.source_files = "ios/*.{h,m}"
s.platform = :ios, "9.0"
s.dependency "React"
end
答案2
得分: 0
我找到了解决方案。我们需要为共享扩展添加 Pods 目标(不在主目标内,而是作为独立的目标),并将所有与 React Native 相关的 Pods 复制到此目标。不要忘记添加后安装命令。
platform :ios, '9.0'
require_relative '../node_modules/@react-native-community/cli-platform-ios/native_modules'
target "sample" do
pod 'FBLazyVector', :path => "../node_modules/react-native/Libraries/FBLazyVector"
pod 'FBReactNativeSpec', :path => "../node_modules/react-native/Libraries/FBReactNativeSpec"
# 其他相关的 Pods
end
target "sampleTests" do
inherit! :search_paths
# 用于测试的 Pods
end
target "sample-tvOS" do
target "airbase-tvOSTests" do
inherit! :search_paths
# 用于测试的 Pods
end
end
target "share" do
pod 'FBLazyVector', :path => "../node_modules/react-native/Libraries/FBLazyVector"
pod 'FBReactNativeSpec', :path => "../node_modules/react-native/Libraries/FBReactNativeSpec"
# 其他相关的 Pods
end
post_install do |installer|
installer.pods_project.targets.each do |target|
target.build_configurations.each do |config|
config.build_settings['APPLICATION_EXTENSION_API_ONLY'] = 'NO'
end
end
end
这是您提供的代码的翻译部分。
英文:
I found the solution to this. We have to add pods target for the share extension (not inside the main target but as a separate target) and copy all the react-native related pods to this target. Don't forget to add the post-install commands as well.
platform :ios, '9.0'
require_relative '../node_modules/@react-native-community/cli-platform-ios/native_modules'
target "sample" do
pod 'FBLazyVector', :path => "../node_modules/react-native/Libraries/FBLazyVector"
pod 'FBReactNativeSpec', :path => "../node_modules/react-native/Libraries/FBReactNativeSpec"
pod 'RCTRequired', :path => "../node_modules/react-native/Libraries/RCTRequired"
pod 'RCTTypeSafety', :path => "../node_modules/react-native/Libraries/TypeSafety"
pod 'React', :path => '../node_modules/react-native/'
pod 'React-Core', :path => '../node_modules/react-native/'
pod 'React-CoreModules', :path => '../node_modules/react-native/React/CoreModules'
pod 'React-Core/DevSupport', :path => '../node_modules/react-native/'
pod 'React-RCTActionSheet', :path => '../node_modules/react-native/Libraries/ActionSheetIOS'
pod 'React-RCTAnimation', :path => '../node_modules/react-native/Libraries/NativeAnimation'
pod 'React-RCTBlob', :path => '../node_modules/react-native/Libraries/Blob'
pod 'React-RCTImage', :path => '../node_modules/react-native/Libraries/Image'
pod 'React-RCTLinking', :path => '../node_modules/react-native/Libraries/LinkingIOS'
pod 'React-RCTNetwork', :path => '../node_modules/react-native/Libraries/Network'
pod 'React-RCTSettings', :path => '../node_modules/react-native/Libraries/Settings'
pod 'React-RCTText', :path => '../node_modules/react-native/Libraries/Text'
pod 'React-RCTVibration', :path => '../node_modules/react-native/Libraries/Vibration'
pod 'React-Core/RCTWebSocket', :path => '../node_modules/react-native/'
pod 'React-cxxreact', :path => '../node_modules/react-native/ReactCommon/cxxreact'
pod 'React-jsi', :path => '../node_modules/react-native/ReactCommon/jsi'
pod 'React-jsiexecutor', :path => '../node_modules/react-native/ReactCommon/jsiexecutor'
pod 'React-jsinspector', :path => '../node_modules/react-native/ReactCommon/jsinspector'
pod 'ReactCommon/jscallinvoker', :path => "../node_modules/react-native/ReactCommon"
pod 'ReactCommon/turbomodule/core', :path => "../node_modules/react-native/ReactCommon"
pod 'Yoga', :path => '../node_modules/react-native/ReactCommon/yoga'
pod 'DoubleConversion', :podspec => '../node_modules/react-native/third-party-podspecs/DoubleConversion.podspec'
pod 'glog', :podspec => '../node_modules/react-native/third-party-podspecs/glog.podspec'
pod 'Folly', :podspec => '../node_modules/react-native/third-party-podspecs/Folly.podspec'
target "sampleTests" do
inherit! :search_paths
# Pods for testing
end
use_native_modules!
end
target "sample-tvOS" do
target "airbase-tvOSTests" do
inherit! :search_paths
# Pods for testing
end
end
target "share" do
pod 'FBLazyVector', :path => "../node_modules/react-native/Libraries/FBLazyVector"
pod 'FBReactNativeSpec', :path => "../node_modules/react-native/Libraries/FBReactNativeSpec"
pod 'RCTRequired', :path => "../node_modules/react-native/Libraries/RCTRequired"
pod 'RCTTypeSafety', :path => "../node_modules/react-native/Libraries/TypeSafety"
pod 'React', :path => '../node_modules/react-native/'
pod 'React-Core', :path => '../node_modules/react-native/'
pod 'React-CoreModules', :path => '../node_modules/react-native/React/CoreModules'
pod 'React-Core/DevSupport', :path => '../node_modules/react-native/'
pod 'React-RCTActionSheet', :path => '../node_modules/react-native/Libraries/ActionSheetIOS'
pod 'React-RCTAnimation', :path => '../node_modules/react-native/Libraries/NativeAnimation'
pod 'React-RCTBlob', :path => '../node_modules/react-native/Libraries/Blob'
pod 'React-RCTImage', :path => '../node_modules/react-native/Libraries/Image'
pod 'React-RCTLinking', :path => '../node_modules/react-native/Libraries/LinkingIOS'
pod 'React-RCTNetwork', :path => '../node_modules/react-native/Libraries/Network'
pod 'React-RCTSettings', :path => '../node_modules/react-native/Libraries/Settings'
pod 'React-RCTText', :path => '../node_modules/react-native/Libraries/Text'
pod 'React-RCTVibration', :path => '../node_modules/react-native/Libraries/Vibration'
pod 'React-Core/RCTWebSocket', :path => '../node_modules/react-native/'
pod 'React-cxxreact', :path => '../node_modules/react-native/ReactCommon/cxxreact'
pod 'React-jsi', :path => '../node_modules/react-native/ReactCommon/jsi'
pod 'React-jsiexecutor', :path => '../node_modules/react-native/ReactCommon/jsiexecutor'
pod 'React-jsinspector', :path => '../node_modules/react-native/ReactCommon/jsinspector'
pod 'ReactCommon/jscallinvoker', :path => "../node_modules/react-native/ReactCommon"
pod 'ReactCommon/turbomodule/core', :path => "../node_modules/react-native/ReactCommon"
pod 'Yoga', :path => '../node_modules/react-native/ReactCommon/yoga'
pod 'DoubleConversion', :podspec => '../node_modules/react-native/third-party-podspecs/DoubleConversion.podspec'
pod 'glog', :podspec => '../node_modules/react-native/third-party-podspecs/glog.podspec'
pod 'Folly', :podspec => '../node_modules/react-native/third-party-podspecs/Folly.podspec'
end
post_install do |installer|
installer.pods_project.targets.each do |target|
target.build_configurations.each do |config|
config.build_settings['APPLICATION_EXTENSION_API_ONLY'] = 'NO'
end
end
end
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论