2016-09-09 20 views
2

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

+0

? 'UIActivityViewController'?你看过它的完成处理程序的文档吗? – rmaddy

+0

@rmaddy是的,我要使用'UIActivityViewController' –

回答

1

您可以使用UIActivityViewController的completionWithItemsHandler检查其activitytype用户选择,以及他们是否完成了活动或取消它:你使用这种共享

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

您可以在目标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:如果你想perticular在Facebook上分享,您可在Objective C中使用SLComposeViewController

@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) 
} 
} 

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

SLComposeViewControlle中的R斯威夫特

@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

请先阅读这个问题,我知道如何分享,但是想分享时选择用户分享的媒体,所以我可以跟踪媒体正在用于大部分共享。 –

相关问题