Open AppStore through button

前端 未结 15 815
逝去的感伤
逝去的感伤 2020-12-12 22:18

Could you guys help me to translate the following code into Swift?

[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@\"itms://itunes.apple.com         


        
15条回答
  •  一个人的身影
    2020-12-12 22:26

    Swift 3 Syntax and improved with an 'if let'

    if let url = URL(string: "itms-apps://itunes.apple.com/app/id1024941703"),
    UIApplication.shared.canOpenURL(url){
        UIApplication.shared.openURL(url)
    }
    

    UPDATE 7/5/17 (Thank you Oscar for pointing this out):

    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)
        }
    }
    

提交回复
热议问题