存档

2015年11月 的存档

iOS9 系统分享调用(UIActivityViewController)

2015年11月3日 没有评论

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

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

iOS9 系统分享调用(UIActivityViewController)

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

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)
            }
        }
    }

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

分类: 未分类 标签: