Saturday, April 30, 2011

Setup Windows Phone 7 apps with Smaato ads for Microsoft XNA framework

You might be asking yourself why you would choose smaato to monetize with in-app ads.
Firstly, smaato supports many countries and it has the SOMA SDK that is compatible with XNA.
Secondly, it allows for transfering of ad revenue through http://www.paypal.com/.

Note: this tutorial is for Windows Phone 7 XNA framework and not for Silverlight framework.



1) Sign up for a smaato account at http://www.smaato.com/


2) Generate AdSpace under the tab My Sites & Apps

3) Select - App (Windows Phone 7)
Note: This will generate your PublisherID and AdSpaceID -- these are very important and will be required as identifiers for you and your app.


4) Download the SOMA SDK for Windows Phone 7


5) From the SOMA SDK zip file place the SOMAWP7.dll and SOMAWP7.pdb files into your Windows Phone 7 project where the projectname.csproj file is located.


6) In your Windows Phone 7 project Right click on References in your Solution Explorer and ad SOMAWP7.DLL from Add Reference Browse tab and Microsoft.Phone from Add Reference .Net tab
























7) Create a 480x79 png file called sampleAd.png in the project content folder and add sampleAd.png into the project's Content area inside the Solution Explorer.


8) In your project.cs file place the following line of code at the top your file

using Microsoft.Phone.Tasks;
using System.IO.IsolatedStorage;
using SOMAWP7;
 

9) In your project class that inherits the XNA framework place the following code

namespace Project
{    public class Project1 : Microsoft.Xna.Framework.Game   
    {      
       SomaAd somaAd;
       Texture2D textureSomaAd;
       SpriteBatch spriteBatch;


10) Initialize SOMA SDK using the AdSpaceID and PublisherID

protected override void Initialize()
{ 
  //Set up SomaAd to get ads
  somaAd = new SomaAd(); 
  somaAd.Adspace = 12345678;
  somaAd.Pub = 12345678; 
 
  //gets user location from device
  somaAd.LocationUseOK = true;

  //information about the app running 
  somaAd.Kws = "games, windows phone, entertainment"; 
  //search string for ads to show
  somaAd.Qs = "games, windows phone, entertainment, fun, exciting";

  //Ask the user this informaton about them 
  //somaAd.Age = 30; 
  //somaAd.Gender = "m";  
  //By default, the SDK sets this to the current location 
  //somaAd.City = "Fremont"; 
  //somaAd.State = "Ca"; 
  //somaAd.Zip = "94539"; 
  //somaAd.Country = "United States";
  //somaAd.Countrycode = "us"; 
 
  //By default, adInterval will deliver ads after 60 seconds.  
  //somaAd.adInterval = 60000;
 
  somaAd.GetAd();
  base.Initialize();
}


11) Load content into app for SOMA

protected override void LoadContent()
{
  // Create a new SpriteBatch, which can be used to draw textures.
  spriteBatch = new SpriteBatch(GraphicsDevice);
  textureSomaAd = null;
  // setup touch
  TouchPanel.EnabledGestures = GestureType.Tap;

12) Make sure to Dispose SOMA with the UnloadContent function

protected override void UnloadContent()
{
  somaAd.Dispose();
}


13) Check for screen touch in the SOMA image. Get the file name of the SOMA image and load up browser with link if touch detected. 

string currentAdImageFileName = "";
protected override void Update(GameTime gameTime)
{

  // if the back button is pressed then go back to the main menu 
  if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
  {
    Exit();
  } 

  Vector2 somaAdPosition = new Vector2(0, GraphicsDevice.PresentationParameters.BackBufferHeight - 80);
  Vector2 somaAdSize = new Vector2(GraphicsDevice.PresentationParameters.BackBufferWidth, 80);
  while (TouchPanel.IsGestureAvailable)
  {
    GestureSample gestureSample = TouchPanel.ReadGesture();
    if (gestureSample.GestureType == GestureType.Tap && somaAd.Uri != "" && textureSomaAd != null)
    {
      Vector2 touchPosition = gestureSample.Position;

      if (touchPosition.X >= 0 &&
        touchPosition.X < somaAdSize.X &&
        touchPosition.Y >= somaAdPosition.Y &&
        touchPosition.Y < (somaAdPosition.Y + somaAdSize.Y))
      {
        WebBrowserTask webBrowserTask = new WebBrowserTask();
        webBrowserTask.URL = somaAd.Uri;
        webBrowserTask.Show();
      }
    }
  }

    // if there is a new ad, get it from Isolated Storage and  show it
  if (somaAd.Status == "success" && somaAd.AdImageFileName != null && somaAd.ImageOK)
  {
    try
    {
      if (currentAdImageFileName != somaAd.AdImageFileName)
      {
        currentAdImageFileName = somaAd.AdImageFileName;
        IsolatedStorageFile myIsoStore = IsolatedStorageFile.GetUserStoreForApplication();
        IsolatedStorageFileStream myAd = new IsolatedStorageFileStream(somaAd.AdImageFileName, FileMode.Open, myIsoStore);
        textureSomaAd = this.Content.Load<Texture2D>("sampleAd");
        textureSomaAd = Texture2D.FromStream(this.GraphicsDevice, myAd);

        myAd.Close();
        myAd.Dispose();
        myIsoStore.Dispose();
      }
    }
    catch (IsolatedStorageException ise)
    {
      string message = ise.Message;
    }
  }
  base.Update(gameTime);
}


14) Draw the texture of the SOMA image
protected override void Draw(GameTime gameTime)
{
  // draw ad
  //cubeToDraw.RenderToDevice(GraphicsDevice);
  if (textureSomaAd != null)
  {
    this.spriteBatch.Begin();
    this.spriteBatch.Draw(textureSomaAd, new Rectangle(0, GraphicsDevice.PresentationParameters.BackBufferHeight - 80, GraphicsDevice.PresentationParameters.BackBufferWidth, 80), Color.White);
    this.spriteBatch.End();
  }
  base.Draw(gameTime);
}


15) Create your app on Microsoft App Hub and submit it.

16) Go to http://www.smaato.com/ and click on Add New App. For the Download URL field copy and paste the Deep link example from your App Hub app information page.



17) Something to be aware of is that your phone must have 3G or a WIFI connection to the internet for your ads to show up.
Note: when you are debugging your app and the phone is connected to Visual Studio 2010 Express for Windows Phone the ads do not show up even with a working internet connection on the phone. You must disconnect the phone from the computer in order for the ads to appear.

Written by: Paul

3 comments:

  1. This comment has been removed by the author.

    ReplyDelete
  2. Thank you for this post. Very usefull.

    ReplyDelete
  3. An upgrade for Sam: we're really certain that FR is NOT bringing on the information utilization. (It would seem that the offender may have been some website pages left open on the browser.) I additionally straightened out the touch about FR utilizing 3g vs Wifi - I misjudged the At&t tech individual. She was alluding to information for requisition redesigns, not normal client utilization.Android developers // Android application development // mobile app developers

    ReplyDelete