昨天有網友說我寫的那段系統分享代碼在iOS9上有warning,看下瞭原來ios8之後UIPopoverController被廢棄瞭。新增加的UIPopoverPresentationController在控制PopView上更簡單好用。
下面是我修改之後的代碼:vcD4KPGg0IGlkPQ==”1-在app內以子視圖方式打開其他app預覽僅支持60以上”>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)
}
}
}