码迷,mamicode.com
首页 > 移动开发 > 详细

iOS9 系统分享调用(UIActivityViewController)

时间:2015-11-03 14:56:50      阅读:276      评论:0      收藏:0      [点我收藏+]

标签:controller   popover   presentati   系统分享   

猴子原创,欢迎转载。转载请注明: 转载自Cocos2Der-CSDN,谢谢!
原文地址: http://blog.csdn.net/cocos2der/article/details/49615109

昨天有网友说我写的那段系统分享代码在iOS9上有warning,看下了原来ios8之后UIPopoverController被废弃了。新增加的UIPopoverPresentationController在控制PopView上更简单好用。

技术分享

下面是我修改之后的代码:

1. 在app内以子视图方式打开其他app预览,仅支持6.0以上

openAppWithIdentifier(appId: String)

2. 分享文字图片信息,ipad上会以sourceView为焦点弹出选择视图

share(textToShare: String, url: String, image: UIImage, sourceView: UIView)

/// 在app内以子视图方式打开其他app预览,仅支持6.0以上
    private func openAppWithIdentifier(appId: String) {
        if let _ = NSClassFromString("SKStoreProductViewController") {
            let storeProductViewController = SKStoreProductViewController()
            storeProductViewController.delegate = self
            let dict = NSDictionary(object:appId, forKey:SKStoreProductParameterITunesItemIdentifier) as! [String : AnyObject]
            storeProductViewController.loadProductWithParameters(dict, completionBlock: { (result, error) -> Void in
//                self.presentViewController(storeProductViewController, animated: true, completion: nil)
            })
            self.presentViewController(storeProductViewController, animated: true, completion: nil)
        }else {
            UIApplication.sharedApplication().openURL(NSURL(string: "itms-apps://itunes.apple.com/app/id\(appId)")!)
        }
    }

    /// 分享文字图片信息,ipad上会以sourceView为焦点弹出选择视图
    private func share(textToShare: String, url: String, image: UIImage, sourceView: UIView) {
        let objectsToShare = [textToShare, url, image]
        let activityViewController = UIActivityViewController(activityItems: objectsToShare, applicationActivities: nil)
        if UIDevice.currentDevice().userInterfaceIdiom == UIUserInterfaceIdiom.Phone {
            self.presentViewController(activityViewController, animated: true, completion: nil)
        }else {
            let popover = activityViewController.popoverPresentationController
            if (popover != nil){
                popover?.sourceView = sourceView
                popover?.sourceRect = sourceView.frame
                popover?.permittedArrowDirections = UIPopoverArrowDirection.Any
                self.presentViewController(activityViewController, animated: true, completion: nil)
            }
        }
    }

版权声明:本文为博主原创文章,未经博主允许不得转载。

iOS9 系统分享调用(UIActivityViewController)

标签:controller   popover   presentati   系统分享   

原文地址:http://blog.csdn.net/cocos2der/article/details/49615109

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!