Standford 2015 week8

72
Standford 2015 iOS讀書會 week8 1. Application Lifecycle and Core Motion 2. Core Location 彼得潘

Transcript of Standford 2015 week8

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" }

接收Notificationoverride 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.mainQueue()) { (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運動記錄