2020年12月17日 星期四

TextField判斷是否輸入Emoji

 最近剛好遇到一個需求就是禁止在TextField內輸入Emoji的需求,

當Google後發現方式滿多的,可是都不是那麼的完美(包含原生Emoji判斷),

說不完美是因為可能需求在於地址,可是iPhone的User可能習慣連動打字,

不完美的原因在於如果使用編碼判斷的方式會造成輸入時只能一個字一個字輸入...

這樣對於使用者體驗個人是覺得很差的...

所以最後使用了原生的方法 請參考:官方文件

裡面總共有以下四個判斷Emoji的方法

而我一開始使用isEmoji發現他連聲符都擋掉了...可是他所以的Emoji都可以判斷出來

最後使用isEmojiPresentation,聲符沒擋了,但是部分的Emoji是可以輸入的...

所以上方才會提說原生判斷也不是那麼完美。

以下提供我所寫的方法

func containsEmoji() -> Bool {

        for scalar in unicodeScalars {

            if !scalar.properties.isEmojiPresentation {

                continue

            }

            return true

        }

        return false

    }

而我們會在text的delegate去判斷

func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {

        

        if textField == txtAddressDetail {

            if string.containsEmoji() {

                return false

            }

        }

}

當判斷是Emoji直接return false就不會進入輸入欄位了

另外附上正則式的寫法如下:

func hasEmoji() -> Bool {

        let pattern = "[^\\u0020-\\u007E\\u00A0-\\u00BE\\u2E80-\\uA4CF\\uF900-\\uFAFF\\uFE30-\\uFE4F\\uFF00-\\uFFEF\\u0080-\\u009F\\u2000-\\u201f\\u2026\\u2022\\u20ac\r\n]"

        let pred = NSPredicate(format: "SELF MATCHES %@",pattern)

        return pred.evaluate(with: self)

    }

基本上用法是一樣的,但是不完美的就是上方所提的輸入問題,只能一個字一個字輸入。

再來就是每年可能都會有新的Emoji,Unicode要每年都去更新...

以上跟大家方享我目前所用的方法跟想法,如果您有更好的方法可以完美的呈現歡迎交流唷^^

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的用法這邊我就不多做解說了,該寫哪就寫哪...


執行後的結果如下