2020年10月28日 星期三

swift 各國國家列表資料(NSLocale)

今天要來寫簡單的各國資料列表取得,顯示方式就看個人或公司需求為主了,今天只做列表資料的簡單說明。基本上我是用最簡單的方式去取得,取得方式排序等等有很多方法再由個人自行探討摟~

首先寫作一個model(如果你是練習懶得做就換方式做吧)

struct countryModel {

    var name:String

    var countryCode:String

    var currencyCode:Any?

    var currencySymbol:String

    

    

    

    init(name:String, countryCode:String, currencyCode:Any?, currencySymbol:String) {

        self.name = name

        self.countryCode = countryCode

        self.currencyCode = currencyCode

        self.currencySymbol = currencySymbol

    }

}

然後是取得資料,要寫在哪也是看個人摟~

 let countryCode = NSLocale.isoCountryCodes

        // 取得使用者 iOS 設定的語系

        let preferredLanguages:String = NSLocale.preferredLanguages.first!

        for code in countryCode  {

            let id = NSLocale.localeIdentifier(fromComponents: [NSLocale.Key.countryCode.rawValue: code])

            //帶入本機語系代碼取得該國國名

            let name = NSLocale(localeIdentifier: preferredLanguages).displayName(forKey: NSLocale.Key.identifier, value: id)

            

            let locale = NSLocale.init(localeIdentifier: id)

            //該國國名英文縮寫

            let countryCode:String = locale.object(forKey: NSLocale.Key.countryCode) as! String

            let currencyCode = locale.object(forKey: NSLocale.Key.currencyCode)

            //該國貨幣代碼或符號

            let currencySymbol = locale.object(forKey: NSLocale.Key.currencySymbol) as! String

            let countryM = countryModel(name: name!, countryCode: countryCode, currencyCode: currencyCode, currencySymbol: currencySymbol)

            country.append(countryM)

          }

顯示效果如下:(因我沒刻意做資料排序,取出直接就引用了,你們可以先做排序在使用)

英文:



中文:





 

2020年10月22日 星期四

Swift 當APP至背景系統自動截圖防止個資洩漏方法 ->xCode12

 今天要用xCode12來進行預防iOS本身自動截圖用於背景開啟未關閉App所顯示的預覽圖

方法其實最簡單的做法就是建立一個新的UIImage或是UIView看您個人需求或是公司需求為主,

我這邊分享一般最簡單的做法,就是直接用一個UIView設定背景色為紅色,然後在SceneDelegate內的

func sceneWillResignActive(_ scene: UIScene)

將您建立的UIView執行addView如下

hideView.frame = self.window!.rootViewController!.view.bounds

self.window?.rootViewController?.view.addSubview(hideView)

這樣當用戶將App退到背景時會先蓋上我們的UIView,此時系統自動截圖當前畫面會截圖到我們所新蓋上去的UIView

然後當用戶從背景回來時再將該view移除即可

 func sceneDidBecomeActive(_ scene: UIScene) {

        if (hideView.superview != nil) {

            hideView.removeFromSuperview()

        }

    }

這樣就能預防一些個資或特殊資訊有洩漏的問題~

大致上顯示結果如下:

1.到背景已截圖為我心覆蓋的紅色


2.多開其他APP做比較,確定我們的是有多蓋一層View


3.回到App內確定該View有被移除掉


以上基本上您可使用Image放入您需要的圖片作為截圖也是很棒的選擇,而且比較美觀唷!


swift 螢幕截圖警示語 -> xcode12

 又好久沒有跟大家分享一些小功能方法拉!

今天要跟大家分享的是用戶執行螢幕截圖時進行提醒,

例如有用戶個人基本資料畫面進行截圖,我們可能會提醒有個資外流疑慮等等...

基本上使用的方法是NotificationCenter內的原生name就可以執行了。

我們僅需寫自己想執行的selecotr動作

範例如下:

NotificationCenter.default.addObserver(self, selector: #selector(showSomeThing), name: UIApplication.userDidTakeScreenshotNotification, object: nil)

先執行觀察(監聽)動作加入,這方法可以加入在你要提醒的頁面viewdidload,記得在離開頁面時做釋放將其remove掉。

今天我是做在SceneDelegate內,主要是方便測試各個頁面等等...假如您覺得你大多數頁面都需要做這件事情就可以像我一樣寫在SceneDelegate ->  func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions)

如果你是舊有專案只有AppDelegate的話就直接加在

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool

接下來就是執行方法

@objc func showSomeThing() {

        let alertController = UIAlertController(title: "提醒:", message: "螢幕截圖可能會造成隱私資訊洩露", preferredStyle: .alert)

        let alertAction = UIAlertAction(title: "確定", style: .default, handler: nil)

        alertController.addAction(alertAction)

        self.window?.rootViewController?.present(alertController, animated: true, completion: nil)

    }

Selector的用法這邊我就不多做解說了,該寫哪就寫哪...


執行後的結果如下