iOS9 系統分享調用(UIActivityViewController)

昨天有網友說我寫的那段系統分享代碼在iOS9上有warning,看下了原來ios8以後UIPopoverController被廢棄了。新增長的UIPopoverPresentationController在控制PopView上更簡單好用。ios

這裏寫圖片描述

下面是我修改以後的代碼:swift

1. 在app內以子視圖方式打開其餘app預覽,僅支持6.0以上

openAppWithIdentifier(appId: String)app

2. 分享文字圖片信息,ipad上會以sourceView爲焦點彈出選擇視圖

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

/// 在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) } } }
相關文章
相關標籤/搜索