ios 发布到 facebook 和 twitter 时,如何自定义 UIActivityViewController 以显示 URL 链接?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12657817/
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
How do I customize a UIActivityViewController to show a URL link when posting to facebook and twitter?
提问by Ovi Tisler
So I'm trying out the new UIActivityViewController
in iOS 6, and it's really easy to get up and running, but I can't figure out how to control it like I want. So I want to post a link to an article and I also want to attach an image for the link. I DON'T want that image uploaded to some facebook album, just as a URL thumbnail.
所以我正在尝试UIActivityViewController
iOS 6 中的新功能,它真的很容易启动和运行,但我不知道如何像我想要的那样控制它。所以我想发布一篇文章的链接,我还想为链接附上一张图片。我不希望这样的图像上传到一些Facebook相册,只是作为一个URL的缩略图。
This is easy to accomplish in the facebook SDK as they give full control for it, but is there a way to do it with the UIActivityViewController
? Here's what I got:
这在 facebook SDK 中很容易实现,因为它们可以完全控制它,但是有没有办法用UIActivityViewController
? 这是我得到的:
NSArray *activityItems = @[[NSURL URLWithString:[article link]], [UIImage imageNamed:@"myStockImage"]];
UIActivityViewController *viewCont = [[UIActivityViewController alloc] initWithActivityItems:activityItems applicationActivities:nil];
[self presentViewController:viewCont animated:YES completion:nil];
And this works, but it uploads the image to an 'iOS album'. If I don't add the image in the array, then the facebook sheet looks like it's blank and the attachment shows a grayed out safari logo (like the thumbnail is missing!) In safari when you try to Facebook a link, it uses a screen grab of the page as the thumbnail, I want to do something like that
这有效,但它将图像上传到“iOS相册”。如果我不在数组中添加图像,那么 Facebook 工作表看起来像空白,附件显示灰色的 safari 徽标(就像缺少缩略图一样!)在 safari 中,当您尝试向 Facebook 发送链接时,它使用页面的屏幕抓取作为缩略图,我想做类似的事情
UPDATE :
更新 :
So here's what it looks like from my app (using UIActivityViewController or the SLCompose way). See how it is going to upload the picture to a photo album iOS Photos
所以这就是我的应用程序的样子(使用 UIActivityViewController 或 SLCompose 方式)。看看它将如何将图片上传到相册iOS Photos
Here's what I want it to look like (see Safari also) :
这是我想要的样子(也请参阅 Safari):
回答by camilomq
I've searched about this and I've come to the conclusion that using the UIActivityViewController
there's just no way to do what you intend to. Every time you try to share a photo it just makes you put the photo in one of your albums, "iOS" album by default.
我已经搜索过这个,我得出的结论是,使用UIActivityViewController
没有办法做你想做的事。每次尝试分享照片时,它只会让您将照片放入其中一个相册中,默认情况下为“iOS”相册。
In my case, I did not need to post a photo as long as an image was retrieved from the link I needed to share (just like when you post a link on your Facebook timeline and it automatically grabs a picture from that link, and the picture becomes the link). In order to do that, I figured out that if I posted only an URL it does not work. You have to post some initial text and then the URL, and you do so by putting the objects in that order within the activityItems
array.
就我而言,只要从我需要分享的链接中检索到图像,我就不需要发布照片(就像当您在 Facebook 时间轴上发布链接时,它会自动从该链接中抓取图片,然后图片变成链接)。为了做到这一点,我发现如果我只发布一个 URL,它就不起作用。您必须发布一些初始文本,然后发布 URL,您可以通过将对象按该顺序放入activityItems
数组中来实现。
This is my working code:
这是我的工作代码:
NSArray * activityItems = @[[NSString stringWithFormat:@"Some initial text."], [NSURL URLWithString:@"http://www.google.com"]];
NSArray * applicationActivities = nil;
NSArray * excludeActivities = @[UIActivityTypeAssignToContact, UIActivityTypeCopyToPasteboard, UIActivityTypePostToWeibo, UIActivityTypePrint, UIActivityTypeMessage];
UIActivityViewController * activityController = [[UIActivityViewController alloc] initWithActivityItems:activityItems applicationActivities:applicationActivities];
activityController.excludedActivityTypes = excludeActivities;
[self presentViewController:activityController animated:YES completion:nil];
I also noticed that if you post a photo along these objects, it will not retrieve any image from the link.
我还注意到,如果您沿着这些对象张贴照片,它将不会从链接中检索任何图像。
I hope this helps.
我希望这有帮助。
回答by mauriii
Updated the answer for Swift 3.0
更新了 Swift 3.0 的答案
let url = URL(string: "google.com")
let shareText = "Your string goes here"
let shareItems: [Any] = [shareText, url!]
let activityVC = UIActivityViewController(activityItems: shareItems, applicationActivities: nil)
activityVC.excludedActivityTypes = [.airDrop, .postToFlickr, .assignToContact, .openInIBooks]
self.present(activityVC, animated: true, completion: nil)
Facebook won't accept the string, but, like many other of the social media sites, will show an image/link box for the URL
Facebook 不会接受该字符串,但与许多其他社交媒体网站一样,它会显示 URL 的图像/链接框
回答by Jonny
If you don't specifically need to use UIActivityViewController using SLComposeViewController is easy to use for this purpose.
如果您不是特别需要使用 UIActivityViewController 使用 SLComposeViewController 很容易用于此目的。
+(void)presentShareWidgetWithText:(NSString*)text url:(NSURL*)url images:(NSArray*)images toService:(NSString*)service presentIn:(UIViewController*)parentViewController {
SLComposeViewController* controller = [SLComposeViewController composeViewControllerForServiceType:service];
if (controller == nil) {
DLog(@"Could not initialize SLComposeViewController for %@!", service);
return;
}
if (url != nil) {
if (![controller addURL:url])
DLog(@"Failed adding url %@!", url);
}
if (images != nil) {
for (UIImage* image in images) {
if (![controller addImage:image]) {
DLog(@"Failed adding image: %@", image);
}
}
}
if (![controller setInitialText:text]) {
DLog(@"Failed setting initial text: %@", text);
}
SLComposeViewControllerCompletionHandler handler = ^(SLComposeViewControllerResult result) {
switch (result) {
case SLComposeViewControllerResultCancelled:
DLog(@"SLComposeViewControllerResultCancelled");
break;
case SLComposeViewControllerResultDone:
DLog(@"SLComposeViewControllerResultDone, Shared on service: %@", service);
break;
default:
DLog(@"Unhandled result: %d", result);
break;
}
dispatch_async(dispatch_get_main_queue(), ^{
[parentViewController dismissViewControllerAnimated:YES completion:^{
DLog(@"Dismissed a controller here!");
}];
});
};
[controller setCompletionHandler:handler];
[parentViewController presentViewController:controller animated:YES completion:^{
DLog(@"Presented %@!", controller);
}];
}
回答by Tamilarasan
- (void)tweetURL:(NSString *)url title:(NSString *)title {
TWTweetComposeViewController *twitter = [[TWTweetComposeViewController alloc] init];
NSString *format = @"“%@” %@ /via @DesignSceneApp";
NSString *message = [NSString stringWithFormat:format, title, url]
NSUInteger idx = title.length;
while (![twitter setInitialText:message]) {
idx -= 5;
if (idx > 5) {
message = [NSString stringWithFormat:format,
[NSString stringWithFormat:@"%@…", [title substringToIndex:idx]],
url
];
} else {
// Give up on the title.
message = [NSString stringWithFormat:@"%@ /via @DesignSceneApp", url];
[twitter setInitialText:message];
break;
}
}
[self presentViewController:twitter animated:YES completion:nil];
}