gpt4 book ai didi

android - react-native handle device "share to"特性 iOS/android

转载 作者:行者123 更新时间:2023-11-28 23:34:16 29 4
gpt4 key购买 nike

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

为了更好地了解我在 Android 中做了什么,以及我想要在 iOS 中做什么。为了清楚起见,我将展示一些代码 fragment 。我不确定 iOS 世界中所有术语的名称,这使得搜索知识/示例非常困难。希望这里有人了解这两个平台并且可以将 android 翻译成 iOS 术语吗?

为了使我的应用程序出现在 native Android“共享方式”菜单中,我将这些 Intent 过滤器添加到我的 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>

这允许用户标记一些文本,我的案例网址,并将网址直接共享到我的应用程序。为了将该共享网址传递给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 prop

这允许我在我的 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目录,我需要向哪些文件添加什么?

enter image description here

最佳答案

您可以使用 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 中的某些内容作为 props 传递给 React-Native,你可以在 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];

关于android - react-native handle device "share to"特性 iOS/android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55572181/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com