Video in SwiftUI

Recently I have been tinkering with SwiftUI, the new UI framework from Apple

I’m enjoying the declarative approach, the ability to have real-time previews, and that all settings are save in code (bye, bye Interface Builder).

While this new framework as a lot going for it, it still doesn’t have the same depth of functionality of UIKit, that has years of development, and you may found out that some components you rely upon in UIKit are entirely non-existent in SwiftUI. But don’t despair Apple make it very easy to reuse any UIKit components by using two protocols to wrap around UIKit components:

They have very similar interfaces the main difference being what type of class they wrap around.

UIViewControllerRepresentable, as the name indicates, is used to wrap around UIViewController classes while the UIViewRepresentable wraps UIView classes.

One of the components missing in SwiftUI is a video view, so let’s see how can we use these protocols to implement it in SwiftUI.

For our video component, we are using an AVPlayerViewController so we will need to wrap it using a UIViewControllerRepresentable protocol and implement the following methods:

  • makeUIViewController() 
  • updateUIViewController()

Let’s see how we can implement those. We start by creating our video view:

public struct Video: UIViewControllerRepresentable {

  /// The URL of the video you want to display
  let videoURL: URL

Then we implement makeUIViewController():

public func makeUIViewController(context: Context) -> AVPlayerViewController {
  let videoViewController = AVPlayerViewController()
  videoViewController.player = AVPlayer(url: videoURL)
  return videoViewController

As you can see the goal of this method is to create the UIViewController we want to use and return it.

Next, we can implement the updateUIViewController() method, because at the moment we don’t allow any change to our video component after creation we can simply do this:

public func updateUIViewController(_ videoViewController: AVPlayerViewController, context: Context) {


At that’s it, our video component is ready to be used! You only need to do this:

struct VideoPlayerView: View {
  let videoURL = Bundle.main.url(forResource: "video", withExtension: "mp4")!
  var body: some View {
        Video(url: videoURL)

Build and run, and you should see something like this:

You know a have a ready to use video view, as you may have notice the video expanded to the full screen, because that is the default behaviour of the AVPlayerViewController, this is not ideally for some scenarios so let’s see how can we fix this.

One of the great thing of using this representable protocols is that you inherit for free all the modifies available for the View component so you can change the code to this:

Video(url: videoURL).frame(width: nil, height: CGFloat(exactly:300), alignment: .center)

Build and run you should see this:

That was easy!

On the next post I will show you how to add properties to change the video, hide the controls and mute the video.

Meanwhile if you want to check the end result you can have a look on the SVEVideoUI project repository in Github.


UIKonf 2019

Last week I attended UIKonf 2019 at Berlin.

This year conference had an all-female speaker line up, and I think this was an excellent idea to help increase gender diversity on our profession. A lot of female attendees commented they felt inspired to have the opportunity to see so many great presentations delivered by female speakers, and that it will help to attract more people to the mobile development space.

The videos will all be available online soon; bellow is just a quick review of the presentations I saw.

Day 1

Kaya Thomas – Inclusive and Accessible App Development

Kaya talked about why we should make our applications more accessible and inclusive. The accessibility part was detailed and offered some excellent insights, but I think the inclusivity part was lacking, there was only a brief reference about the subject.

Shannon Hughes – Detangling Gesture Recognizers

Shanon works for the Omni on the OmniGraffle project. Her presentation gave excellent insight on how to debug and make sense of gesture recognizers. Their application has more than 18 custom gestures!!!

Glenna Buford – Rolling your own Network Stack

A solid presentation on how to implement a network stack using only URLSession and enums.

Natalya – Declarative Presentations

A concise presentation about how to use an MVVM architecture on their app

Sensor – How to develop apps with a state of art architecture

A workshop brought by My Taxi developers ( an Uber kind of company) presenting their app architecture called Sensor. It’s a mix of MVVM, RXSwift and Dispatch/Redux. It looked reliable and easy to use, but if you don’t have previous knowledge of the concepts, it can be tricky to get a grasp on how things work together.

Julie Yaunches – Consistency Principle

Good advice from Julie on how apps architecture can degrade over time and how to avoid that.
The principle runs down that it’s better to have an older architecture but consistent over the app than having lots of new trends of architecture doing the same task on the app.

Lisa Dziuba – How to Market Your App

Lisa is one of the creators of the Flawless App. She gave great recommendations on how to market your app in all different channels available.

Day 2

Erica Sadun – String Interpolations

A legend of the Swift world showing us how to use the new String Interpolation feature in Swift 5.1

Ellen Shapiro – Kotlin Native

High-speed presentation from Ellen, showing all the challenges she encountered when trying to use Kotlin Native for a cross-platform app in iOS, Android and Arduino using Kotlin.
On its current status, I don’t think Kotlin native is ready for prime time, still a lot of problems to make it work.

Alaina Kafkes – A11y-oop Adding new Accessibility Features to not-so-new app

Alaina talked about adding accessibility features to the Medium app. The app was in a pretty bad state A11y wise a year ago, and she spoke on how they add VoiceOver and Dynamic Fonts to it.
This presentation went in way too much code detail, with almost naming all the classes and protocols they used on the project.

Neha Kulkarni – Advance Colors in iOS

Neha showed all the new ways to define colours in the latest version of Xcode and iOS. She also provided great advice on how to organise colours and create a semantic colour library.

Anne Calahan – Promises in iOS

Enjoyed the way Anne did this presentation, instead of declaring Promises was the next great thing, she did a proper analysis of it:

  • investigated the history of Promises ( first time presented in 1979),
  • found out the current implementations available on iOS/Swift
  • the tradeoffs of each one
  • shows us an example of how to refactor an application to use it.

Julia Roggatz – Muse Prototype Challenges

A fantastic presentation about the challenges of developing an app with an innovative infinite zoom interface.
The app interface looks fantastic, and for a prototype, it’s already looking pretty solid.
A lot of attendees asked where they could sign up for the beta.

Kate Castellano – Backend Driven UI

I was a bit disappointed about this presentation; I was expecting a more generic way of deploying a generic UI using a backend technology. But in the end, it was something more similar to what we do with notifications on WPIOS.

Lea Marolt – Mobile && Me == It’s Complicated

Lea tells her story since she was a young developer, and how she grew from being a big fan of mobile, to get utterly disappointed with it because of some unethical practices in the industry.
The story ends with a positive spin after she takes some time off and works on other crafts and starts to realize the advantages of digital when used correctly.


The majority of presentations were of high quality and targetted to different experience levels. The only downside was that there was anything completely new, maybe because of the conference dates are just before WWDC.

The organizers were excellent and helped you out with any request. The venue was terrific, with lots of seating all with high visibility, the sound was great, and there was live transcribing. We also an outside space on the bank of the river with a boat where we could relax in the deck.

Lunch was of excellent quality and 100% vegetarian. Coffee was amazing; the blend changed every two hours!!

People attending the conference were friendly and always ready to change some ideas. The organization had an excellent idea of organizing different touring activities on the Sunday before the conference; this helped to meet other people attending and break the ice before the conference.

I highly recommend attending UIKonf!

Blog Code

Aztec: A native HTML editor

One of the most important aspects of the WordPress app is the post editor, after all the content generated by our users is what make their site different from all the others.

Since we started developing the WordPress apps, we wanted to provide the best writing experience to our users, writing is already hard by itself so you don’t need that the tool you are using to write to be complex, slow, or error prone.

Last August my team at Automattic started a project called Aztec, the ambitious goal was to have HTML editor that could support the same feature set has our web editor but completely written in native code.

After some hard working months, we are now releasing the first beta version of the editor in our native apps. If you want to know more you can read the official release post here and for the more technical inclined an architecture overview is available here. You can also check our open source code on our Github repo.

Remember this is not limited to be used on the WordPress apps, you can use it in any app that needs an HTML editor.

Pull Requests are welcome!

Blog Code

Font Metrics update

I developed FontMetrics some years ago when I was working for several publishing clients. Clients in publishing normally want to use their own brand fonts and have precise requirements regarding layout and text alignments.

This was a bit tricky to do with the iOS text system at the time, so I developed this app to allow to preview fonts and check their different metrics. This allowed the design team to do quick checks on the fonts files provided by the client and to quickly find the metrics to use for layouts.

This week I finally got some free time so I decided to have a look what needed to be done to bring it up to iOS 10. The last time I updated this app was when iOS 7 was launched, so it really needed some TLC!

This was implemented way before Swift was a thing so all of the app was written in Objective-C, and when I started to look at the code I found a lot of small issues and some really bad code.

It’s very humbling to look at code that you developed some years ago, it really makes you understand that you are always learning and you can always improve.

The app was so simple that I decided to update all code to Swift and do some much needed updates to the UI/UX.

I’m pretty happy with the end result so I decided to make the app open source and publish the repo on GitHub, bug reports and PRs are welcome!

Blog Code

Quicker Unit Test in iOS

Today while reviewing a PR on the WordPress iOS app just found about this very interesting technique to make iOS unit tests run quicker and more contained.

Simple and very efficient.



A New WordPress App Update, Designed for the iPad

A great update on our WordPress app.

The Blog

Over the past few months we’ve been working to dramatically improve users’ experience on iPad — and we’re proud to share those with you now, in our WordPress app for iOS, available in the iTunes Store.


The apps were originally designed with only iPhones in mind, so we wanted to make better use of the space available on the iPad and especially iPad Pro, to maximize your productivity in the app. These changes shipped incrementally, with the very final ones being included in the 7.1 release that went out last week.

We’ve improved the My Sites management to make it easier to handle multiple sites on iPad. Now you can see the posts from that blog alongside the list of blogs, and site management is now all on one screen – no more back and forth!


Managing profiles follows the same pattern.


Notifications has been overhauled, too.


We’re really…

View original post 34 more words


Last year Automattic, the company that I work for, bought the .blog domain  and being a generous company they decided to offer to all automatticians  the domain of their choice!

So say hello to my new domain: !!

So from now on I will start using this new home for my personal writing:  travel, review of co-work places, day to day events, music, books and rants.

I will keep going on but now more focused on my professional achievements, conferences and code projects that I work.


Automattic GM 2016 – Whistler

This year the Automattic Grand Meetup was in Whistler village. The Grand Meetup is an annual event where all workers of Automattic meet in a single place, do new friendships renew some old ones, have fun together and plan the year ahead.
The week started with a big town hall where Matt Mullenweg (our CEO) answer all the questions, it lasted until 2 am. Then for the rest of the week we split into groups doing workshops, projects, and classes. The last two days were dedicated to fun activities: trekking, escape rooms, downhill bikes, beer festival, photo tours, zipline and others. It all finished with a bang with a big party on the last night, even a bear showed up!




Try Swift! NYC

This year I had the opportunity to attend Try Swift! NYC, a conference dedicated 100% to Swift development.

This is a fairly new conference, the first edition was back in March in Tokyo, but the organization level was excellent. From the initial website, the registration process, pre-conference events, the conference itself all was great.
It all started with a pre-meetup party on the Meetup headquarters in NY, the nice folks of Meetup did a quick introduction and talked about their new app, 100% made in Swift, and then send us to a rooftop dinner and drinks!!!

Next day the event itself was in the Axa Event and Production Center in downtown Manhattan , this was a one track event and it all happened on a good sized auditorium, all very comfortable and with excellent audio/video resources. The presentations and  speaker were of high quality and they ranged from beginner to advanced topics.

My favorite presentations were:

  • Random Talk: the Consistent World of Noise, excellent talk from Natalia Berdis about random algorithms and how to use it. Excellent  slides to show how different random algorithms works and when to use them.
  • Lambda: There and Back Again, from Rob Napier simple down to earth presentation on how to use functional programming in Swift.
  • Incremental Swift , Amy Dier talks on how Etsy is using Swift in their codebase. Very interesting to learn they try to keep everything done in Swift still easy accessible from Objective-C code.
  • Making rich text editor in Swift, Katsumi Kishikawa with a very detailed talk about all the things you need to be aware when working with text in iOS. I learned this the hard way some year ago when doing the Aljazeera app.

Besides the presentations/speakers, the great thing about the conference was the community that was present, everyone was super friendly and always available for a chat. The speakers themselves were super friendly and made time during their time there to answer any question that you could have, in fact after they did their presentation they were available for an hour to answer questions.

The organizers themselves had super attention to detail, they provided every morning a set of basic toiletries, in case you had forgot something while packing. They had a magician as a break entertrainer to keep people energized between presentations. Of course good coffee was always available and the meals provided where not bad at all.

The only hiccup of the conference was the Pizza and Party event of the first night, it was a bit disorganized and the pizza took forever! Quickly sorted out by going out to local Pizzaria recommended by NY local.

Definitely going back next year! Keep up the good work Natasha the Robot!


Crashes on iOS 10

I spent the last week watching Apple WWDC videos, and after downloading Xcode 8 I was testing several of my libraries and applications.

While testing the WPMediaPicker library I found that the sample app was crashing every time I opened the picker. After some investing on the code I found that the app was crashing the moment I was trying to use the PHPhotoLibrary class.

After some looking around in the documentation I found out this:

Important: To protect user privacy, an iOS app linked on or after iOS 10.0, and which accesses the user’s photo library, must statically declare the intent to do so. Include the NSPhotoLibraryUsageDescription key in your app’s Info.plist file and provide a purpose string for this key. If your app attempts to access the user’s photo library without a corresponding purpose string, your app exits.

Before iOS 9 if you tried to access the Photo Library without providing this key you will simple get a default warning but now it simple crashes the app.

And this isn’t limited to the Photo Library, this is now standard behaviour to all the libraries that need to have an UsageDescription key on the Info.plist file.

Here is a small compilation:

  • NSAppleMusicUsageDescription

  • NSBluetoothPeripheralUsageDescription

  • NSCalendarsUsageDescription

  • NSCameraUsageDescription

  • NSContactsUsageDescription

  • NSHealthShareUsageDescription

  • NSHomeKitUsageDescription

  • NSLocationAlwaysUsageDescription

  • NSLocationWhenInUseUsageDescription

  • NSMicrophoneUsageDescription

  • NSMotionUsageDescription

  • NSPhotoLibraryUsageDescription

You can find the full list here, just search for UsageDescription on the page.

If you still are not using these keys is time to start to update your applications and use this opportunity to provide a good text for your requests for permissions from  your app users.