ios关于自带分享UIActivityViewController

/**
 *  全屏截图
 *
 */
-(UIImage *)fullScreenshots{
    UIWindow *screenWindow = [[UIApplication sharedApplication] keyWindow];
    UIGraphicsBeginImageContext(screenWindow.frame.size);
    [screenWindow.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return viewImage;
}

/**
 *  自带分享
 *  @param vc
 *  @param view ipad分享,需要这个view
 */
-(void)comesShare:(UIViewController *)vc inView:(UIView *)view {
    
    NSString *title = @"标题";
    NSString *description = @"内容";
    UIImage *image = [self fullScreenshots];
    NSURL *url = [NSURL URLWithString:@""];
    
    NSArray *activityItems = @[title,description,image,url];
    
    //创建自定义的Activity
    MyActivity *act1 = [[MyActivity alloc]initWithImage:image atURL:nil atTitle:@"title" atShareContentArray:activityItems];
    NSArray *apps = @[act1];

    //创建
    UIActivityViewController *activityVC = [[UIActivityViewController alloc]initWithActivityItems:activityItems applicationActivities:apps];
    
    //不出现在活动项目
    activityVC.excludedActivityTypes = @[
                                         //UIActivityTypePostToFacebook,
                                         //UIActivityTypePostToTwitter,
                                         //UIActivityTypePostToWeibo,//新浪微博
                                         //UIActivityTypeMessage,//信息
                                         UIActivityTypeMail,
                                         //UIActivityTypePrint,//打印
                                         //UIActivityTypeCopyToPasteboard,//拷贝
                                         //UIActivityTypeAssignToContact,//指定联系人
                                         //UIActivityTypeSaveToCameraRoll,//保存到相机胶卷
                                         //UIActivityTypeAddToReadingList,//加入阅读列表
                                         //UIActivityTypePostToFlickr,
                                         //UIActivityTypePostToVimeo,
                                         //UIActivityTypePostToTencentWeibo,//腾讯微博
                                         //UIActivityTypeAirDrop
                                         //UIActivityTypeOpenInIBooks
                                         ];
    //判断系统版本,初始化点击回调方法
    if([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0){
        UIActivityViewControllerCompletionWithItemsHandler myBlock = ^(NSString *activityType,BOOL completed,NSArray *returnedItems,NSError *activityError) {
            if (completed) {
                NSLog(@"completed");
            }else {
                NSLog(@"cancel");
            }
        };
        
        activityVC.completionWithItemsHandler = myBlock;
    }else{
        UIActivityViewControllerCompletionHandler myBlock = ^(NSString *activityType,BOOL completed) {
            if (completed) {
                NSLog(@"completed");
            }else {
                NSLog(@"cancel");
            }
        };
        activityVC.completionHandler = myBlock;
    }
    
   if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {//判断设备
        [vc presentViewController:activityVC animated:YES completion:nil];
   }else {
       UIPopoverController *popup = [[UIPopoverController alloc] initWithContentViewController:activityVC];
       [popup presentPopoverFromRect:CGRectMake(view.bounds.size.width/2, view.bounds.size.height, 0, 0) inView:view permittedArrowDirections:UIPopoverArrowDirectionUp animated:YES];
   }
    
}


MyActivity.h

#import 

@interface MyActivity : UIActivity
//title是当前类型,shareImage是分享图标,URL是要分享的地址,getShareArray保存用户要分享的内容
@property (nonatomic,strong) UIImage *shareImage;
@property (nonatomic,copy)NSString *URL;
@property (nonatomic,copy)NSString *title;
@property (nonatomic,strong)NSArray *getShareArray;
-(instancetype)initWithImage:(UIImage *)shareImage atURL:(NSString *)URL atTitle:(NSString *)title atShareContentArray:(NSArray *)shareContentArray;
@end

MyActivity.m

#import "MyActivity.h"

@implementation MyActivity
//初始化方法
-(instancetype)initWithImage:(UIImage *)shareImage atURL:(NSString *)URL atTitle:(NSString *)title atShareContentArray:(NSArray *)shareContentArray
{
    if(self = [super init]){
        _shareImage = shareImage;
        _URL = URL;
        _title = title;
        _getShareArray = [[NSArray alloc]initWithArray:shareContentArray];
    }
    return self;
}
//以下方法都是自定义UiActivity需要重写的方法
+(UIActivityCategory)activityCategory
{
    return UIActivityCategoryShare;
}
//设置类型
-(NSString *)activityType
{
    return _title;
}
//设置现实的标题
-(NSString *)activityTitle
{
    return _title;
}
//图片
-(UIImage *)activityImage
{
    return _shareImage;
}
//返回yes就行,这个表示该分享是不是在controller中显示
-(BOOL)canPerformWithActivityItems:(NSArray *)activityItems
{
    return YES;
}
//是不是自定义的controller,如果为空,则调用performActivity方法
- (UIViewController *)activityViewController
{
    return nil;
}
//点击分享图标之后触发的方法
-(void)performActivity
{
    if(nil == _title) {
        return;
    }
    NSLog(@"要分享的内容-----%@",_getShareArray);
    NSLog(@"分享的类型------ %@",_title);
    if([_title isEqualToString:@"share Renren"]){
        NSLog(@"---^^^   renren");
        //调用人人的sdk
    }else if([_title isEqualToString:@"share Sina"]){
        //调用新浪sdk
    }
}

@end


关于SLComposeViewController分享(ios)点击打开链接



我的业余技术微信公众号:YKJGZH,欢迎大家进入

你可能感兴趣的:(ios入门/OC)