#30 天氣App|part 1 , CocoaPods管理第三方套件、GooglePlace Autocomplete、.gitIgnore

使用Cocoapods來管理這次要使用的第三方套件GooglePlace Autocomplete,並學習將重要的資料儲存為隱藏檔,使其不被git追蹤也不會上傳到github

✏️ 使用到的技術

  • Cocoapods
  • GooglePlace Autocomplete之API Key、程式碼建立
  • .gitIgnore

✏️ 程式說明

1.使用CocoaPods來輔助開發這次的天氣App
CocoaPods 網址:
https://cocoapods.org/

如何安裝可參考這一篇⬇️

安裝完成之後

  • 在%之後輸入 cd + 空白鍵 + 專案儲存路徑
  • 輸入 ls 列出該目錄中的文件,如下圖中 WeatherApp \ WeatherApp.xcodeproj
  • 輸入 pod + 空白鍵 + init → 建立Podfile在目錄中 → 將Podfile拖曳到Xcode,使用Xcode打開
  • 搜尋google 地圖平台,打開Places SDK for iOS
  • 點選設定xcode專案,編輯Podfile
  • 將應用程式目標名稱及 GooglePlaces pod 的名稱(上述範例中灰色框框內文字全選)複製貼上後,刪除source、target,留下pod ‘GooglePlaces’, ‘7.4.0’,並注意platform 的最低版本需求,如目前是ios, ‘13.0’
    → 儲存Podfile
  • 輸入pod + 空白鍵 + install,即開使安裝

2.使用Google Places SDK for iOS
取得API Key

  • 搜尋 google 地圖平台,打開Places SDK for iOS
  • 點選 設定 Google Cloud 專案,依序步驟建立專案
  • 使用gmail 登入google cloud,建立專案名稱與位置
  • 建立扣款帳戶,填寫地址、信用卡號等資訊,google cloud 提供90 天的免費試用期,並提供價值 $300 美元的抵免額,除非升級為 Cloud Billing 付費帳戶,否則這項驗證程序完成後並不會產生任何費用
  • 填寫google問題,如要選擇使用Place、選擇您的行業等,以建立API Key
  • 複製Bundle Identifier ,限制API Key只能用於指定的 iOS 應用程式

3.建立 .gitIgnore文件
使檔案不會被.git追蹤也不會上傳到gitHub

啟動終端機

  • 輸入cd + 空白鍵 + 專案儲存路徑
  • 輸入 ls 列出該目錄中的文件,也確認了檔案存在這個路徑中
  • 輸入 nano + 空白鍵 + .gitignore
    nano 是一個文字編輯器,建立一個.gitignore的文件
    出現以下畫面後,在中間空白處,輸入想被隱藏的文件名稱,注意名稱、拼字、大小寫等須完全相同!(這次隱藏的是API Key,屆時在xcode上也會生成這個檔案位置以儲存API Key)
  • 輸入完成後,control + x 離開此畫面 → save modified buffer 輸入Y → 會看到 file name to write = .gitignore → 按 enter → 需被隱藏的文件就被儲存完成囉!
  • 再一次輸入 ls ,看看目前路徑中的檔案列表,並沒有看到.gitignore的檔案, 使用shift + ⌘ + . = 打開隱藏檔案,就可以看到.gitignore的檔案囉~

檢查GitHub~ .gitignore設定成功!

4.GMSAutocompleteViewController 、GMSAutocompleteViewControllerDelegate

接續設定Xcode專案的步驟3,在AppDeledate.swift中新增API Key

點選Place Autocomplete

1.import GooglePlaces

2. 在新增地點的按鈕上,加入紅框內的程式碼,搜尋使用者輸入的所有訊息,在任一個城市街道上取得位置,故不做任何限制,就不需要過濾及回傳地點資料了

@IBAction func addLocationPressed(_ sender: UIBarButtonItem) {
let autocompleteController = GMSAutocompleteViewController()
autocompleteController.delegate = self

// Display the autocomplete view controller.
present(autocompleteController, animated: true, completion: nil)
}

3. 複製extension GMSAutocompleteViewControllerDelegate的功能,分別是didAutocompleteWithPlace、didFailAutocompleteWithError、wasCancelled

(1) didAutocompleteWithPlace 的place 是遵從GMSPlace class ref,可看到有多種屬性,如地名、經緯度、電話號碼、評分等

在此位置建立變數,儲存使用者搜尋後的地理位置,依照自定義的啟動方式,需有位置名稱與經緯度,這邊的name是optional,故提供沒有此地點的參數 “unknow place”

當使用者輸入完地點後,透過delegate將搜尋後的地點、經緯度顯示在table view上,因此,加上tableView.reloadData(),更新畫面中的所有地點

(2) 關閉 didAutocompleteWithPlacedidFailAutocompleteWithErrorwasCancelled 委派方法中的控制器。

extension LocationListViewController: GMSAutocompleteViewControllerDelegate {

// Handle the user's selection.
func viewController(_ viewController: GMSAutocompleteViewController, didAutocompleteWith place: GMSPlace) {

let newLocation = WeatherLocation(name: place.name ?? "unknow place", latitude: place.coordinate.latitude, longitude: place.coordinate.longitude)
weatherLocations.append(newLocation)
tableView.reloadData()
dismiss(animated: true, completion: nil)
}

func viewController(_ viewController: GMSAutocompleteViewController, didFailAutocompleteWithError error: Error) {
// TODO: handle the error.
print("Error: ", error.localizedDescription)
}

// User canceled the operation.
func wasCancelled(_ viewController: GMSAutocompleteViewController) {
dismiss(animated: true, completion: nil)
}

}

下一篇開始進入畫面及程式的部分囉!

--

--