Ask anyone, and they will tell you that WWDC2014 is the most exciting memory in recent years. The General Assembly has not released any new hardware, it is an unprecedented software developers feast.
Just the release of iOS 8 and OS X Yosemite can make 2014 an epoch-making year for the Apple platform, plus extension,continuity,spritekit improvements, iOS scenekit,metal,healthkit,local Authentication and a brand-new photo frame. Needless to say, Xcode and interface Builder have significantly improved, redesigned itunes Connect,testflight, crash reporting and Cloudkit. And oh yeah-swift of course.
What's more, it's a good. Apple has loosened her confidentiality agreement, which means we can discuss these new toys openly now.
This week, we're going to go through the fog of iOS 8 and explore some of the new APIs that everyone should know about.
From now on, Nshipster will mainly use Swift to write sample code. Before the summer is over, we want to convert all existing code to SWIFT and provide the option to switch languages. nsprocessinfo-isoperatingsystematleastversion
Forget [[Uidevice Currentdevice] systemversion] and nsfoundationversionnumber, now you can use Nsprocessinfo- Isoperatingsystematleastversion to determine the system version. Swift
Import Foundation let
yosemite = Nsoperatingsystemversion (Majorversion:10, Minorversion:10, patchversion:0)
Nsprocessinfo (). Isoperatingsystematleastversion (Yosemite)//False
It's worth noting that you should use Someclass.class or respondstoselector when doing compatibility testing:. The compiler macros in Swift and C can be used to select code based on different build configurations and targets. New Nsformatter Subclass
One of the most significant missing features in foundation is the inability to handle weight and length unit conversions. In iOS 8 and OS X Yosemite, three new classes of Nsenergyformatter,nsmassformatter and Nslengthformatter were introduced to compensate for this loss.
This makes the number of Nsformatter subclasses doubled, preceded only by Nsnumberformatter,nsdateformatter and Nsbytecountformatter.
Although these are foundation subclasses, they are mostly used in HealthKit. Nsenergyformatter
Nsenergyformatter uses Coke as the original unit of energy, and when handling health information, the card is used. Swift
Let Energyformatter = Nsenergyformatter ()
Energyformatter.forfoodenergyuse = True let
joules = 10_000.0
println (Energyformatter.stringfromjoules (Joules))//"2.39 Cal"
Nsmassformatter
Although mass is the basic unit of the existence of matter, in HealthKit, it mainly refers to body weight. Swift
Let Massformatter = Nsmassformatter () let
kilograms = 60.0
println ( kilograms))//"132 lb"
Nslengthformatter
The last new subclass of Nsformatter is nslengthformatter. We can think of it as a mkdistanceformatter version of the building. Swift
Let Lengthformatter = Nslengthformatter () let
meters = 5_000.0
println ( meters))//"3.107 mi"
Cmpedometer
Along the health route of iOS 8, Cmstepcounter was redesigned. As an improved version of Cmpedometer, it can not only instantly capture discrete point data, but also track footsteps and distances at the same time, and even calculate the total number of stair climbs.
M7 chips are really powerful. Swift
Import coremotion let
lengthformatter = Nslengthformatter () let
pedometer = Cmpedometer ()
Pedometer.startpedometerupdatesfromdate (NSDate (), Withhandler: {data, error in
if!error {
println ("Steps Taken: \ (data.numberofsteps) ") let
distance = Data.distance.doubleValue
println (" Distance: \ "( Lengthformatter.stringfrommeters (distance)) ") Let time
= Data.endDate.timeIntervalSinceDate (data.startdate) Let
speed = Distance/time
println ("Speed: \ Lengthformatter.stringfrommeters (speed))/S"
}
)
Cmaltimeter
On supported devices, Cmaltimeter can make Cmpedometer's floorsascended,floorsdescended data more accurate: Swift
Import coremotion let
altimeter = Cmaltimeter ()
if cmaltimeter.isrelativealtitudeavailable () {
Altimeter.startrelativealtitudeupdatestoqueue (Nsoperationqueue.mainqueue (), Withhandler: {data, error in
if! Error {
println ("Relative altitude: \ (data.relativealtitude)"}
}
)
}
Clfloor
Clfloor's introduction shows an ambitious plan for Apple's foray into interior navigation, with floor information playing an important role. Swift
Import Corelocation
class Locationmanagerdelegate:nsobject, cllocationmanagerdelegate {
func Locationmanager (manager:cllocationmanager!, didupdatelocations locations:anyobject[]!) {Let
location:cllocation? = Locations[0] as? Cllocation
if let floor:clfloor? = location? Floor {
println ("Current floor: \ Floor?"). level) '}}} let
manager = Cllocationmanager ()
manager.delegate = Locationmanagerdelegate ()
manager.startupdatinglocation ()
Hkstatistics
As a framework, HealthKit contains a large number of subclasses and constants. To fully understand, Hkstatistics is a good start.
HealthKit manages all the physiological information, such as heart rate, calorie intake, blood oxygen, and so on, and is aggregated together through a unified API.
The following example shows how to dig and get separate data from a day's continuous data: Swift
Import HealthKit let
collection:hkstatisticscollection?
Let Statistics:hkstatistics? = collection!. Statisticsfordate (NSDate ()) for
Item:anyobject in statistics!. Sources {
if let Source = Item as? Hksource {
if let quantity:hkquantity = statistics!. Sumquantityforsource (source) {
if Quantity.iscompatiblewithunit (Hkunit.gramunitwithmetricprefix) (. Kilo)) {let
massformatter = Nsmassformatter () let
kilograms = Quantity.doublevalueforunit ( Hkunit.gramunitwithmetricprefix (. Kilo))
println (massformatter).