UIActivityViewController的使用(Swift实现)

8 篇文章 0 订阅

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
        }


点击后的结果:




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值