SlideShare une entreprise Scribd logo
1  sur  72
Télécharger pour lire hors ligne
Standford 2015 iOS讀書會
week8
1. Application Lifecycle and Core Motion
2. Core Location
彼得潘
接收notification
userInfo: notification包含的額外資訊
func addObserver(observer: AnyObject, selector aSelector: Selector, name aName: String?,
object anObject: AnyObject?)
⽅方法2:
發送notification時所在的thread,即是執⾏行Selector的thread
發送notification
func postNotification(notification: NSNotification)
func postNotificationName(aName: String, object anObject: AnyObject?)
func postNotificationName(aName: String, object anObject: AnyObject?,
userInfo aUserInfo: [NSObject : AnyObject]?)
移除notification
func removeObserver(observer: AnyObject)
func removeObserver(observer: AnyObject, name aName: String?, object
anObject: AnyObject?)
沒有remove,notification傳送給死去的物件會crash
特例: controller在死去時會⾃自動remove
可以定期將App喚醒,
進⼊入background mode做事( 很短的時間),
然後再進⼊入suspended mode
令⼈人遺憾的,程式無法知道App死亡,不能在死前完成⼀一些⼼心願
UIApplicationDidFinishLaunchingNotification會在
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions:
[NSObject: AnyObject]?) -> Bool執⾏行完後才被呼叫。
background fetch: 觀察使⽤用者的使⽤用習慣,聰明地觸發某個時間點在背景抓資料
UIApplication
@property(nonatomic) NSInteger applicationIconBadgeNumber;
var statusBarFrame: CGRect { get }
預設⾼高度20,開啟熱點時變40
AirDrop
Document Types &
Imported UTIs
Document Types &
Imported UTIs
http://www.infragistics.com/community/blogs/stevez/archive/2013/03/04/associate-a-file-type-
with-your-ios-application.aspx
https://developer.apple.com/library/ios/documentation/Miscellaneous/Reference/UTIRef/
Articles/System-DeclaredUniformTypeIdentifiers.html#//apple_ref/doc/uid/TP40009259-SW1
http://www.infragistics.com/community/blogs/stevez/archive/2013/03/05/ios-tips-and-tricks-
associate-a-file-type-with-your-app-part-2.aspx
可以接收
func application(application: UIApplication, handleOpenURL url: NSURL)
-> Bool
{
// post a notification when a GPX file arrives
let center = NSNotificationCenter.defaultCenter()
let notification = NSNotification(name: GPXURL.Notification,
object: self, userInfo: [GPXURL.Key:url])
center.postNotification(notification)
return true
}
center.postNotificationName(GPXURL.Notification, object: self,
userInfo: [GPXURL.Key:url])
發送Notification
也可以
struct GPXURL {
static let Notification = "GPXURL Radio Station"
static let Key = "GPXURL URL Key"
}
接收Notification
override func viewDidLoad() {
super.viewDidLoad()
// sign up to hear about GPX files arriving
let center = NSNotificationCenter.defaultCenter()
let queue = NSOperationQueue.mainQueue()
let appDelegate = UIApplication.sharedApplication().delegate
center.addObserverForName(GPXURL.Notification, object:
appDelegate, queue: queue) { notification in
if let url = notification?.userInfo?[GPXURL.Key] as? NSURL
{
self.textView.text = "Received (url)"
}
}
}
耗電
X Y Z
直立的方向

home按鈕在下方

螢幕和地板垂直
0 -1


0
直立的方向

home按鈕在上方

螢幕和地板垂直
0


1 0
水平的方向

右長邊在下

螢幕和地板垂直
1


0 0
水平的方向

右長邊在上

螢幕和地板垂直
-1 0 0
螢幕面對天花板 0 0 -1
螢幕面對地板 0 0 1
(1)	 將iPhone傾向下短邊。

結果: y < 0

(2)	 將iPhone傾向上短邊。

結果: y > 0

(3)	 將iPhone傾向左長邊。

結果: x < 0

(4)	 將iPhone傾向右長邊。

結果: x > 0

iPhone繞著從天⽽而降的重⼒力⽅方向旋轉,
-> 偵偵到的x, y, z值維持定值
偵測旋轉
磁⼒力
結合三種sensor資料後,分析出來的數據
lazy var blockBehavior: UIDynamicItemBehavior = {
let lazilyCreatedBlockBehavior = UIDynamicItemBehavior()
lazilyCreatedBlockBehavior.allowsRotation = true
lazilyCreatedBlockBehavior.elasticity = 0.85
lazilyCreatedBlockBehavior.friction = 0
lazilyCreatedBlockBehavior.resistance = 0
return lazilyCreatedBlockBehavior
}()
let motionManager = AppDelegate.Motion.Manager
if motionManager.accelerometerAvailable {
motionManager.startAccelerometerUpdatesToQueue(NSOperationQueue.mainQu
eue()) { (data, error) -> Void in
self.bouncer.gravity.gravityDirection = CGVector(dx:
data.acceleration.x, dy: -data.acceleration.y)
}
}
Core Location
愈準,愈要耗費⼼心⼒力,愈耗電
timestamp: 偵測到位置的時間點,幫助判斷資料是否過時
設定模擬器位置
設定模擬器位置
取得使⽤用者位置的權限
取得使⽤用者位置的權限
只有使⽤用者還沒回答過的情況下會出現。

(⼀一次表⽩白的機會)
如果重新安裝會再出現
(下輩⼦子可以重新表⽩白)
如何知道使⽤用者reject

(⽐比猜出表⽩白被reject簡單)
[CLLocationManager authorizationStatus];
- (void)locationManager:(CLLocationManager *)manager
didChangeAuthorizationStatus:(CLAuthorizationStatus)status;
當使⽤用者點選權限詢問視窗後觸發
算距離
- (CLLocationDistance)distanceFromLocation:(const CLLocation
*)location ;
公尺
可從經緯度建⽴立CLLocation物件
在背景啟動
搭配requestAlwaysAuthorization
在背景持續取得使⽤用者位置
App介紹的耗電提醒
App Store上的App⽂文字介紹需注明因在背景抓取位置造成耗電,否則將被退件
LOCOMO運動記錄
台灣Health & Fitness Top 2
LOCOMO運動記錄
LOCOMO運動記錄

Contenu connexe

En vedette

不能承受的感動 - iOS App實機測試
不能承受的感動 - iOS App實機測試不能承受的感動 - iOS App實機測試
不能承受的感動 - iOS App實機測試彼得潘 Pan
 
Standford 2015 week7: 1. Unwind Segues, Alerts, Timers, View Animation 2. Dyn...
Standford 2015 week7: 1. Unwind Segues, Alerts, Timers, View Animation 2. Dyn...Standford 2015 week7: 1. Unwind Segues, Alerts, Timers, View Animation 2. Dyn...
Standford 2015 week7: 1. Unwind Segues, Alerts, Timers, View Animation 2. Dyn...彼得潘 Pan
 
你的程式開發初體驗 (以Swift為例)
你的程式開發初體驗 (以Swift為例)你的程式開發初體驗 (以Swift為例)
你的程式開發初體驗 (以Swift為例)彼得潘 Pan
 
iOS 入門教學
iOS 入門教學iOS 入門教學
iOS 入門教學Steven Shen
 
Standford 2015 week4: 1.Protocols and Delegation, Gestures 2. Multiple MVCs
Standford 2015 week4: 1.Protocols and Delegation, Gestures 2. Multiple MVCsStandford 2015 week4: 1.Protocols and Delegation, Gestures 2. Multiple MVCs
Standford 2015 week4: 1.Protocols and Delegation, Gestures 2. Multiple MVCs彼得潘 Pan
 
Standford 2015 week3: Objective-C Compatibility, Property List, Views
Standford 2015 week3: Objective-C Compatibility, Property List, ViewsStandford 2015 week3: Objective-C Compatibility, Property List, Views
Standford 2015 week3: Objective-C Compatibility, Property List, Views彼得潘 Pan
 
Standford 2015 week6
Standford 2015 week6Standford 2015 week6
Standford 2015 week6彼得潘 Pan
 
Standford 2015 week5: 1.View Controller Lifecycle, Autolayout 2. Scroll View ...
Standford 2015 week5: 1.View Controller Lifecycle, Autolayout 2. Scroll View ...Standford 2015 week5: 1.View Controller Lifecycle, Autolayout 2. Scroll View ...
Standford 2015 week5: 1.View Controller Lifecycle, Autolayout 2. Scroll View ...彼得潘 Pan
 
Standford 2015 iOS讀書會 week1: 1.Logistics , iOS 8 Overview 2. More Xcode and S...
Standford 2015 iOS讀書會 week1: 1.Logistics , iOS 8 Overview 2. More Xcode and S...Standford 2015 iOS讀書會 week1: 1.Logistics , iOS 8 Overview 2. More Xcode and S...
Standford 2015 iOS讀書會 week1: 1.Logistics , iOS 8 Overview 2. More Xcode and S...彼得潘 Pan
 
Standford 2015 iOS讀書會 week2: 1. Applying MVC 2. More Swift and Foundation Fra...
Standford 2015 iOS讀書會 week2: 1. Applying MVC 2. More Swift and Foundation Fra...Standford 2015 iOS讀書會 week2: 1. Applying MVC 2. More Swift and Foundation Fra...
Standford 2015 iOS讀書會 week2: 1. Applying MVC 2. More Swift and Foundation Fra...彼得潘 Pan
 
打造你的第一個 iOS App
打造你的第一個 iOS App  打造你的第一個 iOS App
打造你的第一個 iOS App 彼得潘 Pan
 
iOS Coding Best Practices
iOS Coding Best PracticesiOS Coding Best Practices
iOS Coding Best PracticesJean-Luc David
 

En vedette (14)

不能承受的感動 - iOS App實機測試
不能承受的感動 - iOS App實機測試不能承受的感動 - iOS App實機測試
不能承受的感動 - iOS App實機測試
 
iOS 7 SDK特訓班
iOS 7 SDK特訓班iOS 7 SDK特訓班
iOS 7 SDK特訓班
 
Standford 2015 week7: 1. Unwind Segues, Alerts, Timers, View Animation 2. Dyn...
Standford 2015 week7: 1. Unwind Segues, Alerts, Timers, View Animation 2. Dyn...Standford 2015 week7: 1. Unwind Segues, Alerts, Timers, View Animation 2. Dyn...
Standford 2015 week7: 1. Unwind Segues, Alerts, Timers, View Animation 2. Dyn...
 
為愛打造App
為愛打造App為愛打造App
為愛打造App
 
你的程式開發初體驗 (以Swift為例)
你的程式開發初體驗 (以Swift為例)你的程式開發初體驗 (以Swift為例)
你的程式開發初體驗 (以Swift為例)
 
iOS 入門教學
iOS 入門教學iOS 入門教學
iOS 入門教學
 
Standford 2015 week4: 1.Protocols and Delegation, Gestures 2. Multiple MVCs
Standford 2015 week4: 1.Protocols and Delegation, Gestures 2. Multiple MVCsStandford 2015 week4: 1.Protocols and Delegation, Gestures 2. Multiple MVCs
Standford 2015 week4: 1.Protocols and Delegation, Gestures 2. Multiple MVCs
 
Standford 2015 week3: Objective-C Compatibility, Property List, Views
Standford 2015 week3: Objective-C Compatibility, Property List, ViewsStandford 2015 week3: Objective-C Compatibility, Property List, Views
Standford 2015 week3: Objective-C Compatibility, Property List, Views
 
Standford 2015 week6
Standford 2015 week6Standford 2015 week6
Standford 2015 week6
 
Standford 2015 week5: 1.View Controller Lifecycle, Autolayout 2. Scroll View ...
Standford 2015 week5: 1.View Controller Lifecycle, Autolayout 2. Scroll View ...Standford 2015 week5: 1.View Controller Lifecycle, Autolayout 2. Scroll View ...
Standford 2015 week5: 1.View Controller Lifecycle, Autolayout 2. Scroll View ...
 
Standford 2015 iOS讀書會 week1: 1.Logistics , iOS 8 Overview 2. More Xcode and S...
Standford 2015 iOS讀書會 week1: 1.Logistics , iOS 8 Overview 2. More Xcode and S...Standford 2015 iOS讀書會 week1: 1.Logistics , iOS 8 Overview 2. More Xcode and S...
Standford 2015 iOS讀書會 week1: 1.Logistics , iOS 8 Overview 2. More Xcode and S...
 
Standford 2015 iOS讀書會 week2: 1. Applying MVC 2. More Swift and Foundation Fra...
Standford 2015 iOS讀書會 week2: 1. Applying MVC 2. More Swift and Foundation Fra...Standford 2015 iOS讀書會 week2: 1. Applying MVC 2. More Swift and Foundation Fra...
Standford 2015 iOS讀書會 week2: 1. Applying MVC 2. More Swift and Foundation Fra...
 
打造你的第一個 iOS App
打造你的第一個 iOS App  打造你的第一個 iOS App
打造你的第一個 iOS App
 
iOS Coding Best Practices
iOS Coding Best PracticesiOS Coding Best Practices
iOS Coding Best Practices
 

Standford 2015 week8