你们能帮我把以下代码翻译成Swift吗?
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"itms://itunes.apple.com/de/app/x-gift/id839686104?mt=8&uo=4"]];
(还是我必须使用此链接:itms://itunes.apple.com/app/id839686104?)
你们能帮我把以下代码翻译成Swift吗?
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"itms://itunes.apple.com/de/app/x-gift/id839686104?mt=8&uo=4"]];
(还是我必须使用此链接:itms://itunes.apple.com/app/id839686104?)
这里。但我强烈建议你学习Swift的基础知识!
UIApplication.sharedApplication().openURL(NSURL(string: "itms://itunes.apple.com/de/app/x-gift/id839686104?mt=8&uo=4")!)
如果你想在Swift 5中打开AppStore:
if let url = URL(string: "itms-apps://itunes.apple.com/app/id1629135515") {
UIApplication.shared.open(url)
}
x-gift
参考? - Katherine Jenkinsitms-apps://
)。 - gabuchanSwift 3语法并使用'if let'进行改进
if let url = URL(string: "itms-apps://itunes.apple.com/app/id1024941703"),
UIApplication.shared.canOpenURL(url){
UIApplication.shared.openURL(url)
}
更新于7/5/17(感谢Oscar指出):
if let url = URL(string: "itms-apps://itunes.apple.com/app/id1024941703"),
UIApplication.shared.canOpenURL(url)
{
if #available(iOS 10.0, *) {
UIApplication.shared.open(url, options: [:], completionHandler: nil)
} else {
UIApplication.shared.openURL(url)
}
}
{action=write-review}
添加到 URL 字符串中,以直接跳转到评分和撰写评论页面... - Ahmed Khedr我使用这个组合,它更适合评级/购物。
(部分内容来自此处)
@IBAction func rateMe(sender: AnyObject) {
if #available(iOS 8.0, *) {
openStoreProductWithiTunesItemIdentifier("107698237252");
} else {
var url = NSURL(string: "itms://itunes.apple.com/us/app/xxxxxxxxxxx/id107698237252?ls=1&mt=8")
if UIApplication.sharedApplication().canOpenURL(url!) == true {
UIApplication.sharedApplication().openURL(url!)
}
}
}
func openStoreProductWithiTunesItemIdentifier(identifier: String) {
let storeViewController = SKStoreProductViewController()
storeViewController.delegate = self
let parameters = [ SKStoreProductParameterITunesItemIdentifier : identifier]
storeViewController.loadProductWithParameters(parameters) { [weak self] (loaded, error) -> Void in
if loaded {
// Parent class of self is UIViewContorller
self?.presentViewController(storeViewController, animated: true, completion: nil)
}
}
}
func productViewControllerDidFinish(viewController: SKStoreProductViewController) {
viewController.dismissViewControllerAnimated(true, completion: nil)
}
不要忘记导入和委托:
不要忘记导入和委托:
import StoreKit
class RateMeViewController: UIViewController, SKStoreProductViewControllerDelegate {
使用完成处理程序的Swift 4:
请确保在appStoreUrlPath中更新您的id
func openAppStore() {
if let url = URL(string: "itms-apps://itunes.apple.com/app/id..."),
UIApplication.shared.canOpenURL(url){
UIApplication.shared.open(url, options: [:]) { (opened) in
if(opened){
print("App Store Opened")
}
}
} else {
print("Can't Open URL on Simulator")
}
}
self.track()
是做什么用的? - Dmytro Rostopira用于Swift 5(已测试代码)打开App Store链接
if let url = URL(string: "https://itunes.apple.com/in/app/your-appName/id123456?mt=8")
{
if #available(iOS 10.0, *) {
UIApplication.shared.open(url, options: [:], completionHandler: nil)
}
else {
if UIApplication.shared.canOpenURL(url as URL) {
UIApplication.shared.openURL(url as URL)
}
}
}
其他答案对我没有用(Swift,Xcode 6.1.1),所以在这里发布我的解决方案:
var url = NSURL(string: "itms://itunes.apple.com/de/app/x-gift/id839686104?mt=8&uo=4")
if UIApplication.sharedApplication().canOpenURL(url!) {
UIApplication.sharedApplication().openURL(url!)
}
Swift 5.0:
导入 StoreKit 模块
extension YourViewController: SKStoreProductViewControllerDelegate {
func openStoreProductWithiTunesItemIdentifier(_ identifier: String) {
let storeViewController = SKStoreProductViewController()
storeViewController.delegate = self
let parameters = [ SKStoreProductParameterITunesItemIdentifier : identifier]
storeViewController.loadProduct(withParameters: parameters) { [weak self] (loaded, error) -> Void in
if loaded {
self?.present(storeViewController, animated: true, completion: nil)
}
}
}
private func productViewControllerDidFinish(viewController: SKStoreProductViewController) {
viewController.dismiss(animated: true, completion: nil)
}
}
// How to use
openStoreProductWithiTunesItemIdentifier("12345")
let currentAppVersion = Bundle.main.infoDictionary
Alamofire.request("http://itunes.apple.com/jp/lookup/?id=548615", method: .get, parameters: nil, headers: nil).responseJSON { response in
if let value = response.result.value as? [String: AnyObject] {
let versionNum = value["results"]?.value(forKey: "version") as? NSArray
if versionNum?[0] as! String != currentAppVersion?["CFBundleShortVersionString"] as! String {
self.alertForUpdateApp()
}
}
}
func alertForUpdateApp() {
let alertController = UIAlertController(title: "Update Available", message: "There is a newer version of this app available", preferredStyle: .alert)
let alertActionCancel = UIAlertAction(title: "Cancel", style: .cancel, handler: nil)
let alertActionUpdate = UIAlertAction(title: "Update", style: .default, handler: { _ in
if let url = URL(string: Constants.API_REDIRECT_TO_ITUNES),
UIApplication.shared.canOpenURL(url){
UIApplication.shared.open(url, options: [:], completionHandler: nil)
}
})
alertController.addAction(alertActionCancel)
alertController.addAction(alertActionUpdate)
let pushedViewControllers = (self.window?.rootViewController as! UINavigationController).viewControllers
let presentedViewController = pushedViewControllers[pushedViewControllers.count - 1]
presentedViewController.present(alertController, animated: true, completion: nil)
}
URLCache.shared.removeAllCachedResponses()
。 - Jannemanlet url = NSURL(string: "https://itunes.apple.com/in/app/smsdaddy/id1450172544?mt=8")//itms https
if UIApplication.shared.canOpenURL(url! as URL) {
UIApplication.shared.openURL(url! as URL)
}
方法二:新的方法
if let url = URL(string: "https://itunes.apple.com/in/app/smsdaddy/id1450172544?ls=1&mt=8") {
if #available(iOS 10.0, *) {
UIApplication.shared.open(url, options: [:], completionHandler: nil)
} else {
// Earlier versions
if UIApplication.shared.canOpenURL(url as URL) {
UIApplication.shared.openURL(url as URL)
}
}
}
这里提供的大部分解决方案都已过时和废弃,例如“canOpenURL”、“openURL”等。
let appStoreLink = "https://apps.apple.com/app/{app-name}/{app-id}"
guard let url = URL(string: appStoreLink) else { return }
UIApplication.shared.open(url)