Skip to content

Commit

Permalink
Merge pull request facebook#29 from xamarin/facebook-audience-getting…
Browse files Browse the repository at this point in the history
…started

Update getting started with more information
  • Loading branch information
bholmes committed Dec 20, 2016
2 parents 8c5f6a7 + 52928a0 commit 0fd6a62
Showing 1 changed file with 150 additions and 119 deletions.
269 changes: 150 additions & 119 deletions Facebook.AudienceNetwork.iOS/component/GettingStarted.md
Expand Up @@ -2,7 +2,7 @@

### Whitelist Facebook Servers for Network Requests

If you compile your app with iOS SDK 9.0, you will be affected by App Transport Security. Currently, you will need to whitelist Facebook domains in your app by adding the following to your application's plist:
If you compile your app with iOS SDK 9.0 or higher, you will be affected by App Transport Security. Currently, you will need to whitelist Facebook domains in your app by adding the following to your application's plist:

```
<key>NSAppTransportSecurity</key>
Expand Down Expand Up @@ -40,7 +40,7 @@ So you can use this API, you will need to create one or more Placements IDs in y

[gettingstarted]: https://developers.facebook.com/docs/audience-network/getting-started

## Banner Ad
## Banner Ads

This is a simple way to add a Banner Ad to your app:

Expand All @@ -52,67 +52,96 @@ using Facebook.AudienceNetwork;

public partial class FBAudienceNetworkSampleViewController : UIViewController, IAdViewDelegate
{
// Generate your own Placement ID on the Facebook app settings
const string YourPlacementId = "YOUR_PLACEMENT_ID";
AdView adView;
// Generate your own Placement ID on the Facebook app settings
const string YourPlacementId = "YOUR_PLACEMENT_ID";
AdView adView;

public override void ViewDidLoad ()
{
base.ViewDidLoad ();
public override void ViewDidLoad ()
{
base.ViewDidLoad ();

// Create a banner's ad view with a unique placement ID (generate your own on the Facebook app settings).
// Use different ID for each ad placement in your app.
adView = new AdView (YourPlacementId, AdSizes.BannerHeight50, this) {
Delegate = this
};

// When testing on a device, add its hashed ID to force test ads.
// The hash ID is printed to console when running on a device.
//AdSettings.AddTestDevice ("THE HASHED ID AS PRINTED TO CONSOLE");
// Create a banner's ad view with a unique placement ID (generate your own on the Facebook app settings).
// Use different ID for each ad placement in your app.
adView = new AdView (YourPlacementId, AdSizes.BannerHeight50, this) {
Delegate = this
};

// When testing on a device, add its hashed ID to force test ads.
// The hash ID is printed to console when running on a device.
// AdSettings.AddTestDevice ("THE HASHED ID AS PRINTED TO CONSOLE");
// Initiate a request to load an ad.
adView.LoadAd ();

// Reposition the adView to the bottom of the screen
var viewSize = View.Bounds.Size;
var bottomAlignedY = viewSize.Height - AdSizes.BannerHeight50.Size.Height;

adView.Frame = new CGRect (0, bottomAlignedY, viewSize.Width, AdSizes.BannerHeight50.Size.Height);

// Add adView to the view hierarchy.
View.AddSubview (adView);
}

#region IAdViewDelegate

[Export ("adViewDidClick:")]
public void AdViewDidClick (AdView adView)
{
// Handle when the banner is clicked
}
// Initiate a request to load an ad.
adView.LoadAd ();

// Reposition the adView to the bottom of the screen
var viewSize = View.Bounds.Size;
var bottomAlignedY = viewSize.Height - AdSizes.BannerHeight50.Size.Height;

adView.Frame = new CGRect (0, bottomAlignedY, viewSize.Width, AdSizes.BannerHeight50.Size.Height);

// Add adView to the view hierarchy.
View.AddSubview (adView);
}

#region IAdViewDelegate

[Export ("adViewDidClick:")]
public void AdViewDidClick (AdView adView)
{
// Handle when the banner is clicked
}

[Export ("adViewDidFinishHandlingClick:")]
public void AdViewDidFinishHandlingClick (AdView adView)
{
// Handle when the window that is opened by the click is closed);
}

[Export ("adViewDidLoad:")]
public void AdViewDidLoad (AdView adView)
{
// Handle when the ad on the banner is loaded
}

[Export ("adView:didFailWithError:")]
public void AdViewDidFail (AdView adView, NSError error)
{
// Handle if the ad is not loaded correctly
}

#endregion
}

[Export ("adViewDidFinishHandlingClick:")]
public void AdViewDidFinishHandlingClick (AdView adView)
{
// Handle when the window that is opened by the click is closed);
}
```

[Export ("adViewDidLoad:")]
public void AdViewDidLoad (AdView adView)
{
// Handle when the ad on the banner is loaded
}
### Banner Ads on iPad
If you are building your app for iPad, consider using the AdSizes.Height90Banner size instead. In all cases, the banner width is flexible with a minimum of 320px.

[Export ("adView:didFailWithError:")]
public void AdViewDidFail (AdView adView, NSError error)
{
// Handle if the ad is not loaded correctly
}

#endregion

}
### 300x250 Banner Ad Size
The Audience Network also support 300x250 ad size. Configure the AdView with the following ad size: AdSizes.HeightRectangle;

```csharp
public override void ViewDidLoad ()
{
base.ViewDidLoad ();

// Create a banner's ad view with a unique placement ID (generate your own on the Facebook app settings).
// Use different ID for each ad placement in your app.#
adView = new AdView (YourPlacementId, AdSizes.RectangleHeight250, this) {
Delegate = this
};
adView.LoadAd ();

// Reposition the adView to the bottom of the screen
var viewSize = View.Bounds.Size;
var bottomAlignedY = viewSize.Height - AdSizes.RectangleHeight250.Size.Height;
var bottomAlignedX = viewSize.Width / 2 - 150;
adView.Frame = new CGRect (bottomAlignedX, bottomAlignedY, AdSizes.RectagleHeight250.Size.Width, AdSizes.RectangleHeight250.Size.Height);

// Add adView to the view hierarchy.
View.AddSubview (adView);
}
```

## Interstitial Ad
Expand All @@ -127,72 +156,74 @@ using Facebook.AudienceNetwork;

public partial class FBAudienceNetworkSampleViewController : UIViewController, IInterstitialAdDelegate
{
// Generate your own Placement ID on the Facebook app settings
const string YourPlacementId = "YOUR_PLACEMENT_ID";
InterstitialAd interstitialAd;

public override void ViewDidLoad ()
{
base.ViewDidLoad ();

// Create a banner's ad view with a unique placement ID (generate your own on the Facebook app settings).
// Use different ID for each ad placement in your app.
interstitialAd = new InterstitialAd (YourPlacementId) {
Delegate = this
};

// When testing on a device, add its hashed ID to force test ads.
// The hash ID is printed to console when running on a device.
// AdSettings.AddTestDevice ("THE HASHED ID AS PRINTED TO CONSOLE");
// Initiate a request to load an ad.
interstitialAd.LoadAd ();

// Verify if the ad is ready to be shown, if not, you will need to check later somehow (with a button, timer, delegate, etc.)
if (interstitialAd.IsAdValid) {
// Ad is ready, present it!
interstitialAd.ShowAdFromRootViewController (this);
}
}

#region IInterstitialAdDelegate

[Export ("interstitialAdDidLoad:")]
public void InterstitialAdDidLoad (InterstitialAd interstitialAd)
{
// Handle when the ad is loaded and ready to be shown
if (interstitialAd.IsAdValid) {
// Ad is ready, present it!
interstitialAd.ShowAdFromRootViewController (this);
}
// Generate your own Placement ID on the Facebook app settings
const string YourPlacementId = "YOUR_PLACEMENT_ID";
InterstitialAd interstitialAd;

public override void ViewDidLoad ()
{
base.ViewDidLoad ();

// Create a banner's ad view with a unique placement ID (generate your own on the Facebook app settings).
// Use different ID for each ad placement in your app.
interstitialAd = new InterstitialAd (YourPlacementId) {
Delegate = this
};

// When testing on a device, add its hashed ID to force test ads.
// The hash ID is printed to console when running on a device.
//AdSettings.AddTestDevice ("THE HASHED ID AS PRINTED TO CONSOLE");
// Initiate a request to load an ad.
interstitialAd.LoadAd ();

// Verify if the ad is ready to be shown, if not, you will need to check later somehow (with a button, timer, delegate, etc.)
if (interstitialAd.IsAdValid) {
// Ad is ready, present it!
interstitialAd.ShowAdFromRootViewController (this);
}
}

[Export ("interstitialAd:didFailWithError:")]
public void IntersitialDidFail (InterstitialAd interstitialAd, NSError error)
{
// Handle if the ad is not loaded correctly
}
#region IInterstitialAdDelegate

[Export ("interstitialAdDidClick:")]
public void InterstitialAdDidClick (InterstitialAd interstitialAd)
{
// Handle when the user tap the ad
[Export ("interstitialAdDidLoad:")]
public void InterstitialAdDidLoad (InterstitialAd interstitialAd)
{
// Handle when the ad is loaded and ready to be shown
if (interstitialAd.IsAdValid) {
// Ad is ready, present it!
interstitialAd.ShowAdFromRootViewController (this);
}

[Export ("interstitialAdDidClose:")]
public void InterstitialAdDidClose (InterstitialAd interstitialAd)
{
// Handle when the user close the ad
}

[Export ("interstitialAdWillClose:")]
public void InterstitialAdWillClose (InterstitialAd interstitialAd)
{
// Handle before the ad is closed
}

#endregion

}

[Export ("interstitialAd:didFailWithError:")]
public void IntersitialDidFail (InterstitialAd interstitialAd, NSError error)
{
// Handle if the ad is not loaded correctly
}

[Export ("interstitialAdDidClick:")]
public void InterstitialAdDidClick (InterstitialAd interstitialAd)
{
// Handle when the user tap the ad
}

[Export ("interstitialAdDidClose:")]
public void InterstitialAdDidClose (InterstitialAd interstitialAd)
{
// Handle when the user close the ad
}

[Export ("interstitialAdWillClose:")]
public void InterstitialAdWillClose (InterstitialAd interstitialAd)
{
// Handle before the ad is closed
}

#endregion
}

```
```

### Running Ads on Test Device
When running on the simulator, test ads will be shown by default. To enable test ads on a device, add the following line of code before loading an ad: AdSettings.AddTestDevice("HASHED ID");. Use the hashed ID that is printed to the console when you first make a request to load an ad on a device. To see ads on your test device, you must have the Facebook app installed and have logged in within the last 30 days. Also check to make sure you haven't opted out of ads on your device.

0 comments on commit 0fd6a62

Please sign in to comment.