简体   繁体   English

react-native处理设备“共享”功能iOS / android

[英]react-native handle device “share to” feature iOS / android

I have successfully implemented receive share intent handling in my android part of my react-native project. 我已经在我的react-native项目的android部分中成功实现了接收共享意图处理。 Now I want to do the same for the iOS part. 现在,我想对iOS部分执行相同的操作。

To provide a better idea of what I did in android, and so what I want in iOS. 为了更好地了解我在android中所做的事情,以及我想要在iOS中所做的事情。 I will show some code snippets for clarity. 为了清楚起见,我将显示一些代码片段。 I'm not sure what all the terms are called in the iOS world, making it very difficult to search knowledge/examples. 我不确定iOS世界中的所有术语都叫什么,这使得搜索知识/示例非常困难。 Hopefully someone here on SO knows both platforms and can translate android to iOS terminology? 希望SO上的某人了解这两个平台,并且可以将android转换为iOS术语?

To make my app appear in the native android "share via" menu, I added these intent-filters to my AndroidManifest.xml 为了使我的应用程序出现在本机android的“ share via”菜单中,我将这些intent-filters添加到了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>

this allows the user to mark some text, I my case urls, and share the url directly to my app. 这允许用户标记一些文本(我的案例网址),并将该网址直接共享给我的应用。 To pass that shared url to the react-native part I added following to my MainActivity.kt (could have been java as well) 为了将共享的URL传递给我在MainActivity.kt之后添加的react-native部分(也可以是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
    }
}

the putString("shareUrl", it) will become a react-native prop called shareUrl putString("shareUrl", it)将成为一个名为shareUrl的反应本地道具

This allows me to receive data in my react-native code ( app.js ) like this: 这样,我就可以像下面这样在我的本机代码( app.js )中接收数据:

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

If possible, I would like in a similar manner to be able on iOS device to share an url, to this app and pass that url to the shareUrl prop of my react-natve App.js 如果可能的话,我想以类似的方式,能够在iOS设备上共享一个网址,这个应用程序,并且该URL传递给shareUrl我的反应,natve App.js的道具

Where to start? 从哪儿开始? These are the iOS dirs generated by the react-native cli, which files do I need to add what to? 这些是react-native cli生成的iOS目录,我需要添加哪些文件?

在此处输入图片说明

You can use UIActivityViewController to share something from your iOS app to other apps, 您可以使用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 code is, 目标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];
}

and if you want to pass something from the iOS to react-native as props, you can do that in AppDelegate.m file, inside 如果您想将某些东西从iOS传递给react-native作为道具,则可以在AppDelegate.m文件中

didFinishLaunchingWithOptions didFinishLaunchingWithOptions

function. 功能。 Like this, 像这样,

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];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM