iOS UIActivityViewController原生多图分享(swift)

[toc]

前言

UIActivityViewController是iOS平台提供的一种工具类,其中包含了一些社会化插件。iOS平台提供的社会化工具和我们日常使用的友盟、shareSDK、百度分享各有优缺点。

  • UIActivityViewController的优点:
    1. 可以像其他分享工具一样提供大多数平台单图配文分享,或者纯文本纯图片分享.
    2. UIActivityViewController可以一次分享多张图片
    3. 系统自动推测可以操作的平台,开发者可以根据需要保留
  • 缺点:
    1. 多图分享时不可以带文字
    2. 系统提供了许多不经常使用的分享平台
    3. 部分平台不支持多图和文字同时分享(例如:微信)

实现

最基本的使用:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
func activity() -> () {

let image = UIImage(named: "1.jpg")
/// applicationActivities 可以没有元素,系统会自动选择合适的平台
let activityController = UIActivityViewController(activityItems: ["分享的文字", image!], applicationActivities: [])

/// 可以排除一些不希望的平台 UIActivityType
activityController.excludedActivityTypes = [.postToTencentWeibo, .airDrop, .openInIBooks, .postToVimeo, .postToFlickr, .addToReadingList, .saveToCameraRoll, .assignToContact, .copyToPasteboard, .print, .mail]

/// 分享完成后可以看到分享平台的相关字符串
<!-- 部分打印内容
/// UIActivityType(_rawValue: com.apple.UIKit.activity.PostToWeibo)
/// UIActivityType(_rawValue: com.tencent.mqq.ShareExtension) // qq
/// UIActivityType(_rawValue: com.apple.mobilenotes.SharingExtension) 笔记
/// UIActivityType(_rawValue: com.tencent.xin.sharetimeline) weixin
/// com.apple.UIKit.activity.PostToFlickr
/// com.apple.UIKit.activity.PostToVimeo
-->

activityController.completionWithItemsHandler = {
(type, flag, array, error) -> Swift.Void in
print(type ?? "")
}

present(activityController, animated: true) { }
}

注意事项

  • 多图分享图片不可以过大,大图需要压缩
  • 分享可以是多张图片也可以是图片的URL
  • 如果图片名错误或不存在,分享时可能只有更多按钮
  • 部分平台不支持多图和文字同时分享(例如:微信)
  • 分享的item也可以是实现了UIActivityItemSource协议的类,该类涉及图片和标题
    相关示例
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#import <UIKit/UIKit.h>
@interface ActivityItem : NSObject<UIActivityItemSource>

- (instancetype)initWithImage:(UIImage *)image path:(NSURL *)path;

@end

#import "ActivityItem.h"

@interface ActivityItem ()
@property (nonatomic, strong) UIImage *image;
@property (nonatomic, strong) NSURL *path;
@end

@implementation ActivityItem

- (instancetype)initWithImage:(UIImage *)image path:(NSURL *)path
{
self = [super init];
if (self) {
_image = image;
_path = path;
}
return self;
}

/// 数据
- (id)activityViewControllerPlaceholderItem:(UIActivityViewController *)activityViewController{
return _image;
}

/// 路径
- (nullable id)activityViewController:(UIActivityViewController *)activityViewController itemForActivityType:(UIActivityType)activityType{
return _path;
}

@end

/// 调用 URL -> image
func demo3() -> () {
let image1 = UIImage(named:"a")
let url1 = URL(fileURLWithPath: Bundle.main.path(forResource: "a.png", ofType: nil)!)
let item1 = ActivityItem.init(image: image1!, path: url1)

let image2 = UIImage(named:"b")
let url2 = URL(fileURLWithPath: Bundle.main.path(forResource: "b.png", ofType: nil)!)
let item2 = ActivityItem.init(image: image2!, path: url2)

let image3 = UIImage(named:"c")
let url3 = URL(fileURLWithPath: Bundle.main.path(forResource: "c.png", ofType: nil)!)
let item3 = ActivityItem.init(image: image3!, path: url3)


let activityVC = UIActivityViewController.init(activityItems: [item1 ?? "3", item2 ?? "2", item3 ?? "1"], applicationActivities: nil)

activityVC.excludedActivityTypes = [.postToTencentWeibo, .airDrop, .openInIBooks, VimeoActivity().activityType!, FlickrActivity().activityType!, .addToReadingList, .saveToCameraRoll, .assignToContact, .copyToPasteboard, .print, .mail]

activityVC.completionWithItemsHandler = {
(type, flag, array, error) -> Swift.Void in
print("------------")
print(type ?? "")
}

####关于UIActivity类
UIActivity是一个抽象类,你可以通过打印某个分享平台的字符串创建该平台的UIActivity,该子类仅当需要屏蔽某个平台时使用。这种情况下也可以用下面的方法代替:

1
2
3
public struct UIActivityType : RawRepresentable, Equatable, Hashable, Comparable {
public init(rawValue: String)
}

子类化

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
class WeiBoActivity: UIActivity {

@available(iOS 7.0, *)
open override class var activityCategory: UIActivityCategory { return UIActivityCategory.share }

open override var activityType: UIActivityType? {
let postToWeibo = UIActivityType.init(rawValue: "com.apple.UIKit.activity.PostToWeibo")

return postToWeibo
}
}

class QQActivity: UIActivity {

@available(iOS 7.0, *)
open override class var activityCategory: UIActivityCategory { return UIActivityCategory.share }

open override var activityType: UIActivityType? {
let postToQQ = UIActivityType.init(rawValue: "com.tencent.mqq.ShareExtension")

return postToQQ
}
}

class WXActivity: UIActivity {

@available(iOS 7.0, *)
open override class var activityCategory: UIActivityCategory { return UIActivityCategory.action }

open override var activityType: UIActivityType? {
let postToWX = UIActivityType.init(rawValue: "com.tencent.xin.sharetimeline")

return postToWX
}
}

####支持类型参考
支持类型

参考链接