Branded Mini-Games Support
  • Welcome
  • Branded Mini-Games
    • Create a campaign
      • Method 1
      • Method 2
      • Campaign Types
        • Website
        • Mobile App
        • Playable ads
        • Brand Hosting
    • Game Settings
      • Game Customization: Easy Mode
      • Game Customization: Pro Mode
        • Other Features
      • How to Modify Graphics
      • Configurations
        • Minimum Game Score Setting
        • Using Hearts (Game Life)
      • Activating the prepared game
      • Instant Win
    • Campaign Settings
      • Campaign Profiles and SEO
      • Campaign URL
      • CNAME
      • How to Add a Game to Your Website
      • Deleting Games
    • Page Settings
      • Landing Page
      • Login
      • Results and Calls to Action
        • Leaderboard
        • No Leaderboard
        • Sharing
        • Call-to-Action
      • Changing Background Music and Favicon
      • Coupon
      • Skin Editor
    • Website
      • Website -> Leaderboard
      • Website -> UserID & Leaderboard
      • Website -> Participate & Win
      • Website -> UserID & Webhook
    • Mobile App Settings
      • Android App
      • iPhone iOS App
      • Mobile App -> Leaderboard
      • Mobile App -> UserID & Leaderboard
      • Mobile App -> UserID and Webhook
      • Close button
    • Play & Redeem
      • Select a Service Group
      • Choosing a Login Method & Service Start URL
      • Exchange game points to Shopping credits
      • Exchange game points to Coupons
      • Daily Missions settings for the game
    • UserID & Webhook
      • Website
      • Mobile App
      • Brand Hosting
    • Playable Ads
      • Customer acquisition through events
      • App installation
    • Data & Export
      • Participation
      • Sign up
      • Coupon Management
    • Google Analytics
    • Paid Plan & Publish
      • Publish
      • Subscribing to a Paid Plan
      • Purchase additional CPP
      • Invoice
      • Change credit card, update billing information, cancel service
    • Brand Hosting
      • Add game
      • Server setting test
      • Webhook and return values
  • Run actual Branded Mini-Games.
  • Terms of Use
    • Terms of Use
    • Privacy policy
    • Cookie Policy
    • Data Processor Agreement
    • GDPR
      • Understanding GDPR: Overview of Data Protection Regulation
      • Personal Data and Compliance: Key Considerations
      • GDPR Compliance in Studio Operations: User Sign-Up and Privacy Policies
      • Data Management: Retention, Analytics, and Reporting
    • Coupon Town - Terms and Conditions
  • Game Development Request
  • Deutsche / German
    • Marken-Mini-Spiele
  • Français / French
    • Mini-jeux de Marque
  • Italiano / Italian
    • Mini-Giochi Brandizzati
  • Español / Spanish
    • Mini-Juegos de Marca
  • Português / Portuguese
    • Mini-Jogos de Marca
  • Türkçe / Turkish
    • Markalı Mini Oyunlar
  • हिन्दी / Hindi
    • ब्रांडेड मिनी गेम्स
  • 한국어 / Korean
    • 브랜드 미니 게임
  • 日本語 / Japanese
    • ブランドミニゲーム
  • 简体中文 / Chinese
    • 品牌小游戏
  • Arabic (العربية)
    • ألعاب صغيرة ذات علامة تجارية
Powered by GitBook
On this page
  • Android
  • iPhone (iOS)
  1. Branded Mini-Games
  2. Mobile App Settings

Close button

When the branded game is running in the app's Webview, you can enable the Webview to close when the player presses the Close button.

The following content explains how to allow the Webview to close in the App when the Player presses the Close button on the result screen after the game.

Android

The following is an example that shows the Close button on the result screen after the game, and we provide the button. It is not something the brand's app developer needs to prepare.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Mini Game</title>
</head>
<body>
    <h1>Welcome to the Mini Game</h1>
    <!-- Close button -->
    <button onclick="closeWebView()">Close</button>

    <script type="text/javascript">
        function closeWebView() {
            // Call Android's closeWebView method.
            Android.closeWebView();
        }
    </script>
</body>
</html>

They are asking to set up a JavaScript interface in Android code. In MainActivity.java, add a JavaScript interface that will be called from the HTML page and will execute the action to close the WebView.

import android.os.Bundle;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.webkit.JavascriptInterface;
import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

    private WebView webView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        webView = findViewById(R.id.webview);
        webView.setWebViewClient(new WebViewClient()); // Set to open links in WebView
        WebSettings webSettings = webView.getSettings();
        webSettings.setJavaScriptEnabled(true); // Enable JavaScript

        // Add JavaScript interface
        webView.addJavascriptInterface(new WebAppInterface(), "Android");

        String url = "https://branded.mini-games.io/?php=landing@UserID_Data&campaign_no=0123456";
        webView.loadUrl(url); // Load URL
    }

    // JavaScript interface class
    public class WebAppInterface {
        @JavascriptInterface
        public void closeWebView() {
            // Logic to close the WebView
            finish(); // Finish the current activity
        }
    }
}

Overall operation explanation:

  1. When the user opens the corresponding HTML page as a Branded Game in the WebView, a "Close" button is displayed on the result screen after the game..

  2. When the user clicks the "Close" button, JavaScript calls Android.closeWebView().

  3. This call connects to Android's closeWebView() method, where finish() is called to close the current activity, including the WebView.

With this implementation, pressing the "Close" button in the HTML will allow the WebView window itself to close.

iPhone (iOS)

The following is an example that shows the Close button on the result screen after the game, and we provide the button. It is not something the brand's app developer needs to prepare.

<button id="closeButton">Close</button>

<script type="text/javascript">
    document.getElementById("closeButton").addEventListener("click", function() {
        // Sending a message to the WebView.
        window.webkit.messageHandlers.closeWebView.postMessage(null);
    });
</script>

Here, JavaScript sends a message to the iOS app through the webkit.messageHandlers object.

Swift code to handle the JavaScript message

In the GameViewController, you need to implement a message handler to receive the JavaScript message and close the WebView. Below is an example:

import UIKit
import WebKit

class GameViewController: UIViewController, WKNavigationDelegate, WKScriptMessageHandler {
    
    var webView: WKWebView!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        // Configure WebView settings
        let webConfiguration = WKWebViewConfiguration()
        
        // Add message handler (handle “closeWebView”)
        webConfiguration.userContentController.add(self, name: "closeWebView")
        
        // Initialize WKWebView with settings
        webView = WKWebView(frame: self.view.frame, configuration: webConfiguration)
        webView.navigationDelegate = self
        self.view.addSubview(webView)
        
        // Load URL
        if let url = URL(string: "https://branded.mini-games.io/?php=landing@UserID_Data&campaign_no=0123456") {
            let request = URLRequest(url: url)
            webView.load(request)
        }
    }
    
    // Implement method to handle JavaScript message
    func userContentController(_ userContentController: WKUserContentController, didReceive message: WKScriptMessage) {
        if message.name == "closeWebView" {
            // Close WebView upon receiving the message
            self.dismiss(animated: true, completion: nil)
        }
    }
    
    deinit {
        // Remove message handler when the view controller is deallocated
        webView.configuration.userContentController.removeScriptMessageHandler(forName: "closeWebView")
    }
}

Key explanation:

  1. JavaScript button: The line window.webkit.messageHandlers.closeWebView.postMessage(null); in JavaScript sends a message to the iOS app’s closeWebView handler.

  2. Swift message handler: In GameViewController, implement the WKScriptMessageHandler protocol to receive messages from JavaScript. When the "closeWebView" message is received, self.dismiss(animated: true, completion: nil) is called to close the WebView.

  3. Message handler setup: webConfiguration.userContentController.add(self, name: "closeWebView") registers a handler to process the "closeWebView" message.

  4. Memory management: In the deinit block, remove the message handler when the view controller is deallocated to prevent memory leaks.

Conclusion: By following the steps above, you can add a "Close" button to the HTML, and when the user clicks the button, the WebView within the iOS app will close. The key to this solution is leveraging the ability of WKWebView to handle JavaScript messages via the WKScriptMessageHandler.

PreviousMobile App -> UserID and WebhookNextPlay & Redeem

Last updated 7 months ago