Embedding ads in applications is a good way to profit.
Here are two ways to mainstream. IAd, Admob
First mark a very detailed PDF. http://pan.baidu.com/share/link?shareid=1656439633&uk=1394536315&fid=406566606116897
One. Iad
1. Need to join Iad.framework
2. Add the following code to the. h file
#import <UIKit/UIKit.h> #import <iAd/iAd.h>
3. Add the following code to the. m file
#import "ViewController.h" @interface Viewcontroller () @property (Nonatomic,strong) Adbannerview *adview; @end @implementation Viewcontroller-(void) viewdidload {[Super viewdidload]; Self.adview = [[Adbannerview alloc]initwithframe:cgrectmake (0, 64, 320, 50)]; Self.adView.requiredContentSizeIdentifiers = [Nsset setwithobject:adbannercontentsizeidentifierportrait]; Self.adView.currentContentSizeIdentifier = adbannercontentsizeidentifierportrait; Self.adView.delegate = self; [Self.view AddSubview:self.adView]; }-(void) Bannerviewwillloadad: (Adbannerview *) banner{NSLog (@ "Bannerviewwillloadad"); }-(void) Bannerviewdidloadad: (Adbannerview *) banner {NSLog (@ "Bannerviewdidloadad"); }-(void) Bannerview: (Adbannerview *) banner didfailtoreceiveadwitherror: (NSERROR *) error {NSLog (@ "didfailtorecei Veadwitherror "); }
:
Two. Admob
1. Need to include third party documents, and the following framework
2.
#define ADID @ "xxxxxxx" //Set a own global ID
3. Add the following code to the. h file
#import <UIKit/UIKit.h> #import "GADBannerView.h" @interface Admobdefaultviewcontroller: Uiviewcontroller { gadbannerview *adview; }
3. Add the following code to the. m file
-(void) viewdidload { [super viewdidload]; Additional setup after loading the view, typically from a nib. Create A view of the standard size in the bottom of the screen. ADView = [[Gadbannerview alloc] initwithframe:cgrectmake (0.0,self.view.frame.size.height-gad_size_ 320x50.height,gad_size_320x50.width,gad_size_320x50.height)]; Adview.adunitid = adid;//Call id adview.rootviewcontroller = self; Adview.backgroundcolor = [Uicolor yellowcolor]; [Self.view Addsubview:adview]; [ADView loadrequest:[gadrequest request]; }
3. AdMob Interstitial ads
. h File Codes
#import <UIKit/UIKit.h> #import "GADInterstitial.h" #import "GADInterstitialDelegate.h" @ Interface interadmobviewcontroller:uiviewcontroller<gadinterstitialdelegate> @property (nonatomic, Retain) gadinterstitial *interstitial; @end
. m file Codes
-(void) viewdidload { [super viewdidload]; Self.interstitial = [[Gadinterstitial alloc] init]; Self.interstitial.delegate = self; Self.interstitial.adUnitID = ADID; [Self.interstitial loadrequest: [self createrequest]; } -(Gadrequest *) createrequest { gadrequest *request = [gadrequest request]; Make the request for a test ad. Put in a identifier for the simulator as //well as any devices you want to receive test ads. request.testdevices = [Nsarray arraywithobjects: //Todo:add your Device/simulator test identifiers here. They was //printed to the console when the app is launched. Nil Nil]; return request; } -(void) Interstitialdidreceivead: (gadinterstitial *) interstitial { [interstitial Presentfromrootviewcontroller:self]; }