Website Links

Friday, 1 May 2015

iAd Advertisements Not Showing

iAd advertisements can take a while to show up even if your app has the status Live Ads and has been processed for the App Store. If your app has only been approved in the past week I wouldn't worry about whether your iAd integration worked or not (unless you have your fill rate set to 100% in the simulator and cannot see ads)! Here is a list of how long ads have taken to show up in our apps:

App Ad Type Time to show
iou free Banner ads 7 days
Angle Reader free Interstitial ads 3 days

Thursday, 16 April 2015

Integrating with iAd

This is pretty simple... Firstly go to iTunes and then to the Agreements, Tax and Banking section. Select and agree to the iAd App Network contract. Once you have done that, go ahead and open your Xcode project. Go to your project, and in the General section go down to Linked Frameworks and Libraries, hit the '+'and find iAd.framework. The iAd framework comes installed so you don't even need to go and download it first!

iAd BannerView

Go to your view controller where you want to display ads, import iAd and set self.canDisplayBannerAds to true. This will automatically handle the adding of a banner view to your view and resize your view depending on the availability of ads. It will also hide the banner view when no ads can be fetched.

iAd Interstitials

This is a little bit more complicated than banner views but works well when you don't want to make space for banner views, and are commonly used in games. Once again, go to the view controller you want to have show interstitial views and import iAd. You will also need to make it implement ADInterstitialAdDelegate, and add the following code:


    var interstitialAd : ADInterstitialAd!
    var interstitialAdView : UIView!
    var closeButton : UIButton!
    
    override func viewDidLoad() 
    {
        super.viewDidLoad()

        // any viewDidLoad logic you have
        
        closeButton = UIButton.buttonWithType(UIButtonType.System)
                      as! UIButton
        closeButton.frame = CGRectMake(self.view.frame.width - 42,
                                       10, 32, 32)
        closeButton.layer.cornerRadius = 16

        // Note: I have an image asset named "Close"
        // You will need to add one for this to work
        var closeImage = UIImage(named: "Close")
        closeButton.backgroundColor = UIColor.whiteColor()
        closeButton.tintColor = UIColor.blackColor()
        closeButton.setImage(closeImage, 
                             forState: UIControlState.Normal)
        closeButton.addTarget(self, action: "closeAd:", 
                              forControlEvents: .TouchDown)
    }
    
    func closeAd(sender: UIButton) 
    {
        closeButton.removeFromSuperview()
        if ((interstitialAdView) != nil) {
            interstitialAdView.removeFromSuperview()
        }
    }
    
    // call whenever you want to show an ad
    func showAd() 
    {
        interstitialAd = ADInterstitialAd()
        interstitialAd.delegate = self
    }
    
    func interstitialAdDidLoad(interstitialAd: ADInterstitialAd!) 
    {
        interstitialAdView = UIView()
        interstitialAdView.frame = self.view.bounds
        self.view.addSubview(interstitialAdView)
        
        interstitialAd.presentInView(interstitialAdView)
        UIViewController.prepareInterstitialAds()
        
        interstitialAdView.addSubview(closeButton)
    }
    
    func interstitialAdDidUnload(interstitialAd: ADInterstitialAd!)
    {
        closeAd(closeButton)
    }
    
    // can do things with the error if you want to... I chose not to
    func interstitialAd(interstitialAd: ADInterstitialAd!,
                        didFailWithError error: NSError!) 
    {
        closeAd(closeButton)
    }




Alternatively, it may be worthwhile implementing this in another view controller that all your view controllers that show interstitial ads will extend.

Friday, 20 February 2015

Euler Angles

Euler angles allow you to specify any 3D rotation with just 3 values, these values are as follows:

Pitch

Up and down (i.e. looking up and down)

Yaw

Side to side (i.e. looking left and right)

Roll

Tilting (i.e. tilting your head from side to side)

References

Math for Game Developers - Mouse Control (Euler Angles) Video on YouTube

Wednesday, 18 February 2015

App Icons and iTunes Icons for iOS Apps

You can change what App Icon your iOS App is going to have through the assets catalog. You will need to create quite a few different sizes and I've found the best way to do this is to make the biggest one and resize it accordingly, but where do all these different sizes go? First you will need to enable all the sizes you need, by clicking on the AppIcon asset and finding the following pane:



You can enable/disable these as appropriate for your app, and you should end up with an asset catalog that looks like this:



The above image should tell you what image sizes you will need for your App icons, and where they need to be placed in the catalog.

References

Apple Developer

Saturday, 27 December 2014

Xcode - Our Experience

When using Xcode for the first time it can be a bit tricky. So here's some useful things that we have found out along the way when developing using Swift in Xcode 6.

Zooming

  • In iOS Simulator:
    • ⌘+1 for 100%
    • ⌘+2 for 75%
    • ⌘+3 for 50%
  • In storyboard:
    • Double click outside of view will toggle between zoomed in and zoomed out
    • There is a keyboard shortcut but it's less efficient

Adding tab to tabbed application

https://www.youtube.com/watch?v=NFTcR9H-OYQ
  • Add a view controller from the object library to the storyboard
  • Find the Tab Bar Item from the object library and add it to your new view controller
  • Select the Tab Bar Controller and hold down control and drag onto the new view controller
  • Select view controllers under relationship segue

Custom tab bar items

  • Click on the Images.xcassets folder
  • Right click and select import and navigate to the appropriate item
  • Go to the storyboard and click on the tab bar item you want to give a custom item
  • Set the image and title

Using table views

https://www.youtube.com/watch?v=war0gHL26ns

Adding layout constraints

  • Go to the storyboard
  • From the hierarchy of scenes:
    • Hold down control and click on the view that will constrain your view, i.e. to have a table view take up the entire view for different resolutions you would select the parent view
    • Drag down and select the view you want to constrain
    • Select the constraint type

Adding duplicate component

Hold down alt key and drag on existing component

Friday, 26 September 2014

The Recursion Checklist

Recursion isn't the hardest principle to understand, but often is not a way of thinking that comes most naturally for developers. Personally, I have struggled with recursion and try as I might to avoid it, sometimes it is the cleanest way! After using it a few times I have become more comfortable with it, and have developed a checklist of what to look for when writing a recursive function.

The Base Case

  • Prevents infinite recursion
  • Kind of similar to a while loop condition for when to stop looping

The Recursive Call

  • Moves towards the base case
  • The defined method is called from within itself

Famous Recursion

Words are great, but rarely help you understand a problem... So now let's take a look at a few common examples:
  • Fibonnaci
  • Trees

Fibonacci

Fibonacci is simply written using recursion but also is incredibly inefficient this way. Nonetheless, consider the following:

  public int fibonacci(int n)  {
     if(n == 0) {
        return 0;
     } else if(n == 1) {
        return 1;
     } else {
        return fibonacci(n - 1) + fibonacci(n - 2);
     }
  }

Fibonacci is a classic algorithm that makes 2 recursive function calls and is exponentially large. Recursion is terminated when n is either 0 or 1. However, although it is most simply written recursively it can be much more efficiently written iteratively, as follows:

  public int fibbonaci(int n) {
     int current = 0; 
     int next = 1; 

     for (int i = 0; i < n; i++) {
        int temp = current + next;
        current = next;
        next = temp;
     }

     return current;
  }


Finding the root of a tree node

Let's assume we are given the following class to represent a node of a tree, and that the values in the tree are populated appropriately:

  public class Node {
     private Node _parent;
     private IList _children;

     public Node getRoot() {
        if (_parent == null) {
           return this;
        }

        return _parent.getRoot()
     }

     public Node getParent() {
        return _parent;
     }
  }

The getRoot method is recursive as it calls itself, and will keep calling itself until it gets to the root node. The root node is where recursion terminates due to the check if the parent is null. As the root node is the value returned in the base case, this value propagates all the way up through the recursive calls to the entry point of the method, and thus this is a simple way to get the root of the tree. Alternatively, this same method can be written without recursion and this may be preferable for the reasons described later:

  public Node getRoot() {
     Node currentNode = this;
     
     while(currentNode.getParent() != null) {
        currentNode = currentNode.getParent();
     }

     return currentNode;
  }


Iteration or Recursion?

When deciding whether to use recursion there are a few things you should consider:
  1. How much data are you working with?
  2. Is there a simple iterative solution?

How much data are you working with?

This is a very important factor, as if you have too much data and thus very deep recursion, a stack overflow will occur and your program will throw an exception. This is when your program tries to use more space that is available on the call stack. The size of the call stack varies depending on a variety of factors such as machine architecture and programming language.

Some languages, such as Scheme, are optimised to allow for infinite tail recursion, and implement tail call optimisation. Unfortunately, in most languages such as C#, Python and Java, this is not the case. Tail recursion is when the return result comes entirely from the recursive call. For example, our getRoot call is tail recursive as the return result relies entirely on the recursively called function or the base condition. Whereas, the following function is not tail recursion as it relies on the multiplication of the parameter n times the value of the recursive call, and thus the recursive call must be evaluated before the return value can be.

  public int fact(int n) {
     if (n == 0) {
        return 1;
     } else {
        return n * fact(n - 1);
     }
  }


Is there a simple iterative solution?

Often recursion is the simplest way to solve a problem, and this is when it is most commonly used. It is important when choosing whether to use a recursive or iterative approach, to decide whether you can simply implement a solution using an iterative approach. In general, an iterative approach is more efficient as there is less overhead involved.

References

http://stackoverflow.com/questions/3021/what-is-recursion-and-when-should-i-use-it

Thursday, 18 September 2014

C# vs Java - Access Modifiers

C#

There are 5 access modifiers in C#:
  • public
  • private
  • protected
  • internal
  • protected internal
These are used in the declaration of classes, structs and members of classes or structs. The public access modifier allows access from anywhere, and should be used sparingly. It is best to use public only where a member will need to be accessed by a class that is not part of the same assembly or extends the applicable class. The public modifier is common in the declaration of classes or properties, as classes are often externally used and properties define getters and setters and thus aid encapsulation.

The private modifier is good for encapsulation, as it hides anything that should be inaccessible by another class. It is the most restrictive of access modifiers as only code in the same struct or class can access it, and is the default for class or struct members. The private modifier is commonly used on fields, but is also used on methods that may be called within public methods but shouldn't be accessible otherwise. By using the private modifier where possible we can help reduce the chance of other programmers calling the wrong method when they are using our class. It is important to note a class cannot be private unless it is nested within another class.

The protected modifier means that the type or member can only be accessed by code in the same class, or classes that inherit from it. Therefore, it is important to give careful thought to members you are considering declaring protected as if you release your code as a library, and other classes extend your class, and then you want to make a change to the member, your change may break code that uses your library. The protected modifier can be useful when declaring an abstract class, to ensure that classes which extend this abstract class have access to members which should be common amongst all classes that extend the abstract class.

The internal modifier is the default for classes and structs in C#, and is similar to package-visibility in Java. This modifier gives access to any code in the same project, and is commonly used to confine the use of a class to a project in a solution. Generally, a class' members have lower or equal accessibility to the class, but sometimes when an internal class extends a public class it may have public members that it has overridden from the base class.

Additionally, there is the protected internal access modifier. This basically means it can be accessed from code where it could be accessed from if the member was declared protected OR internal. Finally, it is important to note that interface members cannot be declared with an access modifier, this makes sense as the purpose of an interface is to allow another class to access another's members. Interfaces can either be internal or public.

Java

Java is quite similar to C#, so first we will outline the similarities in access modifiers:
  • public behaves the same way in both C# and Java
  • private behaves the same way in both C# and Java.
  • protected behaves the same way in both C# and Java.
However, there are some differences. Firstly, by default classes or members of a class have package-visibility in Java which means that they can be accessed by code in the same package. Whereas, in C# default accessibility for a member is private and the default accessibility for a class in internal. Package-visibility is similar to internal visibility, except that in Java the package is represented physically by a directory and only code in the same directory can access something with package visibility. Whereas, with internal members any code in a project (not necessarily the same directory) can access it.

Finally, in Java there is no way to explicitly set package-visibility, and no way to allow for either protected or package-visibility access for the same member (as you do with protected internal in C#). Therefore, you have a lower level of control over the accessibility of classes and their members than you do in C#. C# is easier to modularize your code in, by using internal and having different projects in the same solution, and therefore I find it better suited for commercial work as it is easier to maintain the structure of a solution.