How to play videos in iOS 9

Until iOS 8 we could use the trusty old MPMoviePLayerViewController class to play videos on our devices, but that’s been deprecated in iOS 9. From now on, Apple recommend we use the AVPlayerViewController class instead. It has many advantages and even supports picture-in-picture out of the box.

Although AVPlayerViewController is a subclass of UIViewController, instances of it cannot be presented using presentViewController – but Apple make sure not to mention this little tidbit. It’s much more “fun” to figure this out on our own.

Here’s how we can use it with a local video from the main bundle:

@import AVFoundation;
@import AVKit;

// ...

// grab a local URL to our video
NSURL *videoURL = [[NSBundle mainBundle]URLForResource:@"video" withExtension:@"mp4"];

// create an AVPlayer
AVPlayer *player = [AVPlayer playerWithURL:videoURL];

// create a player view controller
AVPlayerViewController *controller = [[AVPlayerViewController alloc]init];
controller.player = player;
[player play];

// show the view controller
[self addChildViewController:controller];
[self.view addSubview:controller.view];
controller.view.frame = self.view.frame;

First we grab a URL to either a local or remote video. Next we create an AVPlayer with this URL, and add said player to the newly created AVPlayerViewController instance. You can auto-play a video by using the player’s play method, or remove it and leave it up to the user to start the video.

Next we’ll present the controller by adding it as a subview to our current view, making sure it has the same frame size.

The class works equally well with local and remote URLs. To play a remote asset, construct the URL like this:

NSURL *videoURL = [NSURL URLWithString:@"https://github.com/versluis/Movie-Player/blob/master/Movie%20Player/video.mov?raw=true"];

What’s new in iOS 9.2

This update contains improvements and bug fixes including:

Apple Music improvements

  • You can now create a new playlist when adding a song to a playlist
  • Your most recently changed playlist is now listed at the top when adding songs to playlists
  • Download albums or playlists from your iCloud Music Library by tapping the iCloud download button
  • See which songs have been downloaded with the new download indicator next to each song in My Music and Playlists
  • See works, composers, and performers while browsing Classical music in the Apple Music catalog

General improvements

  • A new Top Stories section in News so you can stay up to date with the most important news of the day (available in the United States, United Kingdom, and Australia)
  • Mail Drop in Mail for sending large attachments
  • iBooks now supports 3D Touch to peek and pop pages from the table of contents, your notes and bookmarks, or from search results inside a book
  • iBooks now supports listening to an audiobook while you browse your library, read other books, or explore the iBooks Store
  • iPhone support for the USB Camera Adapter to import photos and videos
  • Improved stability of Safari
  • Improved stability of Podcasts
  • Fixing an issue that caused mail attachments to be inaccessible for some users with POP email accounts
  • Resolving an issue for some users that caused attachments to overlap text in mail
  • Fixing an issue where Live Photos could have turned off after restoring from a previous iCloud backup
  • Addressing an issue that could cause search in Contacts to display no results
  • Resolving an issue that could have prevented Calendar from displaying all seven days in week view
  • Fixing an issue where Camera screen on iPad could be black when attempting to capture video
  • Addressing an issue that could cause instability in the Activity app when viewing the day of Daylight Savings Time transition
  • Fixing an issue that could prevent data from appearing in Health
  • Fixing an issue that could prevent Wallet updates and Lock screen alerts from displaying
  • Addressing an issue where updating iOS could prevent an alarm from going off
  • Fixing an issue where some users were unable to login to Find my iPhone
  • Fixing an issue that prevented some manual iCloud Backups from completing
  • Addressing an issue where using the iPad keyboard could unintentionally trigger text selection mode
  • Improved keyboard responsiveness when using Quick Reply
  • Improved punctuation input on the 10-key Chinese (Pinyin & Stroke) keyboards with new expanded view of punctuation symbols and better predictions
  • Fixing an issue on Cyrillic keyboards where caps lock would be enabled when typing in URL or email fields

Accessibility improvements

  • Fixing issues with VoiceOver when using Camera face detection
  • Adding support for VoiceOver to wake up the screen
  • Adding support for VoiceOver to invoke app switcher with 3D Touch gesture
  • Fixing an issue with Guided Access when trying to end phone calls
  • Improved functionality for Switch Control users when using 3D Touch
  • Fixing an issue with speech rate of Speak Screen
  • Siri support for Arabic (Saudi Arabia, United Arab Emirates)

For information on the security content of this update, please visit this website:
https://support.apple.com/HT201222

How to use Arrays in Swift

Swift has its own built-in Array class. Although it’s bridged to NSArray, a Swift Array is something entirely different. Swift Arrays do not work with such trusted methods like addObject anymore.

Because I will probably have forgotten most of this by tomorrow, here’s a quick lowdown on how to play with Swift Arrays.

// create an empty array [type in square brackets]
var emptyArray = [String]()

// create an array with inferred values
var array = ["one", "two", "three"]

// loop through the array
for value in array {
    print("The value is \(value)")
}

// insert new value at a particular index
array.insert("two and a half", atIndex: 2)
var value = array[2]

// add something to the end of our array
array += ["new value"]
// or
array.append("another value")

// grab the last entry in our array
value = array.last!

// add another array
let anotherArray = ["four", "five", "six"]
array += anotherArray

// how many entries does our array have?
print(array.count)

// remove the last item in the array
array.removeLast()

// remove an item at a particular index
array.removeAtIndex(3)

Swift Arrays and Lazy Initialisation

Swift Arrays don’t like being initialised with a closure. God only knows why. We can however solve this puzzle with a simple function that returns our initialised values:

    lazy var data: [String] = self.initData()
    
    func initData() -> [String] {
        
        // initialize data array and return it
        var data = [String]()
        
        let formatter = NSNumberFormatter()
        formatter.numberStyle = .SpellOutStyle
        
        for var i = 0; i < 30; i++ {
            let number = NSNumber.init(integer: i)
            data.append(formatter.stringFromNumber(number)!)
        }
        return data
    }

For everything else about Swift Arrays, check out the Collection Types section of The Swift Programming Language:

How to fix “no rule to process file” warning in Xcode

The other day Xcode was trying to tell me that it had “no rule to process a file”. It was a simple readme file that I had created on GitHub and subsequently pulled into my working copy.

Even though the project compiled fine, if there is a warning we can eliminate, we definitely should. Turns out it’s easy fix this problem.

Click on your project in the Project Navigator (the blue item on the left where you see all your project files), then head over to Build Phases. You’ll find a section called Compiled Sources. Expand it to see a selection of files.

Select the one Xcode is complaining about and remove it by clicking the little minus sign at the bottom.

Screen Shot 2015-12-09 at 12.51.27

Build the project again and see that warning disappear for good.

How to build WordPress for iOS (2015)

WordPress-LogoThe WordPress for iOS project has changed and grown over the years. While it’s still Open Source, it’s not as easy as “checkout and deploy” anymore. You may encounter several pitfalls along the way.

At the time of writing, the current version of WordPress for iOS is 5.7, with 5.8 just around the corner. In this article I’ll show you how to build the project and deploy it on your own devices.

Continue reading

How to write a Custom Initialiser in Swift

I’m used to initialising my Objective-C objects with a custom initialiser. It’s a trick I’ve learnt from the legendary Simon Allardice back in the day. It works a treat every time!

Turns out it’s a Cocoa and Cocoa Touch design pattern, and the principle can be applied similarly in Swift, with a couple of important differences.

Or shall I say pitfalls?

Continue reading