ios UIActivityViewController - 电子邮件和 Twitter 共享
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12984403/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
UIActivityViewController - Email and Twitter sharing
提问by anthoprotic
I recently started working with UIActivity to share my app to the world, but I have few problems. First, I didn't find how to set the subject of my email.Is there any way? Second, when I set the body text of my email, there is a extra "enter" (first line of the email is blank and my text starts at the second line). Here's the code:
我最近开始使用 UIActivity 向全世界分享我的应用程序,但我遇到的问题很少。首先,我没有找到如何设置电子邮件主题。有什么办法吗?其次,当我设置电子邮件的正文时,有一个额外的“输入”(电子邮件的第一行是空白的,我的文本从第二行开始)。这是代码:
NSMutableArray *array = [[NSMutableArray alloc] initWithObjects: @"Test", nil];
UIActivityViewController *activityViewController = [[UIActivityViewController alloc]
initWithActivityItems:array applicationActivities:nil];
And in the email, it shows that:
在电子邮件中,它显示:
"
”
Test "
测试 ”
Third: is there a way to know which sharing method has been selected? Because I want to include a hashtag in my post when the user shares on twitter, but now it gets integrated in the email also, which obviously doesn't make sense.
第三:有没有办法知道选择了哪种分享方式?因为当用户在 twitter 上分享时,我想在我的帖子中包含一个主题标签,但现在它也被集成到电子邮件中,这显然没有意义。
Thanks!
谢谢!
回答by Ajay
For adding subjectto the email using UIActivityViewController on iOS6, this is the best solution that anyone can use.. All you have to do is call the following while initializing UIActivityViewController.
对于在 iOS6 上使用 UIActivityViewController添加主题的电子邮件,这是任何人都可以使用的最佳解决方案。您所要做的就是在初始化 UIActivityViewController 时调用以下内容。
UIActivityViewController *activityViewController = [[UIActivityViewController alloc] initWithActivityItems:activityItems applicationActivities:applicationActivities];
[activityViewController setValue:@"My Subject Text" forKey:@"subject"];
And your UIActivityViewController is populated with a subject.
并且您的 UIActivityViewController 填充了一个主题。
回答by Namit Gupta
In iOS7, this is possible by using -
在 iOS7 中,这可以通过使用 -
activityViewController:subjectForActivityType:
When posting an item the service may provide for a separate subject field and data field, such as an email message. Implement this method if you wish to provide a subject field for services that support one.
在发布项目时,该服务可能会提供单独的主题字段和数据字段,例如电子邮件。如果您希望为支持某个服务的服务提供主题字段,请实现此方法。
回答by esp
1 and 2: How do I set recipients for UIActivityViewController in iOS 6?
1 和 2:如何在 iOS 6 中为 UIActivityViewController 设置收件人?
Although both provided methods are a bit of a hack, especially the first one, it is possible.
尽管提供的两种方法都有点小技巧,尤其是第一种方法,但这是可能的。
3: it is possible to share different content on different services, but the number of items and their types should be the same (but it is not a limitation, really, as you can return nil for items you don't need on particular service). You have to create sharing items after the service was selected using UIActivityItemSource protocol
3:可以在不同的服务上共享不同的内容,但项目的数量和类型应该是相同的(但这不是一个限制,真的,因为你可以为特定服务不需要的项目返回 nil )。您必须在使用 UIActivityItemSource 协议选择服务后创建共享项
Code I use:
我使用的代码:
Show UIActivityViewController with the current controller as provider of all items (it should have in .h file):
使用当前控制器显示 UIActivityViewController 作为所有项目的提供者(它应该在 .h 文件中):
const int numberOfSharedItems = 5;
- (IBAction)shareAction:(id)sender
{
NSMutableArray *shareItems = [NSMutableArray new];
while ([shareItems count] < numberOfSharedItems)
[shareItems addObject: self];
UIActivityViewController *shareController =
[[UIActivityViewController alloc]
// actual items are prepared by UIActivityItemSource protocol methods below
initWithActivityItems: shareItems
applicationActivities :nil];
shareController.excludedActivityTypes = @[UIActivityTypeMessage, UIActivityTypePrint, UIActivityTypeCopyToPasteboard, UIActivityTypeAssignToContact, UIActivityTypeSaveToCameraRoll];
[self presentViewController: shareController animated: YES completion: nil];
}
Make placeholders for items that will be shared:
为将要共享的项目制作占位符:
-(id)activityViewControllerPlaceholderItem:(UIActivityViewController *)activityViewController
{
static UIActivityViewController *shareController;
static int itemNo;
if (shareController == activityViewController && itemNo < numberOfSharedItems - 1)
itemNo++;
else {
itemNo = 0;
shareController = activityViewController;
}
switch (itemNo) {
case 0: return @""; // intro in email
case 1: return @""; // email text
case 2: return [NSURL new]; // link
case 3: return [UIImage new]; // picture
case 4: return @""; // extra text (via in twitter, signature in email)
default: return nil;
}
}
Make actual items that will be shared, differently for different services:
为不同的服务制作不同的共享项目:
-(id)activityViewController:(UIActivityViewController *)activityViewController itemForActivityType:(NSString *)activityType
{
// the number of item to share
static UIActivityViewController *shareController;
static int itemNo;
if (shareController == activityViewController && itemNo < numberOfSharedItems - 1)
itemNo++;
else {
itemNo = 0;
shareController = activityViewController;
}
NSString *shareText = [self shareText]; // whatever you fancy
NSURL *shareURL = [self shareURL];
// twitter
if ([activityType isEqualToString: UIActivityTypePostToTwitter])
switch (itemNo) {
case 0: return nil;
case 1: return shareText; // you can change text for twitter, I add $ to stock symbol inside shareText here, e.g. Hashtags can be added too
case 2: return shareURL;
case 3: return nil; // no picture
case 4: return @"via @YourApp";
default: return nil;
}
// email
else if ([activityType isEqualToString: UIActivityTypeMail])
switch (itemNo) {
case 0: return @"Hi!\r\n\r\nI used YourApp\r\n";
case 1: return shareText;
case 2: return shareURL;
case 3: return nil; // no picture
case 4: return [@"\r\nCheck it out.\r\n\r\nCheers\r\n" stringByAppendingString: [self userName]];
default: return nil;
}
else // Facebook or something else in the future
switch (itemNo) {
case 0: return nil;
case 1: return shareText;
case 2: return shareURL;
case 3: return [self shareImage];
case 4: return nil;
default: return nil;
}
}
回答by KP_G
u can simply create a class as follows :
你可以简单地创建一个类,如下所示:
@interface MYNActivityProvider : UIActivityItemProvider <UIActivityItemSource>
@end
// implementation
// 执行
- (id) activityViewController:(UIActivityViewController *)activityViewController
itemForActivityType:(NSString *)activityType
{
if ( [activityType isEqualToString:UIActivityTypePostToTwitter] ) {
return stringToPost;
}
if ( [activityType isEqualToString:UIActivityTypePostToFacebook] ) {
return stringToPost;
}
if ( [activityType isEqualToString:UIActivityTypeMessage] ) {
return @"SMS message text";
}
if ( [activityType isEqualToString:UIActivityTypeMail] ) {
return @"Email text here!";
}
if ( [activityType isEqualToString:@"Custom"] ) {
return @"app custom text";
}
return nil;
}
回答by samwize
You might want to try OvershareKit.
您可能想尝试OvershareKit。
We are frequently asked why someone would use OvershareKit instead of UIActivityViewController (UIAVC) and UIActivity. UIAVC is great for apps that know they'll never have a need for any of the following:
- Never need to integrate with more than one or two third party services.
- Never need to tweak the UI for the activity sheet and sharing screens.
- Never care to provide separate, media-specific content for each sharing type (email versus SMS, etc.)
- Never need to have multiple items such as a Copy Text versus a Copy Link in the same sheet.
- Don't mind that all non-system-provided activities get stuck with boring monochromatic icons.
我们经常被问到为什么有人会使用 OvershareKit 而不是 UIActivityViewController (UIAVC) 和 UIActivity。UIAVC 非常适合那些知道他们永远不需要以下任何一项的应用程序:
- 永远不需要与一两个以上的第三方服务集成。
- 永远不需要调整活动表和共享屏幕的 UI。
- 从不关心为每种共享类型(电子邮件与 SMS 等)提供单独的、特定于媒体的内容
- 永远不需要在同一工作表中有多个项目,例如复制文本与复制链接。
- 不要介意所有非系统提供的活动都会被无聊的单色图标卡住。
Your situation is (3) - a need to care about different content for different sharing type.
您的情况是 (3) - 需要关注不同共享类型的不同内容。