iOS9 系統分享調用(UIActivityViewController)

猴子原創,歡迎轉載。轉載請註明: 轉載自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)
            }
        }
    }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章