狠狠撸

狠狠撸Share a Scribd company logo
Cocoapods
第三方庫資源相依性管理工具
Cocoapods
? 一個負責管理iOS專案中第三方開源程式碼的工具。
? 透過Cocoapods可以使我們節省設置還更新第三方Library
的時間。
不使用Cocoapods
? 當我們要使用第三方Library時,要個別下載並加入Xcode
之中。
遇到的問題 :
會與原本的版本控管失去連結,未來如果想要更新
Library,每次都要重新下載才能更新。
如何安裝?
? 開啟終端機
? 輸入指令:
sudo gem install cocoapods
? 輸入使用者密碼 (* 終端機上並不會顯示輸入的密碼)
安裝完成後顯示的訊息
在Xcode專案應用Cocoapods
(*這邊以Google Maps SDK做說明)
? 新增一個Xcode專案
? 產生Podfile文件(*打開終端機輸入以下指令)
cd <path to project>
pod init
? 編輯Podfile文件
pod ‘GoogleMaps’
Podfile文件
Project Name
Project Name
在Xcode專案應用Cocoapods(續)
? 載入Podfile
cd <path to project>
pod install
? 完成後,資料夾內會有一個副檔名為’xcworkspace’的檔
案,這裡面包含了剛剛在Podfile新增的第三方Library。
使用Google Maps
? 產生’API key’
1. 到這個網址登入google帳(https://goo.gl/MncrN0)
2. 建立憑證
使用Google Maps(續)
? 產生’API key’
? 點進去後可以做一下其他設定
產生的API key
可以對這組API key做一些其他限制
使用Google Maps(續)
? 將API key新增到應用程式中
? 將API key新增到’AppDelegate.swift’中
1. 新增下列重要陳述式
import GoogleMaps
2. 新增下列內容到 application(_:didFinishLaunchingWithOptions:)方法,將 取
代為自己的API key
GMSServices.provideAPIKey(“YOUR_API_KEY”)
使用Google Maps(續)
? 新增地圖
import UIKit
import GoogleMaps
class ViewController: UIViewController {
override func viewDidLoad() {
// Create a GMSCameraPosition that tells the map to display the
// coordinate -33.86,151.20 at zoom level 6.
let camera = GMSCameraPosition.camera(withLatitude: -33.86, longitude: 151.20, zoom: 6.0)
let mapView = GMSMapView.map(withFrame: CGRect.zero, camera: camera)
mapView.isMyLocationEnabled = true //自己的位置
mapView.settings.compassButton = true //指南針
mapView.settings.myLocationButton = true //myLocation按鈕
self.view = mapView
// Creates a marker in the center of the map.
let marker = GMSMarker()
marker.position = CLLocationCoordinate2D(latitude: -33.86, longitude: 151.20)
marker.title = "Sydney"
marker.snippet = "Australia"
marker.map = mapView
}
}
使用Google Maps(續)
補充:
如果開啟應用後地圖一直無法顯示可能是API管理員資訊主
頁中,並沒有啟用”Google Maps SDK for iOS”,請到資訊主
頁中案啟用。
開啟定位
? 預設的應用程式並沒有定位功能,所以必須先將函示庫
引入
1. Build Phases -> Link Binary with Libraries -> Add CoreLocation.framework
import CoreLocation
開啟定位(續)
? 設置
//建立獲得定位資訊的變數
let locationManger = CLLocationManager()
//設置委任對象
locationManger.delegate = self
//設置移動多遠才更新位置
locationManger.distanceFilter =
//取得自身定位位置的精確度
locationManger.desiredAccuracy =
//精確度最高,適用於導航的定位
kCLLocationAccuracyBestForNavigation
//精確度高
kCLLocationAccuracyBest
//精確度 10 公尺以內
kCLLocationAccuracyNearestTenMeters
//精確度 100 公尺以內
kCLLocationAccuracyHundredMeters
//精確度 1 公里以內
kCLLocationAccuracyKilometer
//精確度 3 公里以內
kCLLocationAccuracyThreeKilometers
開啟定位(續)
? 加入協定
1. 為委任對象(ViewController)加入所需要的協定
CLLocationManagerDelegate
2. 實作
func locationManager(_ manager: CLLocationManager,
didUpdateLocations locations: [CLLocation])
{
let userLocation = locations[0]
}
開啟定位(續)
? 定位權限
? 將詢問授權的動作寫在viewDidAppear()
中,進到這個頁面就確認一次,避免再
多頁面的應用中,使用者把權限關閉。
? 使用.authorizationStatus()來確認目前授
權狀態為何
//首次使用,向使用者詢問定位自身位置權限
if CLLocationManager.authorizationStatus() == .NotDetermined {
myLocationManager.requestWhenInUseAuthorization()
myLocationManager.startUpdatingLocation()
}
//使用者已經拒絕定位自身位置權限
else if CLLocationManager.authorizationStatus() == .Denied {
// 跳出提示視窗
let alertController = UIAlertController(title: “定位權限已關
message: “如要變更權限,請至 設定 開啟”, preferredStyle:
UIAlertControllerStyle.alert)
let alertAction = UIAlertAction(title: “OK”, style:
UIAlertActionStyle.default, handler:nil)
alertController.addAction(alertAction)
present(alertController, animated: true, completion: nil)
}
// 使用者已經同意定位自身位置權限
else if CLLocationManager.authorizationStatus() == .AuthorizedWhenInU
myLocationManager.startUpdatingLocation()
}
.NotDerermined:首次使用,向使用者詢問定位自身位置權限
.Restricted:此應用未被授權使用位置服務(使用者無法更改)
.Denied:使用者拒絕,或設置中目前狀態為禁用
.AuthorizedAlways
.AuthorizedWhenInUse
開啟定位(續)
? 增加定位服務規則
? 除了向使用者詢問權限外,還要再Info.plist檔案中加入一個值
開啟定位(續)
補充:
可以授權的定位功能依照定位時機不同有兩種模式,分別
為開啟應用程式時定位(WhenInUse)及永遠定位(Always)。
前面範例都是使用WhenInUse的權限,如果要改成Always,
請注意下述部分:
? 詢問授權的方法改成requestAlwaysAuthorization()。
? 授權狀態會變為.AuthorizedAlways。
? 定位服务规则填入的值改成尝辞肠补迟颈辞苍础濒飞补测蝉鲍蝉补驳别顿别蝉肠谤颈辫迟颈辞苍。
參考資料
? http://www.appcoda.com.tw/cocoapods/
? https://goo.gl/qfZZCn
? https://goo.gl/xDy89r
Ad

Recommended

Docker workshop
Docker workshop
Wei Tung
?
Docker - 30秒生出100台伺服器
Docker - 30秒生出100台伺服器
升煌 黃
?
Docker tutorial
Docker tutorial
azole Lai
?
Docker集群管理 工具篇
Docker集群管理 工具篇
Guangya Liu
?
Docker open stack
Docker open stack
Guangya Liu
?
cec-hello-docker
cec-hello-docker
Bruce Huang
?
CocoaPods 使用教學
CocoaPods 使用教學
ShengWen Chiou
?
原型
原型
qbb225827455
?
2024 Trend Updates: What Really Works In SEO & Content Marketing
2024 Trend Updates: What Really Works In SEO & Content Marketing
Search Engine Journal
?
Storytelling For The Web: Integrate Storytelling in your Design Process
Storytelling For The Web: Integrate Storytelling in your Design Process
Chiara Aliotta
?
Artificial Intelligence, Data and Competition – SCHREPEL – June 2024 OECD dis...
Artificial Intelligence, Data and Competition – SCHREPEL – June 2024 OECD dis...
OECD Directorate for Financial and Enterprise Affairs
?
How to Leverage AI to Boost Employee Wellness - Lydia Di Francesco - SocialHR...
How to Leverage AI to Boost Employee Wellness - Lydia Di Francesco - SocialHR...
SocialHRCamp
?
2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot
Marius Sescu
?
Everything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPT
Expeed Software
?
Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage Engineerings
Pixeldarts
?
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental Health
ThinkNow
?
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
marketingartwork
?
Skeleton Culture Code
Skeleton Culture Code
Skeleton Technologies
?
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024
Neil Kimberley
?
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
contently
?
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
Albert Qian
?
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
Kurio // The Social Media Age(ncy)
?
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
Search Engine Journal
?
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
SpeakerHub
?
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
Clark Boyd
?
Getting into the tech field. what next
Getting into the tech field. what next
Tessa Mero
?
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Lily Ray
?
How to have difficult conversations
How to have difficult conversations
Rajiv Jayarajah, MAppComm, ACC
?

More Related Content

Featured (20)

2024 Trend Updates: What Really Works In SEO & Content Marketing
2024 Trend Updates: What Really Works In SEO & Content Marketing
Search Engine Journal
?
Storytelling For The Web: Integrate Storytelling in your Design Process
Storytelling For The Web: Integrate Storytelling in your Design Process
Chiara Aliotta
?
Artificial Intelligence, Data and Competition – SCHREPEL – June 2024 OECD dis...
Artificial Intelligence, Data and Competition – SCHREPEL – June 2024 OECD dis...
OECD Directorate for Financial and Enterprise Affairs
?
How to Leverage AI to Boost Employee Wellness - Lydia Di Francesco - SocialHR...
How to Leverage AI to Boost Employee Wellness - Lydia Di Francesco - SocialHR...
SocialHRCamp
?
2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot
Marius Sescu
?
Everything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPT
Expeed Software
?
Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage Engineerings
Pixeldarts
?
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental Health
ThinkNow
?
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
marketingartwork
?
Skeleton Culture Code
Skeleton Culture Code
Skeleton Technologies
?
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024
Neil Kimberley
?
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
contently
?
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
Albert Qian
?
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
Kurio // The Social Media Age(ncy)
?
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
Search Engine Journal
?
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
SpeakerHub
?
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
Clark Boyd
?
Getting into the tech field. what next
Getting into the tech field. what next
Tessa Mero
?
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Lily Ray
?
How to have difficult conversations
How to have difficult conversations
Rajiv Jayarajah, MAppComm, ACC
?
2024 Trend Updates: What Really Works In SEO & Content Marketing
2024 Trend Updates: What Really Works In SEO & Content Marketing
Search Engine Journal
?
Storytelling For The Web: Integrate Storytelling in your Design Process
Storytelling For The Web: Integrate Storytelling in your Design Process
Chiara Aliotta
?
How to Leverage AI to Boost Employee Wellness - Lydia Di Francesco - SocialHR...
How to Leverage AI to Boost Employee Wellness - Lydia Di Francesco - SocialHR...
SocialHRCamp
?
2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot
Marius Sescu
?
Everything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPT
Expeed Software
?
Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage Engineerings
Pixeldarts
?
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental Health
ThinkNow
?
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
marketingartwork
?
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024
Neil Kimberley
?
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
contently
?
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
Albert Qian
?
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
Search Engine Journal
?
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
SpeakerHub
?
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
Clark Boyd
?
Getting into the tech field. what next
Getting into the tech field. what next
Tessa Mero
?
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Lily Ray
?

iOS