我想弄清楚是否有办法捕获选定的媒体共享。我有一个iOS应用程序,并有一个共享按钮,共享预定义的模板。当用户点击分享按钮时,他会看到可用于分享的应用程序,如果用户选择Facebook或WhatsApp或Skype,则会将其重定向到所选媒体的本机应用程序。现在我想知道他们选择了什么。是否有可能知道在iOS中使用什么介质进行共享?
2
A
回答
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
请先阅读这个问题,我知道如何分享,但是想分享时选择用户分享的媒体,所以我可以跟踪媒体正在用于大部分共享。 –
相关问题
- 1. 有什么方法可以知道链接是否被共享(发送)?
- 2. 如何知道我的共享库是否正在使用?
- 3. 是否有可能知道iPhone什么时候进入睡眠状态?
- 4. 是否有可能通过共享扩展执行API调用
- 5. 使用共享数据库连接时需要知道什么?
- 6. 是否有可能知道从哪个
- 7. 是否有可能使用JAXB生成共享对象?
- 8. 是否有可能为QObject的父级使用共享指针?
- 9. 是否有可能“延长”共享内存的使用寿命?
- 10. 是否有可能在React Native和iOS/Android中共享Apollo缓存?
- 11. 是否有可能在php中知道(服务器)CPU性能?
- 12. 有什么办法可以知道DataGridViewCell是否可见?
- 13. 可以使用管道在不同进程中共享Dll吗?
- 14. 是否有可能在Haskell中检测共享?
- 15. 是否有可能知道python进程正在运行哪个脚本?
- 16. ios是否有任何共享套件
- 17. 是否有可能知道JFrame是否完全可见?
- 18. 有什么办法可以知道报告是否有订阅
- 19. 是否有可能知道ydn-db放置操作的进度?
- 20. 是否有可能知道该函数是否在PHP中的eval()中调用?
- 21. iOS 8分享扩展程序:是否有回调可用来知道用户是否在共享扩展正在呈现时退出主机应用程序?
- 22. 是否有可能在iOS
- 23. 如何知道驱动器中使用的是什么类型的光学介质.net
- 24. 使用sendfile(),是否有可能知道in_fd何时处于EOF?
- 25. 我想知道是否有可能使用Node.js的
- 26. 是否有可能在BigQuery中使用MD5进行散列?
- 27. 从共享点门户网站,我怎么知道什么版本的共享点正在运行?
- 28. 是否有可能在进程运行时阻止iTunes通知
- 29. 有什么方法可以知道下载是否发生
- 30. 有可能知道CLLocation点是否在MKCoordinateRegion中?
? 'UIActivityViewController'?你看过它的完成处理程序的文档吗? – rmaddy
@rmaddy是的,我要使用'UIActivityViewController' –