amirrajan / rubymotion-applied

RubyMotion documentation provided by the community. Submit a pull request to the docs for a free one year indie subscription.
Apache License 2.0
49 stars 10 forks source link

AdMob and iOS HowTo #19

Open amirrajan opened 6 years ago

amirrajan commented 6 years ago

Tutorial/code snippets that show how to integrate Ad Mob into an iOS app.

Edit:

This tutorial has been turned into a template.

  1. First run motion repo to update your community templates.
  2. Then run motion create HelloAdMob --template=ios-admob-rewarded-ads
  3. Open the Rakefile, in there you should see a sample account_id and unit_id you can use to verify if ads work.
pedrovillarruel commented 6 years ago

Admob Implementation

The source code is not sufficient to get Admob setup. Read all information for proper implementation.

This is going to be a very basic explanation of how to integrate AdMob using reward video ads into iOS device running RubyMotion (RM). Reward video ads are ads that when played reward the user with whatever you set as an incentive for watching the video ad (this is my favorite).

Step 1: Import the Mobile Ads SDK CocoaPods (preferred) using command line The simplest way to import the SDK into an iOS project is with CocoaPods. Open your project's Gemfile and add this line to your app's target:

gem "motion-cocoapods"

Then from command line run: rake pod:install (if needed run: pod install update)

If you're new to CocoaPods , see their official documentation for info on how to create and use Podfiles.

Step2: In your project in Rakefile add the following code to your setup function.

this imports the SDK into the build

app.pods do
    pod 'Google-Mobile-Ads-SDK'
end

Step 3: In your project in the AppDelegate file add the following code to your application function. To allow videos to be preloaded, we recommend calling loadRequest: as early as possible (for example, in your app delegate's application:didFinishLaunchingWithOptions: method). pass your admob account info here, for now we use a test account and have this currently loading in game_home_scene.

GADRewardBasedVideoAd.sharedInstance().delegate = self
GADRewardBasedVideoAd.sharedInstance().loadRequest(GADRequest.request(), withAdUnitID: 'ca-app-pub-3940256099942544/1712485313')

Step 4: To allow videos to be preloaded, we recommend calling loadRequest: as early as possible (for example, in your app delegate's didMoveToView method (initializer) which is where I did it in my application.

GADRewardBasedVideoAd.sharedInstance().delegate = self
GADRewardBasedVideoAd.sharedInstance().loadRequest(GADRequest.request(), withAdUnitID: 'ca-app-pub-3940256099942544/1712485313')

Step 5: Setup all of your callback functions in the same scene which you initilized the loadRequest.

def rewardBasedVideoAd rewardBasedVideoAd, didRewardUserWithReward:reward
    puts "Reward received, giving user their reward here."
end
def rewardBasedVideoAdDidReceiveAd rewardBasedVideoAd
    puts "Reward based video ad is received."
end
def rewardBasedVideoAdDidOpen rewardBasedVideoAd
    puts "Opened reward based video ad."
end
def rewardBasedVideoAdDidStartPlaying rewardBasedVideoAd
    puts "Reward based video ad started playing."
end
def rewardBasedVideoAdDidClose rewardBasedVideoAd
    puts "Reward based video ad is closed."
end
def rewardBasedVideoAdWillLeaveApplication rewardBasedVideoAd
    puts "Reward based video ad will leave application."
end
def rewardBasedVideoAd rewardBasedVideoAd, didFailToLoadWithError:error
    puts "Reward based video ad failed to load."
end

Step 6: Setup when you want to call the ad to actually play. For example in this test application when you click/touch on the watch ad button:

def touchesBegan touches, withEvent: _

 #identify what was touched, and create a node
 node = nodeAtPoint(touches.allObjects.first.locationInNode(self))

 #check if node touched was what you wanted. In this case 'button_ad'
 if (node.name == 'button_ad')
    #check if video has loaded and ready to be viewed, if so lets show it
    if (GADRewardBasedVideoAd.sharedInstance().isReady() == true)
        GADRewardBasedVideoAd.sharedInstance.presentFromRootViewController(root.self)
    end
  end

end

Step 7: Make sure after the video ad is played successfully or depending when you want to have another ad ready to load that you call to initialize a new ad. This is done by calling the same methods as earlier in the didmoveToView method (initializer) .

GADRewardBasedVideoAd.sharedInstance().delegate = self
GADRewardBasedVideoAd.sharedInstance().loadRequest(GADRequest.request(), withAdUnitID: 'ca-app-pub-3940256099942544/1712485313')

Have fun setting this up!

Pedro V.