Image animations on Watchkit sdk

The aim of this post is just showing how to do a simple image animation. I have been investigating if it was possible to make animations in Watchkit using CALayer class, as in normal iPhone and iPad apps, but it has not been possible. The possibilities that provides UI in the Watchkit are very very poor, I figure it out that apple guys are fixing that.

The base project

For setup the base project just follow the instructions from a previous post. Add in the main scene the image that will hold the animation.

Image animations on Watchkit

Link the image to an IBOutlet.

IBOUTLETImage animations on Watchkit

 

Now we will add the images used for the animation:

Image animations on Watchkit

Add the following lines to the watch kit interface view controller:

And Finally run the target:

Image animations on Watchkit

If the instructions were followed you should have to see something like this:

Image animations on Watchkit

 

If you had any doubt about what I have explained, you can find this project on Github.

Communication between Apple Watch and iPhone app

The aim of this post is to show a possible mechanism for implementing the communication between Apple Watch and iPhone app. After trying several methods: CoreData, NSKeyedUnarchiver and NSUserDefaults, I have decided to chose NSUserDefault because it was the most simplified one. I would recommend the other two just in case that you wanted to share bigs amounts of data.

The application that I am going to create will show in the watch screen the value of a counter, that is increased (or decreased) from the iPhone app. Before continuing, be sure that you are working with Xcode 6.2, otherwise you will not be able to create a watchkit extension.

Create a new Single View project

Create a new Single View Application project:

Communication between Apple Watch and iPhone app

Communication between Apple Watch and iPhone app

 

Go to project properties>Capabilites>App Group, turn it on and create a new group:

Communication between Apple Watch and iPhone app

 

In the iPhone app storyboard we will add another label and a stepper:

Communication between Apple Watch and iPhone app

 

Add an action method for the stepper and leave it in blank by the moment, we will fill it later.

Communication between Apple Watch and iPhone app

The watch kit extension

Go to File>New>Target…>iOS>Apple Watch

Communication between Apple Watch and iPhone app

Communication between Apple Watch and iPhone app

 

And go to project properties, select the target extension (E), select Capabilities, turn on App Groups and select the group that you created before:

Communication between Apple Watch and iPhone app

 

Now we will place the label in the watch kit storyboard that will show the value of the counter.

Communication between Apple Watch and iPhone app

 The common data

I have created a new group on top the project hierarchy and added a module called Model, that is where I will place the common data.

Communication between Apple Watch and iPhone app

 

VERY IMPORTANT: Be sure that those files are assigned for both targets:

Communication between Apple Watch and iPhone app

 

Now is time to fill the stepper action method that we have created previously and do not forget the #import:

Now let make an stop in the path for checking if stepper is updating the counter. Select watchkitCounter target and any iPhone simulator and press the play button for running the app in the iPhone:

Communication between Apple Watch and iPhone app

 

Click on the stepper for assuring that label updates with counter value:

Communication between Apple Watch and iPhone app

 

Next step is to fill in the watch kit extension interface controller:

 

I have initialised the label when the form is loaded and program a timer for pulling the data. Finally build and execute the target watchkitCounter Watch App:

Communication between Apple Watch and iPhone app

 

Once the simulator is on you will see that the app is not running, this is normal. Be sure that apple watch simulator is also on screen, if does not go to iOS Simulator>Hardware>External Displays>Apple Watch (and re-run):

Communication between Apple Watch and iPhone app

 

Do not get nervous, if you see blank screen on watch kit simulator, on my computer (Mac-mini 16GB+SSD) takes almost 10 seconds for starting up. Once is on, you will see that watch-simulator refreshes the value of the counter, but the iPhone simulator does not start up the iPhone app, next step is select the iPhone app play with the stepper and observe how counter in the watch-simulator is being updated.

Communication between Apple Watch and iPhone app

 

And that is all!. In case that you had any doubt or something were not properly explained, you can download the demo project from here. I hope that this post would be useful and clarified your doubts, please feel free to send me any comment or amend.

 

A simplified framework creation tutorial

The aim of this post is provide a simplified framework creation tutorial. This post is targeted to those who want to create a static library (framework) in iOS and never did it before.

Getting started

Start off by creating a new Cocoa Touch Static Library project.

A simplified framework creation tutorial

 

 

A simplified framework creation tutorial

 

Next step is add some basic code to the library. This is the header file:

… And that the implementation:

 

A simplified framework creation tutorial

 

The framework (library) has not been generated yet, you will see the libSimplifiedFramework.a in red.

This is an important issue, before compiling select the target SimplifiedFramework and the device iOS Device. Otherwise it will appear the message that says that compilation is successful but the library binary will not be generated.

A simplified framework creation tutorial

 

For generating the library binary, go to Xcode menu Product>Archive

A simplified framework creation tutorial

Now you should have to see that library is in black, that means that library has been successfully created.

A simplified framework creation tutorial

Universal libraries

Reached to that point the library is generated, but for a concrete platform. The target now is generate a multi-platform library. For doing that you have to create a new target

A simplified framework creation tutorial

 

 

A simplified framework creation tutorial

 

 

A simplified framework creation tutorial

 

 

A simplified framework creation tutorial

 

 

Select universalSimplifiedFramework target and in its project options add a new build phase.

A simplified framework creation tutorial

 

Now is time to add the script that will generate the multiplatform library, this script is extracted from another article that explains with more detail how to create a library.

Replace in Step 1, SimplifiedFramework by your project name.

A simplified framework creation tutorial

 

For building just select Product>Archive from Xcode menu:

A simplified framework creation tutorial

 

And check in the logs if all was fine.

A simplified framework creation tutorial

Client app

So as to check if the proper if all the process was carried on well, we will create a new app that will use the library. For doing that we will add a new target:

Go to Xcode menu and select File > New > Target :

A simplified framework creation tutorial

 

 

A simplified framework creation tutorial

 

 

A simplified framework creation tutorial

 

 

Once the app has been created, it is time to setup the project options that the app is going to use the library:

 

A simplified framework creation tutorial

 

Select SimplifiedApp target and in General section, unfold Linked Frameworks and Libraries and select add:

A simplified framework creation tutorial

A simplified framework creation tutorial

 

The following code will call the function defined in the library. Place that code in the view controller class:

And run the app target for any device, in my case I have used iPhone6 device.

A simplified framework creation tutorial

As you can see in the last screen shot the client library app is calling a function from the library without major problems.

Conclusion

As I said at the beginning of the post, this is a simplified vision of how to create a library. From that point you can find very interesting articles about how to create frameworks in Xcode, like:

Ernesto GarcíaCreating a static Librfary in iOS Tutorial.

Sam DaviesHow to Create a Framework for iOS

 

Disable fullscreen mode on MPMoviePlayerController

The aim of this post is explain how to disable fullscreen mode  on MPMoviePlayerController. From the beginning you could think that there would be a flag to control that, but the reality is quite different. Since apple guys do not put hands on it, the rest we will have to deal with workarounds like this one.

The problem

My problem was that I wanted to embed a movie player in a scene, but movie player showed the fullscreen button mode. The aim of my scene was allow to play a movie in a part of the scene. Googling about this matter I found that this problem was known, but none of the solutions given worked fine to me, that’s why I want to report my personal solution.

The solution.

The solution consists in two independent actions. The first one is track all Movie player subviews, and once found the one that contains the fullscreen button view just hide it.

Add this chunk of code in the place where you initialise your player:

The call must be deferred because until the movie player is not presented, its sub collection of views is not built.

As movie player view is a tree of subviews, the unique I had to track this structure by using a recursive call:

A clarification, movieclip is an instance of MPMoviePlayerController. Since there is no identifier tag for the view I had to check its properties to know which view I had to hide. In my case was the view coordinates, it does not sound professional solution, but after days breaking stones I did not something better.

The second one is overwrite tap actions on movie player, because doble tap does a full screen. Also add the following chunk of code in the place where you initalise the movie player.

And its selector methods:

… for doing nothing.

If all was implemented as described, you should have to see that it has disappeared fullscreen icon and double tapping on video does not do anything at all.

Disable fullscreen mode on MPMoviePlayerController

Disable fullscreen mode on MPMoviePlayerController

This solution is also explained in Stackoverflow .

Location services problem in iOS 8

Location services problem in iOS 8

We will create an app that will fix the location services problem found in iOS 8. In the latest release iOS, some developers found that released apps that used location services stop working.

The sample project

Lets create a new  single view application  project.

Single view application template project for Location services problem in iOS 8

One of the key points for fixing the location services problem in iOS 8 is add one of the two new keys in YourApp-Info.plist file. Open the file as source code and paste the following chunk of xml code.

or

Info-plist file for fixing Location services problem in iOS 8

Then in the view controller class header: import Corelocation header; define that the class will implement CLLocationManager delegate protocol and define an attribute for handling the device location

In the view controller implementation class add the #define:

In the view ViewDidLoad method add the following snippet:

Add the method startUpdationLocation:

And finally implement the protocol method:

If all was set properly didUpdateLocations callback should be called, this is the test that you can succeed with the location services problem in iOS 8:

debugging didUpdateLocations for fixing Location services problem in iOS 8

On the target device

And finally last, but not least be sure that the location services are enabled for your app in the device. Select Settigs >Privacy > Location Services and be sure that your app has this services as Always or While Using.

Some times location services problem in iOS 8, is just that the settings of your app were not properly right.

IPphone_iOS_8_Location_Service maximize-your-iphones-battery-life-ios-8.w654

 

Your can find more information about that issue here.