Open AppStore through button

前端 未结 15 775
逝去的感伤
逝去的感伤 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:36

    In Swift 4.2 and Xcode 10.2

    You have two ways to open App Store or iTunes Store

    If you want to open App Store use https or if you want to open iTunes Store use itms

    Ex: https://itunes.apple.com/in/app/yourAppName/id14****4?mt=8 //For App Store

    itms://itunes.apple.com/in/app/yourAppName/id14****4?mt=8 //For iTunes Store

    Method 1: This is simple direct and old approach

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

    Method 2: New approach

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

提交回复
热议问题