是否有可能知道在 iOS 中用于共享的媒介是啥?
Posted
技术标签:
【中文标题】是否有可能知道在 iOS 中用于共享的媒介是啥?【英文标题】:Is it possible to know what medium is used for sharing in iOS?是否有可能知道在 iOS 中用于共享的媒介是什么? 【发布时间】:2016-09-09 05:36:32 【问题描述】:我正在尝试找出是否有办法捕获所选媒体以进行共享。我有一个 ios 应用程序,并且有一个共享按钮,它共享一个预定义的模板。当用户点击分享按钮时,他会看到可用于分享的应用程序,如果用户选择 Facebook、WhatsApp 或 Skype,他们将被重定向到他们选择的媒体的本机应用程序。现在我想知道他们选择了什么。
【问题讨论】:
你用什么来分享这个?UIActivityViewController
?您是否查看过其完成处理程序的文档?
@rmaddy 是的,我将使用UIActivityViewController
【参考方案1】:
您可以使用 UIActivityViewController 的 completionWithItemsHandler
来检查用户选择了哪个活动类型以及他们是否完成了活动或取消了活动:
let activityViewController = UIActivityViewController(activityItems:[ "Hello"], applicationActivities: nil)
activityViewController.completionWithItemsHandler = (activityType, completed, returnedItems, error) in
//check completed
if completed
//check activity type using activity type
if activityType!==UIActivityTypePostToFacebook
//Facebook
and so on
presentViewController(activityViewController, animated: true, completion: )
【讨论】:
【参考方案2】:您可以使用此代码在 Objective C 中与 UIActivityViewController 共享
- (IBAction)shareButton:(UIBarButtonItem *)sender
NSString *textToShare = @"Look at this awesome website for aspiring iOS Developers!";
NSURL *myWebsite = [NSURL URLWithString:@"http://www.codingexplorer.com/"];
NSArray *objectsToShare = @[textToShare, myWebsite];
UIActivityViewController *activityVC = [[UIActivityViewController alloc] initWithActivityItems:objectsToShare applicationActivities:nil];
NSArray *excludeActivities = @[UIActivityTypeAirDrop,
UIActivityTypePrint,
UIActivityTypeAssignToContact,
UIActivityTypeSaveToCameraRoll,
UIActivityTypeAddToReadingList,
UIActivityTypePostToFlickr,
UIActivityTypePostToVimeo];
activityVC.excludedActivityTypes = excludeActivities;
[self presentViewController:activityVC animated:YES completion:nil];
在带有 UIActivityViewController 的 Swift 中:
@IBAction func shareButtonClicked(sender: UIButton)
let textToShare = "Swift is awesome! Check out this website about it!"
if let myWebsite = NSURL(string: "http://www.codingexplorer.com/")
let objectsToShare = [textToShare, myWebsite]
let activityVC = UIActivityViewController(activityItems: objectsToShare, applicationActivities: nil)
//New Excluded Activities Code
activityVC.excludedActivityTypes = [UIActivityTypeAirDrop, UIActivityTypeAddToReadingList]
//
activityVC.popoverPresentationController?.sourceView = sender
self.presentViewController(activityVC, animated: true, completion: nil)
如果你想在 facebook 上进行特定分享,你可以在 Objective C 中使用 SLComposeViewController
if ([SLComposeViewController isAvailableForServiceType:SLServiceTypeFacebook])
fbSLComposeViewController = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeFacebook];
[fbSLComposeViewController addImage:someImage];
[fbSLComposeViewController setInitialText:@"Some Text"];
[self presentViewController:fbSLComposeViewController animated:YES completion:nil];
fbSLComposeViewController.completionHandler = ^(SLComposeViewControllerResult result)
switch(result)
case SLComposeViewControllerResultCancelled:
NSLog(@"facebook: CANCELLED");
break;
case SLComposeViewControllerResultDone:
NSLog(@"facebook: SHARED");
break;
;
else
UIAlertView *fbError = [[UIAlertView alloc] initWithTitle:@"Facebook Unavailable" message:@"Sorry, we're unable to find a Facebook account on your device.\nPlease setup an account in your devices settings and try again." delegate:self cancelButtonTitle:@"Close" otherButtonTitles:nil];
[fbError show];
Swift 中的 SLComposeViewController
@IBAction func facebookButtonPushed(sender: UIButton)
if SLComposeViewController.isAvailableForServiceType(SLServiceTypeFacebook)
var facebookSheet:SLComposeViewController = SLComposeViewController(forServiceType: SLServiceTypeFacebook)
facebookSheet.setInitialText("Share on Facebook")
self.presentViewController(facebookSheet, animated: true, completion: nil)
else
var alert = UIAlertController(title: "Accounts", message: "Please login to a Facebook account to share.", preferredStyle: UIAlertControllerStyle.Alert)
alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: nil))
self.presentViewController(alert, animated: true, completion: nil)
【讨论】:
请先阅读问题,我知道如何分享,但想在分享时获取用户选择的分享媒体,这样我就可以跟踪大多数人使用的是什么媒体分享。以上是关于是否有可能知道在 iOS 中用于共享的媒介是啥?的主要内容,如果未能解决你的问题,请参考以下文章
如何将一个应用程序添加到 App Store,而在这个应用程序中,一个用于 ios7,一个用于其余的?