UIActivityViewController可以用于一些标准服务,例如国内的微博分享,发送邮件,发送短信,保存图片等。
UIActivityViewController类中只有三个方法
init(activityItems: [AnyObject], applicationActivities: [AnyObject]?) //初始化方法<pre name="code" class="objc">
var completionHandler: UIActivityViewControllerCompletionHandler? //结束后执行的block
var excludedActivityTypes: [AnyObject]?//排除的Activity
利用默认的UIActivity去对一个图片,一个URL,一个字符串进行操作。
//屏幕截图得到一个图片
UIGraphicsBeginImageContext(self.view.bounds.size)
self.view.layer.renderInContext(UIGraphicsGetCurrentContext())
let image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
//一个字符串
let shareString = "share text"
//一个URL
let shareURL = NSURL(string: "http://www.baidu.com")
//初始化一个UIActivity
var activity = UIActivity()
let activityItems = [image, shareString, shareURL]
let activities = [activity]
//初始化UIActivityViewController
let activityController = UIActivityViewController(activityItems: activityItems, applicationActivities: activities)
//排除一些服务:例如复制到粘贴板,拷贝到通讯录
activityController.excludedActivityTypes = [UIActivityTypeCopyToPasteboard,UIActivityTypeAssignToContact]
//iphone中为模式跳转
self.presentViewController(activityController, animated: true) { () -> Void in
}
//结束后执行的Block,可以查看是那个类型执行,和是否已经完成
var activityType:String?
var error:NSError?
activityController.completionHandler = { activityType, error in
println("\(activityType)")
println("\(error.description)")
}
利用自定义的UIActivity在百度搜歌手的歌曲---例如搜Beyond
首先需要自定义UIActivity,并重写方法
class MuSicSearchActivity: UIActivity {
var searchStr:NSString = ""
//定义这个Activity的类型,有Action和Share,默认是Action。
override class func activityCategory() -> UIActivityCategory {
return .Action
}
//这个是显示的标题,默认返回nil
override func activityTitle() -> String? {
return "搜索歌手"
}
//这个是显示的图片,默认返回nil
override func activityImage() -> UIImage? {
return UIImage(named: "search")
}
//是否能够执行相应的类型,此处如果是String就能执行
override func canPerformWithActivityItems(activityItems: [AnyObject]) -> Bool {
let count:Int = activityItems.count
for index in 0...count-1 {
var object: AnyObject = activityItems[index]
if object.isKindOfClass(NSString.self) {
return true
}
}
return false
}
//执行操作的准备工作
override func prepareWithActivityItems(activityItems: [AnyObject]) {
let count:Int = activityItems.count
for index in 0...count-1 {
var object: AnyObject = activityItems[index]
if object.isKindOfClass(NSString.self) {
searchStr = object as NSString
return;
}
}
}
//在新的Controllerz中显示
override func activityViewController() -> UIViewController? {
let url = NSURL(string:"http://music.baidu.com/search?key=\(searchStr)")
let request = NSURLRequest(URL: url!)
let storyboard = UIStoryboard(name:"Main", bundle:nil)
let webView = storyboard.instantiateViewControllerWithIdentifier("WebViewController") as WebViewController
webView.request = request
println("webViewController = \(webView)")
return webView
}
//完成后调用这个方法把ActivityController去掉
func back() {
activityDidFinish(true)
}
}
初始化ActivityController
//一个字符串
let shareString = "Beyond"
let activityItems:Array = [shareString]
var activity = MuSicSearchActivity()
let activities = [activity]
//初始化UIActivityViewController
let activityController = UIActivityViewController(activityItems: activityItems, applicationActivities: activities)
//排除其他服务
activityController.excludedActivityTypes = [UIActivityTypeCopyToPasteboard,UIActivityTypeAssignToContact,UIActivityTypeMail]
//iphone中为模式跳转
self.presentViewController(activityController, animated: true) { () -> Void in
}
点击后的结果: