iOS共享扩展在react-native 0.61.5中不起作用。

huangapple go评论130阅读模式
英文:

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

Screenshot of the error

答案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 &lt;React/RCTBridge.h&gt;
#import &lt;React/RCTRootView.h&gt;

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 &quot;json&quot;

package = JSON.parse(File.read(File.join(File.dirname(__FILE__), &quot;package.json&quot;)))

Pod::Spec.new do |s|
  # NPM package specification
  
  s.name           = &#39;Share Extension&#39;
  s.version        = package[&#39;version&#39;]
  s.summary        = package[&#39;description&#39;]
  s.description    = package[&#39;description&#39;]
  s.license        = package[&#39;license&#39;]
  s.author         = package[&#39;author&#39;]
  s.homepage       = package[&#39;homepage&#39;]
  
  s.source       = { :git =&gt; &quot;https://github.com/react-native-community/react-native-share-extension&quot;, :tag =&gt; &quot;v#{s.version}&quot; }
  s.source_files = &quot;ios/*.{h,m}&quot;

  s.platform     = :ios, &quot;9.0&quot;

  s.dependency &quot;React&quot;

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, &#39;9.0&#39;
require_relative &#39;../node_modules/@react-native-community/cli-platform-ios/native_modules&#39;

target &quot;sample&quot; do
  pod &#39;FBLazyVector&#39;, :path =&gt; &quot;../node_modules/react-native/Libraries/FBLazyVector&quot;
  pod &#39;FBReactNativeSpec&#39;, :path =&gt; &quot;../node_modules/react-native/Libraries/FBReactNativeSpec&quot;
  pod &#39;RCTRequired&#39;, :path =&gt; &quot;../node_modules/react-native/Libraries/RCTRequired&quot;
  pod &#39;RCTTypeSafety&#39;, :path =&gt; &quot;../node_modules/react-native/Libraries/TypeSafety&quot;
  pod &#39;React&#39;, :path =&gt; &#39;../node_modules/react-native/&#39;
  pod &#39;React-Core&#39;, :path =&gt; &#39;../node_modules/react-native/&#39;
  pod &#39;React-CoreModules&#39;, :path =&gt; &#39;../node_modules/react-native/React/CoreModules&#39;
  pod &#39;React-Core/DevSupport&#39;, :path =&gt; &#39;../node_modules/react-native/&#39;
  pod &#39;React-RCTActionSheet&#39;, :path =&gt; &#39;../node_modules/react-native/Libraries/ActionSheetIOS&#39;
  pod &#39;React-RCTAnimation&#39;, :path =&gt; &#39;../node_modules/react-native/Libraries/NativeAnimation&#39;
  pod &#39;React-RCTBlob&#39;, :path =&gt; &#39;../node_modules/react-native/Libraries/Blob&#39;
  pod &#39;React-RCTImage&#39;, :path =&gt; &#39;../node_modules/react-native/Libraries/Image&#39;
  pod &#39;React-RCTLinking&#39;, :path =&gt; &#39;../node_modules/react-native/Libraries/LinkingIOS&#39;
  pod &#39;React-RCTNetwork&#39;, :path =&gt; &#39;../node_modules/react-native/Libraries/Network&#39;
  pod &#39;React-RCTSettings&#39;, :path =&gt; &#39;../node_modules/react-native/Libraries/Settings&#39;
  pod &#39;React-RCTText&#39;, :path =&gt; &#39;../node_modules/react-native/Libraries/Text&#39;
  pod &#39;React-RCTVibration&#39;, :path =&gt; &#39;../node_modules/react-native/Libraries/Vibration&#39;
  pod &#39;React-Core/RCTWebSocket&#39;, :path =&gt; &#39;../node_modules/react-native/&#39;

  pod &#39;React-cxxreact&#39;, :path =&gt; &#39;../node_modules/react-native/ReactCommon/cxxreact&#39;
  pod &#39;React-jsi&#39;, :path =&gt; &#39;../node_modules/react-native/ReactCommon/jsi&#39;
  pod &#39;React-jsiexecutor&#39;, :path =&gt; &#39;../node_modules/react-native/ReactCommon/jsiexecutor&#39;
  pod &#39;React-jsinspector&#39;, :path =&gt; &#39;../node_modules/react-native/ReactCommon/jsinspector&#39;
  pod &#39;ReactCommon/jscallinvoker&#39;, :path =&gt; &quot;../node_modules/react-native/ReactCommon&quot;
  pod &#39;ReactCommon/turbomodule/core&#39;, :path =&gt; &quot;../node_modules/react-native/ReactCommon&quot;
  pod &#39;Yoga&#39;, :path =&gt; &#39;../node_modules/react-native/ReactCommon/yoga&#39;

  pod &#39;DoubleConversion&#39;, :podspec =&gt; &#39;../node_modules/react-native/third-party-podspecs/DoubleConversion.podspec&#39;
  pod &#39;glog&#39;, :podspec =&gt; &#39;../node_modules/react-native/third-party-podspecs/glog.podspec&#39;
  pod &#39;Folly&#39;, :podspec =&gt; &#39;../node_modules/react-native/third-party-podspecs/Folly.podspec&#39;

  target &quot;sampleTests&quot; do
    inherit! :search_paths
    # Pods for testing
  end

  use_native_modules!
end

target &quot;sample-tvOS&quot; do
  target &quot;airbase-tvOSTests&quot; do
    inherit! :search_paths
    # Pods for testing
  end
end

target &quot;share&quot; do
  pod &#39;FBLazyVector&#39;, :path =&gt; &quot;../node_modules/react-native/Libraries/FBLazyVector&quot;
  pod &#39;FBReactNativeSpec&#39;, :path =&gt; &quot;../node_modules/react-native/Libraries/FBReactNativeSpec&quot;
  pod &#39;RCTRequired&#39;, :path =&gt; &quot;../node_modules/react-native/Libraries/RCTRequired&quot;
  pod &#39;RCTTypeSafety&#39;, :path =&gt; &quot;../node_modules/react-native/Libraries/TypeSafety&quot;
  pod &#39;React&#39;, :path =&gt; &#39;../node_modules/react-native/&#39;
  pod &#39;React-Core&#39;, :path =&gt; &#39;../node_modules/react-native/&#39;
  pod &#39;React-CoreModules&#39;, :path =&gt; &#39;../node_modules/react-native/React/CoreModules&#39;
  pod &#39;React-Core/DevSupport&#39;, :path =&gt; &#39;../node_modules/react-native/&#39;
  pod &#39;React-RCTActionSheet&#39;, :path =&gt; &#39;../node_modules/react-native/Libraries/ActionSheetIOS&#39;
  pod &#39;React-RCTAnimation&#39;, :path =&gt; &#39;../node_modules/react-native/Libraries/NativeAnimation&#39;
  pod &#39;React-RCTBlob&#39;, :path =&gt; &#39;../node_modules/react-native/Libraries/Blob&#39;
  pod &#39;React-RCTImage&#39;, :path =&gt; &#39;../node_modules/react-native/Libraries/Image&#39;
  pod &#39;React-RCTLinking&#39;, :path =&gt; &#39;../node_modules/react-native/Libraries/LinkingIOS&#39;
  pod &#39;React-RCTNetwork&#39;, :path =&gt; &#39;../node_modules/react-native/Libraries/Network&#39;
  pod &#39;React-RCTSettings&#39;, :path =&gt; &#39;../node_modules/react-native/Libraries/Settings&#39;
  pod &#39;React-RCTText&#39;, :path =&gt; &#39;../node_modules/react-native/Libraries/Text&#39;
  pod &#39;React-RCTVibration&#39;, :path =&gt; &#39;../node_modules/react-native/Libraries/Vibration&#39;
  pod &#39;React-Core/RCTWebSocket&#39;, :path =&gt; &#39;../node_modules/react-native/&#39;

  pod &#39;React-cxxreact&#39;, :path =&gt; &#39;../node_modules/react-native/ReactCommon/cxxreact&#39;
  pod &#39;React-jsi&#39;, :path =&gt; &#39;../node_modules/react-native/ReactCommon/jsi&#39;
  pod &#39;React-jsiexecutor&#39;, :path =&gt; &#39;../node_modules/react-native/ReactCommon/jsiexecutor&#39;
  pod &#39;React-jsinspector&#39;, :path =&gt; &#39;../node_modules/react-native/ReactCommon/jsinspector&#39;
  pod &#39;ReactCommon/jscallinvoker&#39;, :path =&gt; &quot;../node_modules/react-native/ReactCommon&quot;
  pod &#39;ReactCommon/turbomodule/core&#39;, :path =&gt; &quot;../node_modules/react-native/ReactCommon&quot;
  pod &#39;Yoga&#39;, :path =&gt; &#39;../node_modules/react-native/ReactCommon/yoga&#39;

  pod &#39;DoubleConversion&#39;, :podspec =&gt; &#39;../node_modules/react-native/third-party-podspecs/DoubleConversion.podspec&#39;
  pod &#39;glog&#39;, :podspec =&gt; &#39;../node_modules/react-native/third-party-podspecs/glog.podspec&#39;
  pod &#39;Folly&#39;, :podspec =&gt; &#39;../node_modules/react-native/third-party-podspecs/Folly.podspec&#39;
end

post_install do |installer|
  installer.pods_project.targets.each do |target|
    target.build_configurations.each do |config|
      config.build_settings[&#39;APPLICATION_EXTENSION_API_ONLY&#39;] = &#39;NO&#39;
    end
  end
end

huangapple
  • 本文由 发表于 2020年1月7日 00:56:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/59616054.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定