效果图如下:
--- ViewController.swift ---
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
|
import
UIKit
class
ViewController
:
UIViewController
{
override
func
viewDidLoad() {
super
.viewDidLoad()
}
didReceiveMemoryWarning() {
.didReceiveMemoryWarning()
}
//点击按钮弹出分享框
@IBAction
btnClicked(sender:
AnyObject
) {
let
items = [
"航歌"
,
UIImage
(named:
"toSendImage.png"
)!,
acts = [
WeiXinActivity
(),147)!important">HanggeActivity
()]
actView:
UIActivityViewController
=
(activityItems: items,applicationActivities: acts)
actView.excludedActivityTypes = [
UIActivityTypeMail
UIActivityTypeCopyToPasteboard
UIActivityTypePrint
UIActivityTypeAssignToContact
UIActivityTypeSaveToCameraRoll
]
self
.presentViewController(actView,animated:
true
nil
)
}
}
|
--- HanggeActivity.swift ---
UIActivity
//用于保存传递过来的要分享的数据
var
text:
String
!
url:
!
image:
!
activityTitle() ->
? {
return
"航歌"
}
activityImage() ->
? {
return
"swift.png"
)
}
//分享类型,在UIActivityViewController.completionHandler回调里可以用于判断,一般取当前类名
activityType() ->
? {
.
.description()
}
class
activityCategory() ->
UIActivityCategory
{
.
Action
}
canPerformWithActivityItems(activityItems: [
]) ->
Bool
{
for
item
in
activityItems {
if
is
{
return
true
}
is
{
true
}
{
true
}
}
false
}
prepareWithActivityItems(activityItems: [
]) {
print
(
"prepareWithActivityItems"
)
activityItems {
{
image = item
as
!
UIImage
}
{
text = item
!
String
}
{
url = item
NSURL
}
}
}
//执行分享行为
//这里根据自己的应用做相应的处理
performActivity() {
"performActivity"
)
//具体的执行代码这边先省略
}
activityViewController() ->
? {
"activityViewController"
)
nil
}
activityDidFinish(completed:
) {
"activitydidfinish"
)
}
--- WeiXinActivity.swift ---
|