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

iOS9 系统分享调用(UIActivityViewController)

时间:2015-11-09 17:18:57      阅读:268      评论:0      收藏:0      [点我收藏+]

标签:

昨天有网友说我写的那段系统分享代码在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)

标签:

原文地址:http://www.cnblogs.com/crash-wu/p/4950382.html

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