美文网首页
iOS 自定义UIActivityViewController

iOS 自定义UIActivityViewController

作者: 桀骜不驯的搬砖者 | 来源:发表于2018-12-29 09:20 被阅读0次
    一、系统用法

    1.1. 现在的微信SDK,暂时不支持,多图和视频发给好友。但是系统的分享确实可以的。下面来实现一波:

    #1.如果是多图,items 存的是图片,所以我们要先把图片拿到,放置在 items 中即可
    #2.如果是视频,items 存的就是视频的沙盒地址,然后包装成一个 NSURL 对象
     NSMutableArray *items = [NSMutableArray array];
    #1.1.图片
    #1.2.视频
    //
    //if ([filePath hasPrefix:@"file://"]) {
    //                filePath = [filePath stringByReplacingOccurrencesOfString:@"file://" withString:@""];
     //          }
     //        NSURL *videoUrl = [NSURL fileURLWithPath:filePath];
    //}
     UIActivityViewController *activityVC = [[UIActivityViewController alloc] initWithActivityItems:items applicationActivities:nil];
                activityVC.completionWithItemsHandler = ^(UIActivityType  _Nullable activityType, BOOL completed, NSArray * _Nullable returnedItems, NSError * _Nullable activityError) {
                    NSLog(@"%@",activityType);
                    if (completed) {
                        
                    }
                };
     [self presentViewController:activityVC animated:YES completion:nil];
    

    上面说的是分享的常规操作


    二、自定义用法
    自定义效果图 1
    2.1 先做黑色提示部分
    原理:自定义一个类继承于 UIActivityViewController,然后重写它相应的 view 方法,然后在里面添加相应的操作即可。如下:

    1.首先自定义一个类继承于 UIActivityViewController,叫 XXActivityViewController

    //XXActivityViewController.h 文件
    #import <UIKit/UIKit.h>
    
    NS_ASSUME_NONNULL_BEGIN
    
    @interface XXActivityViewController : UIActivityViewController
    
    @end
    
    NS_ASSUME_NONNULL_END
    
    //XXActivityViewController.m 文件
    #import "XXActivityViewController.h"
    
    @interface XXActivityViewController ()
    
    @property (nonatomic, weak) UIView *tipView;
    
    @end
    
    @implementation XXActivityViewController
    
    //重写 view 方法
    - (void)viewWillAppear:(BOOL)animated
    {
        [super viewWillAppear:animated];
        //这里延迟加载一下,可能有的机型会错乱
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(.25 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{    
            CGFloat tipViewH = 100;
            //这里 10:是黑色与它的间隔; 57:是取消按钮; 15:是间隔;safeAreaBottom:是底部安全距离一个宏而已
            UIView *av = [[UIView alloc] initWithFrame:CGRectMake(self.view.frame.origin.x, SCREEN_HEIGHT - self.view.frame.size.height - tipViewH - 10 - 57 - 15 + safeAreaBottom, self.view.frame.size.width, tipViewH)];
            av.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:.8];
            av.layer.cornerRadius = 5.0;
            av.layer.masksToBounds = YES;
            [[UIApplication sharedApplication].delegate.window addSubview:av];
            _tipView = av;
            
            //
            UILabel *lb = [[UILabel alloc] initWithFrame:CGRectMake(12, 15, self.view.frame.size.width - 12 * 2, tipViewH - 15 * 2)];
            lb.numberOfLines = 0;
            lb.text = @"文案已复制,选择分享到微信好友/群或朋友圈时,长按粘贴文案。\n好友通过识别分享的海报二维码即可进入你的店铺。";
            lb.font = [UIFont boldSystemFontOfSize:14];
            lb.textColor = [UIColor whiteColor];
            [av addSubview:lb];
        });
    }
    
    #//消失的时候,记得把我们添加的控件移除。否则会留在当前窗口
    - (void)viewWillDisappear:(BOOL)animated
    {
        [super viewWillDisappear:animated];
        [_tipView removeFromSuperview];
    }
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        
    }
    
    - (void)dealloc
    {
        NSLog(@"%s  %@", __FUNCTION__, self.class);
    }
    
    @end
    
    2.2 手动发圈按钮制作
    原理:自定义一个类继承于 UIActivity,然后重写它相应的方法,然后在里面添加相应的操作即可。如下:
    //XXActivity.h
    #import <UIKit/UIKit.h>
    
    NS_ASSUME_NONNULL_BEGIN
    
    typedef void(^ActionBlock)(void);
    
    @interface XXActivity : UIActivity
    
    @property (nonatomic, nullable) UIActivityType xxActivityType;
    @property (nonatomic, nullable) NSString *xxActivityTitle;
    @property (nonatomic, nullable) UIImage *xxActivityImage;
    @property (nonatomic, nullable) NSString *xxActivityUrl;
    @property (nonatomic, copy) ActionBlock itemActionCallBack;
    
    - (id)initWithTitle:(NSString *)title image:(UIImage *)image openUrl:(NSString *)openUrl;
    
    @end
    
    NS_ASSUME_NONNULL_END
    
    //XXActivity.m
    #import "xxActivity.h"
    
    @implementation xxActivity
    
    - (id)initWithTitle:(NSString *)title image:(UIImage *)image openUrl:(NSString *)openUrl
    {
        self = [super init];
        if (self) {
            self.xxActivityTitle = title;
            self.xxActivityUrl = openUrl;
            self.xxActivityImage = image;
        }
        return self;
    }
    
    + (UIActivityCategory)activityCategory {
    #//UIActivityCategoryAction 是下面那排灰色的,但是要注意图片的问题,图片有颜色的地方都是显示成灰色的,透明的才是白色显示。(就是和平时做图相反。)
        return UIActivityCategoryShare;
    }
    - (NSString *)activityType { return @"customAppType"; }
    - (NSString *)activityTitle { return self.xxActivityTitle;}
    - (UIImage *)activityImage { return self.xxActivityImage; }
    - (BOOL)canPerformWithActivityItems:(NSArray *)activityItems { return YES; }
    - (void)prepareWithActivityItems:(NSArray *)activityItems {
        
    }
    - (UIViewController *) activityViewController { return nil; }
    - (void)performActivity {
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:self.xxActivityUrl] options:@{UIApplicationOpenURLOptionsSourceApplicationKey : @(YES)} completionHandler:^(BOOL success) {
            if (!success) {
                if ([self.xxActivityUrl hasPrefix:@"wechat://"]) {
                   NSLog(@"请先安装微信客户端");
                }
            }else{
                if (_itemActionCallBack) {
                    _itemActionCallBack();
                }
            }
        }];
    }
    
    到此重写结束,下面是使用:
    #//这里的 items 就是上面开始说的东西(视频或图片),并不是软件哦
    - (void)showSystemShareAlertVCWithItems:(NSArray *)items
    {
        XXActivity *lineTimeActivity = [[XXActivity alloc] initWithTitle:@"手动发圈" image:IMG(@"jiaji_icon_penyq") openUrl:@"wechat://"];
        XXActivityViewController *activityVC = [[XXActivityViewController alloc] initWithActivityItems:items applicationActivities:@[lineTimeActivity]];
    //这里是排除哪些系统自带不要的
        activityVC.excludedActivityTypes = @[UIActivityTypePostToFacebook,UIActivityTypePostToTwitter, UIActivityTypePostToWeibo,UIActivityTypeMessage,UIActivityTypeMail,UIActivityTypePrint,UIActivityTypeCopyToPasteboard,UIActivityTypeAssignToContact,UIActivityTypeAddToReadingList,UIActivityTypePostToFlickr,UIActivityTypePostToVimeo,UIActivityTypePostToTencentWeibo,UIActivityTypeAirDrop,UIActivityTypeOpenInIBooks];
        activityVC.completionWithItemsHandler = ^(UIActivityType  _Nullable activityType, BOOL completed, NSArray * _Nullable returnedItems, NSError * _Nullable activityError) {
            NSLog(@"%@",activityType);
            if (completed) {
                
            }
        };
        [self presentViewController:activityVC animated:YES completion:nil];
    }
    

    本文到此结束。

    总结:在自定义系统控件时,其实原理就是:重写它的方法,然后加入我们自己的实现。

    声明:本文仅是学习笔记,如果涉及违法违规行为请联系删除,谢谢🙏

    相关文章

      网友评论

          本文标题:iOS 自定义UIActivityViewController

          本文链接:https://www.haomeiwen.com/subject/xgomkqtx.html