利用UIActivityController调用ios系统自带的分享功能,实现微信发布多图的功能
通过一番查找以后找到一个类UIActivityController,可以调用系统的social.framework中的分享接口。看下面的图就知道了,这个还是挺常见的
微信发布多图
借鉴了CSDN上的一个demo,
里面自定义了一个实现UIActivityItemSource协议的类 ShareItem
//
// SharedItem.h
// xingjiang
//
// Created by 吴德明 on 16/7/14.
// Copyright © 2016年 吴德明. All rights reserved.
//
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
@interface SharedItem : NSObject<UIActivityItemSource>
-(instancetype)initWithData:(UIImage*)img andFile:(NSURL*)file;
@property (nonatomic, strong) UIImage *img;
@property (nonatomic, strong) NSURL *path;
@end
实现文件
//
// SharedItem.m
// xingjiang
//
// Created by 吴德明 on 16/7/14.
// Copyright © 2016年 吴德明. All rights reserved.
//
#import "SharedItem.h"
@implementation SharedItem
-(instancetype)initWithData:(UIImage *)img andFile:(NSURL *)file
{
self = [super init];
if (self) {
_img = img;
_path = file;
}
return self;
}
-(instancetype)init
{
//不期望这种初始化方式,所以返回nil了。
return nil;
}
#pragma mark - UIActivityItemSource
-(id)activityViewControllerPlaceholderItem:(UIActivityViewController *)activityViewController
{
return _img;
}
-(id)activityViewController:(UIActivityViewController *)activityViewController itemForActivityType:(NSString *)activityType
{
return _path;
}
-(NSString*)activityViewController:(UIActivityViewController *)activityViewController subjectForActivityType:(NSString *)activityType
{
// 这里对我这分享图好像没啥用.... 是的 没啥用....
return @"";
}
实际调用的代码
//
// ViewController.m
// ShareItemWX
//
// Created by 吴德明 on 16/7/14.
// Copyright © 2016年 吴德明. All rights reserved.
//
#import "ViewController.h"
#import "SharedItem.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
UIButton *Button = [UIButton buttonWithType:UIButtonTypeCustom];
Button.frame = CGRectMake(0, 0, 100, 100);
Button.center = self.view.center;
Button.backgroundColor = [UIColor blueColor];
[Button setTitle:@"分享到微信" forState:UIControlStateNormal];
[Button addTarget:self action:@selector(shareWX) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:Button];
}
/** 分享到微信 以九宫格的方式*/
-(void)shareWX{
/** 图片网址数组*/
NSArray *array_photo = @[@"http://img.meifajia.com/o1aneipt09eCl5bqQp4ifbQdTHlKIJfq.jpg?imageView2/1/w/360/h/480/q/85",@"http://img.meifajia.com/o1aneipt2fbZm38Zct4DH92p-ez7-fXt.jpg?imageView2/1/w/360/h/480/q/85",@"http://img.meifajia.com/o1aneiocd24Y6jK8uQA8-8y-47H6vRe7.jpg?imageView2/1/w/360/h/480/q/85",@"http://img.meifajia.com/o1aneiocdd94h6ld4kQJh8PcpjGSkORS.jpg?imageView2/1/w/360/h/480/q/85",@"http://img.meifajia.com/o1aneiocdd94h6ld4kQJh8PcpjGSkORS.jpg?imageView2/1/w/360/h/480/q/85",@"http://img.meifajia.com/o1aneipt09eCl5bqQp4ifbQdTHlKIJfq.jpg?imageView2/1/w/360/h/480/q/85",@"http://img.meifajia.com/o1aneioccpacV1LVg2AfG9fbYl8zN1So.jpg?imageView2/1/w/360/h/480/q/85",@"http://img.meifajia.com/o1aneipt0haf1zwepSkxx9okI0W34t05.jpg?imageView2/1/w/360/h/480/q/85",@"http://img.meifajia.com/o1aneipt09eCl5bqQp4ifbQdTHlKIJfq.jpg?imageView2/1/w/360/h/480/q/85",@"http://img.meifajia.com/o1aneipt09eCl5bqQp4ifbQdTHlKIJfq.jpg?imageView2/1/w/360/h/480/q/85",@"http://img.meifajia.com/o1aneipt09eCl5bqQp4ifbQdTHlKIJfq.jpg?imageView2/1/w/360/h/480/q/85"];
NSMutableArray *array = [[NSMutableArray alloc]init];
for (int i = 0; i <8 && i<array_photo.count; i++) {
NSString *URL = array_photo[i];
NSData * data = [NSData dataWithContentsOfURL:[NSURL URLWithString:URL]];
UIImage *imagerang = [UIImage imageWithData:data];
NSString *path_sandox = NSHomeDirectory();
NSString *imagePath = [path_sandox stringByAppendingString:[NSString stringWithFormat:@"/Documents/ShareWX%d.jpg",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];
}
UIActivityViewController *activityViewController =[[UIActivityViewController alloc] initWithActivityItems:array
applicationActivities:nil];
//尽量不显示其他分享的选项内容
activityViewController.excludedActivityTypes = @[ UIActivityTypePostToFacebook,UIActivityTypePostToTwitter, UIActivityTypePostToWeibo, UIActivityTypeMessage,UIActivityTypeMail,UIActivityTypePrint,UIActivityTypeCopyToPasteboard,UIActivityTypeAssignToContact,UIActivityTypeSaveToCameraRoll,UIActivityTypeAddToReadingList,UIActivityTypePostToFlickr,UIActivityTypePostToVimeo,UIActivityTypePostToTencentWeibo,UIActivityTypeAirDrop,UIActivityTypeOpenInIBooks];
if (activityViewController) {
[self presentViewController:activityViewController animated:TRUE completion:nil];
}
}
@end
最终效果
中间用户的操作步骤
app界面
第一步
第二步
第三步写评论
ios 版本9.2.1
微信版本6.3.22