UIActivityViewController使用

UIActivityViewController

在使用了QQ、微信等的原生与第三方分享SDK之后,我更喜欢iOS自带分享框架UIActivityViewController,UIActivityViewController继承自UIViewController,简直不能更加便捷简单了,也不用去申请QQ、微信(每年¥300)等的key与id了,好了下面就来看UIActivityViewController的基本用法,自定义有空再写。一定要记得判断iPad环境,不然在iPad上直接present要crash,且crash原因会是UIPopoverPresentationController (<_UIAlertControllerActionSheetRegularPresentationController: 0x7fce54d70b50>) should have a non-nil sourceView or barButtonItem set before the presentation occurs.这就需要你判断为iPad环境时需要pop,加上popoverPresentationController.sourceView就好了。

NSString *textToShare = @"我就是喜欢踩坑呢,咋的了。生活不止眼前的苟且,还有翔和未来的苟且。";
UIImage *imageShare = [UIImage imageNamed:@"这儿放你自己的图片"];
NSURL *urlShare = [NSURL URLWithString:@"http://www.jianshu.com/u/9ab932a98604"];
NSArray *activityItems = @[textShare, imageShare, urlShare];
UIActivityViewController *actVC = [[UIActivityViewController alloc] initWithActivityItems:activityItems applicationActivities:nil];
//下面的excludedActivityTypes填写你需要剔除的(比如打印UIActivityTypePrint,短信UIActivityTypeMessage),不写excludedActivityTypes这一行即使用系统默认的,好了,我们来看一下excludedActivityTypes的所有值。

UIKIT_EXTERN NSString *const UIActivityTypePostToFacebook     NS_AVAILABLE_IOS(6_0) __TVOS_PROHIBITED;    //发送到Facebook   
UIKIT_EXTERN NSString *const UIActivityTypePostToTwitter      NS_AVAILABLE_IOS(6_0) __TVOS_PROHIBITED;    //发送到Twitter
UIKIT_EXTERN NSString *const UIActivityTypePostToWeibo        NS_AVAILABLE_IOS(6_0) __TVOS_PROHIBITED;    //发送到新浪微博
UIKIT_EXTERN NSString *const UIActivityTypeMessage            NS_AVAILABLE_IOS(6_0) __TVOS_PROHIBITED;    //短信
UIKIT_EXTERN NSString *const UIActivityTypeMail               NS_AVAILABLE_IOS(6_0) __TVOS_PROHIBITED;    //邮件
UIKIT_EXTERN NSString *const UIActivityTypePrint              NS_AVAILABLE_IOS(6_0) __TVOS_PROHIBITED;    //打印
UIKIT_EXTERN NSString *const UIActivityTypeCopyToPasteboard   NS_AVAILABLE_IOS(6_0) __TVOS_PROHIBITED;    //拷贝
UIKIT_EXTERN NSString *const UIActivityTypeAssignToContact    NS_AVAILABLE_IOS(6_0) __TVOS_PROHIBITED;    //指定给联系人
UIKIT_EXTERN NSString *const UIActivityTypeSaveToCameraRoll   NS_AVAILABLE_IOS(6_0) __TVOS_PROHIBITED;    //保存至相机胶卷
UIKIT_EXTERN NSString *const UIActivityTypeAddToReadingList   NS_AVAILABLE_IOS(7_0) __TVOS_PROHIBITED;    //添加至阅读列表
UIKIT_EXTERN NSString *const UIActivityTypePostToFlickr       NS_AVAILABLE_IOS(7_0) __TVOS_PROHIBITED;    //发送到Flickr(雅虎旗下图片分享网站)
UIKIT_EXTERN NSString *const UIActivityTypePostToVimeo        NS_AVAILABLE_IOS(7_0) __TVOS_PROHIBITED;    //发送到Vimeo
UIKIT_EXTERN NSString *const UIActivityTypePostToTencentWeibo NS_AVAILABLE_IOS(7_0) __TVOS_PROHIBITED;    //发送到腾讯微博
UIKIT_EXTERN NSString *const UIActivityTypeAirDrop            NS_AVAILABLE_IOS(7_0) __TVOS_PROHIBITED;    //AirDrop
UIKIT_EXTERN NSString *const UIActivityTypeOpenInIBooks       NS_AVAILABLE_IOS(9_0) __TVOS_PROHIBITED;    //在iBooks中打开

actVC.excludedActivityTypes = @[UIActivityTypeMessage,UIActivityTypePrint,UIActivityTypeSaveToCameraRoll,UIActivityTypeAddToReadingList,UIActivityTypePostToFlickr,UIActivityTypePostToVimeo,UIActivityTypeAirDrop];
//这儿一定要做iPhone与iPad的判断,因为这儿只有iPhone可以present,iPad需pop,所以这儿actVC.popoverPresentationController.sourceView = self.view;在iPad下必须有,不然iPad会crash,self.view你可以换成任何view,你可以理解为弹出的窗需要找个依托。
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
    actVC.popoverPresentationController.sourceView = self.view;
    [self presentViewController:actVC animated:YES completion:nil];
} else {
    [self presentViewController:actVC animated:YES completion:nil];
}

你可能感兴趣的:(UIActivityViewController使用)