我已经在我的react-native项目的android部分中成功实现了接收共享意图处理。现在,我想对iOS部分做同样的事情。
可以更好地了解我在android中所做的事情,以及我想要在iOS中所做的事情。为了清楚起见,我将显示一些代码片段。我不确定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传递给我在 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
}
}
putString("shareUrl", it)
将成为名为shareUrl
这使我可以像这样在本机代码( app.js )中接收数据:
export default class App extends Component<Props> {
constructor(props) {
super(props)
this.state = {
uri: undefined,
shareUrl: this.props.shareUrl
}
}
...
}
如果可能的话,我希望以类似的方式在iOS设备上共享此应用程序的URL,并将该URL传递给我的nakve App.js的shareUrl
道具
从哪里开始?这些是react-native cli生成的iOS目录,我需要添加哪些文件?
答案 0 :(得分:1)
You can use UIActivityViewController to share something from your iOS app to other apps,
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,
- (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
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];