iPhone iOS App

You can create HTML5 mini-games using Studio, activate the game, and obtain a URL link to run it. To add the completed mini-game to an iOS app, the app must support WebView.

Below is an example of code to load and run the URL in an iOS WebView, assuming that you have created a mini-game in Studio and the execution link is: https://branded.mini-games.io/?php=landing@UserID_Data&campaign_no=0123456.


1. Basic Setup for Using WKWebView

First, we’ll show how to add WKWebView to your project and use it to load and run the HTML5 game.

2. Example Code

Swift Example:

import UIKit
import WebKit

class GameViewController: UIViewController, WKNavigationDelegate {
    
    var webView: WKWebView!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        // Initialize WKWebView
        webView = WKWebView(frame: self.view.frame)
        webView.navigationDelegate = self
        self.view.addSubview(webView)
        
        // Load the 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)
        }
    }
    
    // Called when the web content is successfully loaded
    func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
        print("WebView content loaded successfully.")
    }
    
    // Called if the web content fails to load
    func webView(_ webView: WKWebView, didFail navigation: WKNavigation!, withError error: Error) {
        print("Failed to load WebView content: \(error.localizedDescription)")
    }
}

3. Key Explanations

  1. Initialize WKWebView: webView is set up as a WKWebView, filling the entire screen.

  2. Loading the URL: The URLRequest is used to load the specified URL (https://branded.mini-games.io/?php=landing@UserID_Data&campaign_no=0123456).

  3. navigationDelegate: This delegate is used to track the WebView’s state. The didFinish and didFail methods are called when the web content has either successfully loaded or failed.

  4. Add to the ViewController: The WebView is added to the view controller to make it visible on the screen.

4. Additional Considerations

  • App Permissions: If you’re using WebView, you may need to update your app’s Info.plist file with the App Transport Security settings. If you need to load an HTTP URL, you can add the following keys to allow HTTP:

<key>NSAppTransportSecurity</key>
<dict>
    <key>NSAllowsArbitraryLoads</key>
    <true/>
</dict>
  • Error Handling: It's a good practice to handle network errors or failures to access the URL properly with error handling logic.

By using this code, you can load and run an HTML5 mini-game in an iOS app via a WebView.

Last updated