是否有可能知道在iOS中使用哪种媒体进行共享?

时间:2016-09-09 05:36:33

标签: ios

我想弄清楚是否有办法捕获所选媒体进行分享。我有一个iOS应用程序,并且有一个共享预定义模板的共享按钮。当用户点击共享按钮时,会向他显示可用于共享该应用的应用程序,如果用户选择Facebook或WhatsApp或Skype,则会将其重定向到所选媒体的本机应用程序。现在我想知道他们选择了什么。

2 个答案:

答案 0 :(得分:1)

您可以使用此代码与UIActivityViewController共享目标C

- (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];
}

在Swift中使用UIActivityViewController:

@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上进行特定共享你可以在目标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)
}
}

答案 1 :(得分: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: {})