【问题标题】:react-native handle device "share to" feature iOS / androidreact-native 手柄设备“分享到”功能 iOS / android
【发布时间】:2019-04-08 11:14:54
【问题描述】:

我已经在我的 react-native 项目的 android 部分成功实现了接收共享意图处理。现在我想对 iOS 部分做同样的事情。

为了更好地了解我在 android 中做了什么,以及我在 iOS 中想要什么。为了清楚起见,我将展示一些代码 sn-ps。我不确定 iOS 世界中的所有术语都叫什么,因此很难搜索知识/示例。希望 SO 上的某个人知道这两个平台并且可以将 android 翻译成 iOS 术语?

为了让我的应用出现在原生 android 的“分享方式”菜单中,我将这些意图过滤器添加到了我的 AndroidManifest.xml

  <intent-filter>
      <action android:name="android.intent.action.MAIN" />
      <category android:name="android.intent.category.LAUNCHER" />
  </intent-filter>
  <intent-filter>
    <action android:name="android.intent.action.SEND" />
    <category android:name="android.intent.category.DEFAULT" />
    <data android:mimeType="text/plain" />
  </intent-filter>

这允许用户标记一些文本,我的案例 url,并直接将 url 共享到我的应用程序。为了将该共享 url 传递给 react-native 部分,我在 MainActivity.kt 中添加了以下内容(也可以是 java)

class MainActivity : ReactActivity() {
    override fun createReactActivityDelegate(): ReactActivityDelegate {
        return object : ReactActivityDelegate(this, mainComponentName) {
            override fun getLaunchOptions(): Bundle? {
                return gerenateBundleFromIntent(intent)
            }
        }
    }


    private fun gerenateBundleFromIntent(intent: Intent) : Bundle {
        val bundle = Bundle()
        when {
            intent?.action == Intent.ACTION_SEND -> {
                if ("text/plain" == intent.type) {
                    intent.getStringExtra(Intent.EXTRA_TEXT)?.let {
                        bundle.putString("shareUrl", it)
                    }
                }
            } else -> {
            // Handle other intents, such as being started from the home screen
            }
        }
        return bundle
    }
}

putString("shareUrl", it) 将成为一个名为 shareUrl 的 react-native 属性

这允许我在我的 react-native 代码 (app.js) 中接收数据,如下所示:

export default class App extends Component<Props> {
  constructor(props) {
    super(props)
    this.state = {
      uri: undefined,
      shareUrl: this.props.shareUrl
    }
  }
...
}

如果可能的话,我希望能够以类似的方式在 iOS 设备上共享一个 url,给这个应用程序,并将该 url 传递给我的 react-natve App.js 的 shareUrl 属性

从哪里开始?这些是 react-native cli 生成的 iOS 目录,我需要在哪些文件中添加什么?

【问题讨论】:

    标签: android ios react-native


    【解决方案1】:

    您可以使用 UIActivityViewController 将您的 iOS 应用程序中的某些内容共享给其他应用程序,

    func shareSomething() {
      let text = "abc"
      let image = UIImage(named: "ABC")
      let url = NSURL(string:"https://www.google.com")
      let share = [text , image! , url]
      let activityViewController = UIActivityViewController(activityItems: shareAll, applicationActivities: nil)
      activityViewController.popoverPresentationController?.sourceView = self.view 
      self.present(activityViewController, animated: true, completion: nil)
    }
    

    objective-c 代码是,

    - (void)shareSomething
    {
        NSURL *url = [NSURL fileURLWithPath:imagePath];
        NSArray *objectsToShare = @[url];
        UIActivityViewController *controller = [[UIActivityViewController alloc] initWithActivityItems:objectsToShare applicationActivities:nil];
        [controller setCompletionHandler:^(NSString *activityType, BOOL completed)
        {
    
        }]
        [self presentViewController:controller animated:YES completion:nil];
    }
    

    如果你想将一些东西从 iOS 传递给 react-native 作为 props,你可以在 AppDelegate.m 文件中进行,在

    didFinishLaunchingWithOptions

    功能。像这样,

    NSArray *imageList = @[@"http://foo.com/bar1.png",
                       @"http://foo.com/bar2.png"];
    
    NSDictionary *props = @{@"images" : imageList};
    
    RCTRootView *rootView = [[RCTRootView alloc] initWithBridge:bridge
                                                 moduleName:@"YOUR_APP_NAME"
                                          initialProperties:props];
    

    【讨论】:

    • 嘿,谢谢。你的底部代码 sn-p 看起来像我需要的,以便将数据从 iOS 传递到 react-native。太棒了,到目前为止一切都很好。但我对分享我的 iOS 应用程序中的任何内容不感兴趣,我想要相反,接收“分享”。即您在您的 Safari 浏览器上的网站上,当按下共享图标,打开本机共享菜单时,我希望我的应用程序出现在该菜单中并在我的应用程序中接收 URL。你知道我如何将我的应用注册为接收者并收集共享数据吗?
    • @RasmusPuls 看看这个教程yudiz.com/share-extention-in-ios-app
    • 再次感谢,share extension 是术语......他的 SEO 一定很糟糕,因为他拼错了博客文章标题:^)
    猜你喜欢
    • 2011-08-08
    • 1970-01-01
    • 2018-03-25
    • 1970-01-01
    • 2019-01-24
    • 2019-04-19
    • 2020-07-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多