我正在尝试弄清楚是否有一种方法可以捕获所选的媒体以供共享。我有一个iOS应用程序,还有一个分享按钮,可以分享一个预定义的模板。当用户点击分享按钮时,他将看到可用于分享的应用程序,如果用户选择Facebook、WhatsApp或Skype,这些应用程序将被重定向到他们选择的媒体的本地应用程序。现在我想知道他们选择了什么。
发布于 2016-09-09 20:47:39
您可以使用UIActivityViewController的completionWithItemsHandler来检查用户选择了哪种activity类型,以及他们是完成了activity还是取消了activity:
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: {})发布于 2016-09-09 13:51:50
您可以使用此代码在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)
}
}https://stackoverflow.com/questions/39404498
复制相似问题