Today we'll take a look at how to add Apple Pay to an app, letting users buy things. Let's begin.
Our first step is to go to Project > Capabilities > Apple Pay and flip the switch to "on". This will configure our project to use Apple Pay and create a Merchant ID for us automatically (We'll use this later when configuring our payment request). Then, we'll importing PassKit, and add a button to kick off the process:
funcpaymentButtonTapped(){letrequest=PKPaymentRequest()request.merchantIdentifier="merchant.com.magnus.apple-samplecode.apple-pay.Emporium"request.countryCode="US"request.currencyCode="USD"request.supportedNetworks=[PKPaymentNetworkVisa,PKPaymentNetworkMasterCard]request.merchantCapabilities=.Capability3DS// 3DS or EMVrequest.paymentSummaryItems=[PKPaymentSummaryItem(label:"Little Bites of Cocoa Volume 1",amount:NSDecimalNumber(double:39.99),type:.Final),PKPaymentSummaryItem(label:"Shipping",amount:NSDecimalNumber(double:5.99),type:.Final),PKPaymentSummaryItem(label:"Tax",amount:NSDecimalNumber(double:3.79),type:.Final),PKPaymentSummaryItem(label:"Total",amount:NSDecimalNumber(double:49.77),type:.Final)]letauthVC=PKPaymentAuthorizationViewController(paymentRequest:request)authVC.delegate=selfpresentViewController(authVC,animated:true,completion:nil)}
Then we'll implement the authorized delegate function:
A continued huge thanks to the folks at Hired.com for sponsoring this week's bites. Finding a good job can be a daunting task. Today we'll take a look at how we can use Hired.com to save our sanity, and find a great job in the process. Let's dive in.
We'll start by entering our email and answering some basic questions:
That's it. Hired will then work with over 3,000 pre-screened companies (both big and small) in 13 major metro areas (North America & Europe) to try find us a great job. Software engineers and designers on Hired can get 5+ job offers in a single week. Each offer will include salary and equity details upfront.
We can view interview requests and accept or reject them before talking to any company. If we get a job through Hired, they'll give us a $1,000 "thank you" bonus!
View offers and accept or reject them without talking to anyone.
Completely FREE + no obligations ever.
Hired is the real deal. They have solved some very real problems and fixed this historically messy process. Looking for a job? Don't waste time, sign up for Hired today so you can get back to work!
Finally, Hue can analyze a UIImage, returning a tuple of colors it finds inside.
let(bg,primary,secondary,_)=albumArt.colors()
It provides background, primary, secondary and detail colors from the image. We can the use these to create interfaces such as the one found in iTunes on OS X, where the colors in the UI match those found in the album artwork. Neat!
Ever since 1983 when Matthew Broderick's IMSAI 8080 began speaking out loud, we've dreamed of computers that can have conversations with us.
In iOS 9, Apple added the ability to synthesize speech using the high-quality 'Alex' voice. Sadly it's only available on US devices for now, but that's sure to change. Let's try it out:
guardletvoice=AVSpeechSynthesisVoice(identifier:AVSpeechSynthesisVoiceIdentifierAlex)else{return}letsynth=AVSpeechSynthesizer()synth.delegate=selfletutter=AVSpeechUtterance(string:"Would you like to play a game?")utter.voice=voicesynth.speakUtterance(utter)
We start by making sure 'Alex' is available, then we make a new synthesizer. Next, we create an AVSpeechUtterance, and set it's voice. Then, we simply tell the synthesizer to speak! Very cool.
Even cooler, we can implement one of the optional functions of AVSpeechSynthesizerDelegate to get live progress callbacks as each word is spoken. Neat!
Most iOS developers have used the fantastic UIViewanimateWithDuration family of functions. But there's another, slightly-lesser known static function on UIView that can help us transition like a pro. Let's check it out:
The function we'll be trying is transitionWithView. At first glance you'll see it's takes the same duration, options, and closures as its more-popular sister function.
However, instead of applying our changes over time, this function will (conceptually) take a snapshot of our view before and after the work in the animations** closure** is performed, then visually transition from the first snapshot to the second.
The type of transition depends on the animation option we pass in. We can do everything from simple cross-dissolves (fades), to fancy 3D flips. Super handy for simple transitions.
Download a sample project at j.mp/bite193. In it, we flip a thumbs up emoji using this incredibly simple technique.
Low Power Mode debuted in iOS 9. It's a great way to get another hour or two of use out of a device.
When Apple first introduced this functionality, they touted how the feature turns of lots of little things that contribute to the battery savings.
It turns out we can actually provide these same savings in our own apps. Today we'll learn how. Let's get started:
The first way we'll interact with Low Power Mode is through a property on NSProcessInfo.
Here we're working on app that displays animated GIFs. When the user stops scrolling, we automatically begin looping the GIF. However, if the user has Low Power Mode enabled, we'll save a few CPU/GPU cycles by requiring the user to tap to start the animation:
Then, inside lowPowerModeChanged, we can check with NSProcessInfo again. Now when a user returns to our app, we'll start/stop visible GIFs based on the Low Power Mode state. Neat!
A continued huge thanks to the folks at Hired.com for sponsoring this week's bites. Finding a good job can be a daunting task. Today we'll take a look at how we can use Hired.com to save our sanity, and find a great job in the process. Let's dive in.
We'll start by entering our email and answering some basic questions:
That's it. Hired will then work with over 3,000 pre-screened companies (both big and small) in 13 major metro areas (North America & Europe) to try find us a great job. Software engineers and designers on Hired can get 5+ job offers in a single week. Each offer will include salary and equity details upfront.
We can view interview requests and accept or reject them before talking to any company. If we get a job through Hired, they'll give us a $1,000 "thank you" bonus!
View offers and accept or reject them without talking to anyone.
Completely FREE + no obligations ever.
Hired is the real deal. They have solved some very real problems and fixed this historically messy process. Looking for a job? Don't waste time, sign up for Hired today so you can get back to work!
Today we'll follow up with another Bite in our localization series (sort of). Units of measurement are important to get right when localizing our apps for different cultures.
Today we'll look at a library from Khoa Pham called Scale that can help us convert between different units of measurement (for either localization or just-for-fun purposes). Let's begin.
Scale makes it incredible simple to work with different units of measurement:
Most Swift developers are familiar with how great Swift is at integrating with Objective-C, but what about C itself? Today we'll look at how we can interact with C variables and pointers in Swift. Let's dive in.
First we create a regular Int value in Swift. Nothing special. Then, we'll pass it in to the withUnsafePointerfunction, along with a closure. Inside, we'll be passed in an UnsafePointer version of our original Int. We'll use the unsafeBitCastfunction to convert it into to a void pointer. Finally, we'll pass it in the C function. Whew!