App分享多图到第三方应用(朋友圈、微博等)

有时候,我们需要把自己App上的图片直接发布到微信朋友圈,而不是把链接发布到微信朋友圈,此时我们就需要用到多图分享功能了。

App分享多图到第三方应用(朋友圈、微博等)_第1张图片
Jietu20180730-105947.gif
1、重写UIActivityItemSource的方法

SharedItem.h

#import 
#import 

@interface SharedItem : NSObject

-(instancetype)initWithData:(UIImage*)img andFile:(NSURL*)file;

@property (nonatomic, strong) UIImage *img;
@property (nonatomic, strong) NSURL *path;

@end

SharedItem.m

#import "SharedItem.h"

@implementation SharedItem

-(instancetype)initWithData:(UIImage *)img andFile:(NSURL *)file
{
    self = [super init];
    if (self) {
        _img = img;
        _path = file;
    }
    return self;
}

#pragma mark - UIActivityItemSource
-(id)activityViewControllerPlaceholderItem:(UIActivityViewController *)activityViewController
{
    return _img;
}

-(id)activityViewController:(UIActivityViewController *)activityViewController itemForActivityType:(NSString *)activityType
{
    return _path;
}

@end
2、引用SharedItem.h并生成所需要的数据
//文字剪切板
UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
pasteboard.string = @"开发测试...";

//拿到本地图片
NSMutableArray * array_photo = [NSMutableArray array];
for (int i = 0; i<3; i++) {
    UIImage *img = [UIImage imageNamed:[NSString stringWithFormat:@"image%d",i]];
    [array_photo addObject:img];
}

//把图片放入SharedItem
NSMutableArray *array = [[NSMutableArray alloc]init];
 for (int i = 0; i < array_photo.count; i++) {
    UIImage *imagerang = array_photo[i];
    NSString *path_sandox = NSHomeDirectory();
    NSString *imagePath = [path_sandox stringByAppendingString:[NSString stringWithFormat:@"/Documents/ShareWX%d.png",i]];
        [UIImagePNGRepresentation(imagerang) writeToFile:imagePath atomically:YES];
        NSURL *shareobj = [NSURL fileURLWithPath:imagePath];
        /** 这里做个解释 imagerang : UIimage 对象  shareobj:NSURL 对象 这个方法的实际作用就是 在吊起微信的分享的时候 传递给他 UIimage对象,在分享的时候 实际传递的是 NSURL对象 达到我们分享九宫格的目的 */
    SharedItem *item = [[SharedItem alloc] initWithData:imagerang andFile:shareobj];
    [array addObject:item];
}
3、创建系统分享弹窗
//创建系统分享弹窗
_activityViewController =[[UIActivityViewController alloc] initWithActivityItems:array applicationActivities: nil];

//屏蔽分享的第反方App
 _activityViewController.excludedActivityTypes = @[UIActivityTypePostToFacebook,UIActivityTypeAssignToContact,UIActivityTypeAddToReadingList,UIActivityTypePrint,UIActivityTypeMessage,UIActivityTypePostToTwitter,UIActivityTypePostToFlickr,UIActivityTypePostToVimeo,UIActivityTypePostToTencentWeibo,UIActivityTypeOpenInIBooks,UIActivityTypeMail,UIActivityTypeMarkupAsPDF,UIActivityTypeCopyToPasteboard,UIActivityTypeSaveToCameraRoll,UIActivityTypeAirDrop,@"com.apple.mobilenotes.SharingExtension",@"com.apple.reminders.RemindersEditorExtension",@"com.apple.mobileslideshow.StreamShareService",@"com.google.Drive.ShareExtension"];
    
//弹出系统分享窗口
 [self presentViewController:_activityViewController animated:TRUE completion:nil];
4、分享成功后的回掉
//分享成功后的回掉
 __weak typeof(self) weakSelf = self;
 _activityViewController.completionWithItemsHandler = ^(UIActivityType  _Nullable activityType, BOOL completed, NSArray * _Nullable returnedItems, NSError * _Nullable activityError) {
    if (completed) {
        //成功后别忘了删除本地的文件
        pasteboard.string = @"";
        [weakSelf removeLocalImages:array.count];
    }
};

//删除存储在系统文件夹的临时文件
- (void)removeLocalImages:(NSInteger)count{
    NSFileManager *fileManager = [NSFileManager defaultManager];
    for (int i = 0; i < count; i++) {
        NSString *path_sandox = NSHomeDirectory();
        NSString *imagePath = [path_sandox stringByAppendingString:[NSString stringWithFormat:@"/Documents/ShareWX%d.png",i]];
        BOOL exists = [fileManager fileExistsAtPath:imagePath];
        if (exists) {
            NSError *err;
            [fileManager removeItemAtPath:imagePath error:&err];
        }
    }
}
以上就是App分享多图到第三方应用的大概内容,需要Demo的可以点开看。

你可能感兴趣的:(App分享多图到第三方应用(朋友圈、微博等))