顯示具有 Swift 標籤的文章。 顯示所有文章
顯示具有 Swift 標籤的文章。 顯示所有文章

2020年3月3日 星期二

iOS Label text 自動改變大小 dynamic type




字體大小對照表



Label

1.

2.

3.
iOS 10
label.font = UIFont.preferredFont(forTextStyle: .body)
label.adjustsFontForContentSizeCategory = true

4.
自訂字體(iOS 11才有)
let customFont = UIFont.systemFont(ofSize: 20)
label.font = UIFontMetrics.default.scaledFont(for: customFont)
label.adjustsFontForContentSizeCategory =
true



Xcode 快速驗證 


取得現在字體大小
UIApplication.shared.preferredContentSizeCategory

修改字體大小的Notification
UIContentSizeCategory.didChangeNotification


2018年9月4日 星期二

iOS Security Code AutoFill 驗證碼自動填入


驗證碼自動填入(僅App部分):
l   原文:Security Code AutoFill

l   iOS 12的新功能

l   只適用內建鍵盤

l   UITextField需設定textContentType為oneTimeCode

l   除了自動帶入外,還有偵測複製功能。
簡訊關鍵字有Code 或Passcode,就會將驗證碼加上底線,messages內容舉例:
點擊有底線的部分,可以進行複製。


2018年3月14日 星期三

iOS app 中實作 Siri

前置作業


使用範圍


實作過程:





新增target


我在做到這裡的時候遇到了問題,Siri會跟我說我的app還沒設定好,但是同樣的app在別台ipad可以正常執行。推測是作業系統版本問題。

如果Siri一直說未設定完成,可以換一台ipad試試。

進入debug mode

案例舉例


新增情境








2017年10月30日 星期一


2017年10月17日 星期二

iOS 串接 Firebase 的 Database

Firebase 官方網站:https://firebase.google.com/
要實作分為兩個部分:
第一部分:為專案加入 Firebase
第二部分:實作 Database 基礎功能

第一部分:

  1. 先創專案
  2. 為專案安裝 Firebase,並在專案中實現

  • 官方講解如何安裝 Firebase,示範CocoaPods步驟如下 
    • CocoaPods
    • SDK
1.開啟終端機

2.進入你的專案位置
 cd 你的專案位置 
cd 是進入後面位置的指令
ls 看現在的資料夾底下有什麼(就 ls ,後面不需接其他字)

*可以從 Finder 到專案底下,對檔案按右鍵 -> 取得資訊
把位置直接複製貼上終端機

終端機進入資料夾後:

3.創建 Podfile
已經有就不用創,創過再創一次也不會怎樣
 pod init 
4. Podfile 這時候會出現在你的專案底下,把下面這行加入你的 Podfile
 pod 'Firebase/Core' 
 我自己多加了 Database 進來,但我沒有試過沒打會怎樣~

5.最後要安裝你的 Podfile裡面的東西
 pod install 
這時 Firebase 就在你的專案裡了
要開專案要點 . xcworkspace
不是上面的 .xcodeproj


要在專案中實現:
在 AppDelegate.swift 中實現

 import Firebase 

在application:didFinishLaunchingWithOptions:函式底下

 FirebaseApp.configure() 


第二部分:

Firebase的設定:
!記得要把 Firebase 創出的 GoogleService-Info.plist 放到專案裡

權限預設:
不需登入就開放讀寫權限:

我範例的 JSON 格式:


Get a FIRDatabaseReference

var ref: DatabaseReference!
ref = Database.database().reference()


 修改 

修改時會先判斷現在資料庫有沒有這個 key 的值,
沒有的話會變成新增,
所以修改可以當新增用。

self.ref.child("users/(user.uid)/username").setValue(username)
更新內容:

let key = ref.child("posts").childByAutoId().key
let post = ["uid": userID,
            "author": username,
            "title": title,
            "body": body]
let childUpdates = ["/posts/\(key)": post,
                    "/user-posts/\(userID)/\(key)/": post]
ref.updateChildValues(childUpdates)

 新增 

self.ref.child("users").child(user.uid).setValue(["username": username])
可以把不需要的拿掉,驗證身份、child...等

會把原本其他的全部蓋掉

 查詢 

let userID = Auth.auth().currentUser?.uidref.child("users").child(userID!).observeSingleEvent(of: .value, with: { (snapshot) in
  // Get user value
  let value = snapshot.value as? NSDictionary
  let username = value?["username"] as? String ?? ""
  let user = User.init(username: username)

  // ...
  }) { (error) in
    print(error.localizedDescription)
}
取出的是 snapshot

監聽 Database 是否有異動:
// Listen for new comments in the Firebase database
commentsRef.observe(.childAdded, with: { (snapshot) -> Void in
  self.comments.append(snapshot)
  self.tableView.insertRows(at: [IndexPath(row: self.comments.count-1, section: self.kSectionComments)], with: UITableViewRowAnimation.automatic)
})
異動分類:

 刪除 

把值改成 nil 或是 remove 都可以刪除。


 DEMO 

我的範例專案:Github