swift 3/4 筆記/備忘

2017102022:20



以  Safari 瀏覽器開啟一個 URL:
guard let url = URL(string: "http://mini.nidbox.com") else {
 return
}

if #available(iOS 10.0, *) {
  UIApplication.shared.open(url, options: [:], completionHandler: nil)

  //If you want handle the completion block than:
  //UIApplication.shared.open(url, options: [:], completionHandler: { 
  //  (success) in print("Open url: \(success)") })

} else { //iOS 10.0之前可這樣:
  UIApplication.shared.openURL(url)
}

 

另外,UIApplication.shared.open(url, options: [:], completionHandler: nil)  也可用來開啟其他的 App
例如開啟 FB / twitter app

參考資料:
Querying URL Schemes with canOpenURL
https://useyourloaf.com/blog/querying-url-schemes-with-canopenurl/


How to open an iOS app with custom URL
https://kitefaster.com/2016/07/13/how-to-open-ios-app-with-custom-url/
   This tutorial shows you how to open another iOS app from within your iOS app or from the safari browser using a custom URL scheme.



如何轉跳 APP Store 評分畫面
let appID = "443904275"  //這是Line ID
let rateURL = "itms-apps:itunes.apple.com/us/app/apple-store/id\(appID)?mt=8&action=write-review"

guard let url = URL(string: rateURL) else {
 return
}

if #available(iOS 10.0, *) {
  UIApplication.shared.open(url, options: [:], completionHandler: nil)

  //If you want handle the completion block than:
  //UIApplication.shared.open(url, options: [:], completionHandler: { 
  //  (success) in print("Open url: \(success)") })

} else { //iOS 10.0之前可這樣:
  UIApplication.shared.openURL(url)
}

 ref: https://stackoverflow.com/questions/27755069/how-can-i-add-a-link-for-a-rate-button-with-swift





Swift - URL schemes的使用样例(如:在Safari中打开App)
http://www.hangge.com/blog/cache/detail_1042.html
  我们可以将应用“绑定”到一个自定义 URL scheme 上,该 scheme 用于从浏览器或其他应用中启动本应用,同时跳转时也可以传递参数。
  比如,可以在网页上添加一个链接,点击这个链接后会自动打开对应的APP上。
  或者从一个APP跳转到另一个APP 


『Swift』從自己的ios APP跳轉到系統設定的方法
http://andy02172001.blogspot.tw/2017/07/swiftios-app.html
  當想要從自己的app跳至系統設定,如設定wifi或設定藍芽等等地方



讓圖片變暗
 
  //圖片上方放個暗色view 讓圖片變暗				
  let overlay: UIView = UIView(frame: CGRect(x: 0, y: 0, width: img1.frame.size.width, height: img1.frame.size.height))
  overlay.backgroundColor = UIColor(red: 0/255, green: 0/255, blue: 0/255, alpha: 0.4)

  img1.addSubview(overlay)




How to get UIScrollView vertical direction in Swift?

6 Solutions Collect From Internet About “How to get UIScrollView vertical direction in Swift?”