将文本作为字符串共享给另一个应用程序 [英] Sharing text as a string to another app
问题描述
我有一个基本上像这样的字符串:
I have a string that basically looks like this:
得分:
播放器一:得分
选手二得分
玩家三:得分
Player One: Score
Player Two Score
Player Three: Score
我想以文本形式与诸如WhatsApp,Facebook,iMessage等应用程序共享.最佳方法是什么?我曾尝试以.txt文件的形式进行共享,但是它以文件形式而不是WhatsApp中的常规消息进行共享.
I want to share this as text to apps such as WhatsApp, Facebook, iMessage, etc. What is the best way to do this? I have tried sharing as a .txt file, but it shares as a file instead of a regular message in WhatsApp.
推荐答案
您可以使用自定义URL方案.像Facebook和WhatsApp这样的应用程序通常都有自己的方案,可用于将数据发送到这些应用程序中.在此处查看WhatsApp的信息:链接
You could use a custom URL scheme. Apps like Facebook and WhatsApp generally have their own schemes that you can use to send data into those apps. See WhatsApp's info here: Link
或者,您可以使用UIActivityViewController
.这也支持其他数据类型,而不仅仅是字符串(请参见此SO问题).
Alternatively, you could use a UIActivityViewController
. This also supports other data types, not just strings (see this SO question).
NSString *textToShare = @"your text";
UIActivityViewController *activityVC = [[UIActivityViewController alloc] initWithActivityItems:@[textToShare] applicationActivities:nil];
activityVC.excludedActivityTypes = @[UIActivityTypePrint, UIActivityTypeCopyToPasteboard, UIActivityTypeAssignToContact, UIActivityTypeSaveToCameraRoll]; //Exclude whichever aren't relevant
[self presentViewController:activityVC animated:YES completion:nil];
关于此方法,这里有一篇不错的博客文章:链接
Here's a nice blog post on this method: Link
这篇关于将文本作为字符串共享给另一个应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!