首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >有没有可能知道在iOS中使用什么介质进行共享?

有没有可能知道在iOS中使用什么介质进行共享?
EN

Stack Overflow用户
提问于 2016-09-09 13:36:33
回答 2查看 232关注 0票数 2

我正在尝试弄清楚是否有一种方法可以捕获所选的媒体以供共享。我有一个iOS应用程序,还有一个分享按钮,可以分享一个预定义的模板。当用户点击分享按钮时,他将看到可用于分享的应用程序,如果用户选择Facebook、WhatsApp或Skype,这些应用程序将被重定向到他们选择的媒体的本地应用程序。现在我想知道他们选择了什么。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2016-09-09 20:47:39

您可以使用UIActivityViewController的completionWithItemsHandler来检查用户选择了哪种activity类型,以及他们是完成了activity还是取消了activity:

代码语言:javascript
复制
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: {})
票数 1
EN

Stack Overflow用户

发布于 2016-09-09 13:51:50

您可以使用此代码在objective C中与UIActivityViewController进行共享

代码语言:javascript
复制
- (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中:

代码语言:javascript
复制
@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

代码语言:javascript
复制
 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

代码语言:javascript
复制
 @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
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39404498

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档