Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save jeffhigham-f3/880f28ea178d72cc6d25c77ec40cd120 to your computer and use it in GitHub Desktop.
Save jeffhigham-f3/880f28ea178d72cc6d25c77ec40cd120 to your computer and use it in GitHub Desktop.
App Clips + Google Play Instant + React Native - QR Code Reader

App Clips + Google Play Instant + React Native - QR Code Reader

This gist provides a basic implementation of handling QR codes with App Clips using SwiftUI and React Native for iOS, and Google Play Instant for Android.

Setup Instructions

iOS App Clips

1. Configuring Associated Domains

In Xcode, select your App Clip target and navigate to Signing & Capabilities. Add an Associated Domains entitlement, and include your domain in the format applinks:yourdomain.com.

2. Handling URLs in AppDelegate.swift

Add the following code to your AppDelegate.swift:

import UIKit
import SwiftUI
import React

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

  var window: UIWindow?

  func application(_ application: UIApplication, continue userActivity: NSUserActivity, restorationHandler: @escaping ([UIUserActivityRestoring]?) -> Void) -> Bool {
    if userActivity.activityType == NSUserActivityTypeBrowsingWeb, let url = userActivity.webpageURL {
      handleIncomingURL(url)
    }
    return true
  }

  func handleIncomingURL(_ url: URL) {
    let bridge = RCTBridge(delegate: self, launchOptions: nil)
    let initialProps: [String: Any] = ["url": url.absoluteString]
    let rootView = RCTRootView(bridge: bridge, moduleName: "AppClip", initialProperties: initialProps)
    
    if let windowScene = UIApplication.shared.connectedScenes.first as? UIWindowScene {
      let window = UIWindow(windowScene: windowScene)
      let rootViewController = UIHostingController(rootView: AppClipView(url: url.absoluteString))
      window.rootViewController = rootViewController
      self.window = window
      window.makeKeyAndVisible()
    }
  }
}

extension AppDelegate: RCTBridgeDelegate {
  func sourceURL(for bridge: RCTBridge!) -> URL! {
    return RCTBundleURLProvider.sharedSettings().jsBundleURL(forBundleRoot: "index", fallbackResource: nil)
  }
}

3. Handling URL Parameters in SwiftUI

Add the following code to your AppClipView.swift:

import SwiftUI

struct AppClipView: View {
  let url: String
  
  var body: some View {
    VStack {
      Text("App Clip Launched!")
        .font(.largeTitle)
        .padding()
      
      Text("URL: \(url)")
        .padding()
      
      // Add more UI elements as needed
    }
  }
}

struct AppClipView_Previews: PreviewProvider {
  static var previews: some View {
    AppClipView(url: "https://example.com")
  }
}

4. Handling URL Parameters in React Native

Add the following code to your AppClip.js:

import React from 'react';
import { View, Text } from 'react-native';

const AppClip = ({ url }) => {
  const params = new URLSearchParams(url.split('?')[1]);
  const someValue = params.get('someValue'); // Extract parameters as needed

  return (
    <View>
      <Text>QR Code Value: {someValue}</Text>
    </View>
  );
};

export default AppClip;

5. Testing

  • Generate a QR Code: Use a QR code generator to create a QR code with the URL associated with your App Clip.
  • Scan the QR Code: Use the iPhone camera app or a QR code scanning app to scan the QR code. It should prompt the user to open the App Clip if everything is set up correctly.

Additional Tips

  • Testing on Device: Ensure to test on a real device since App Clips might not fully function in the simulator.
  • Debugging: Use Xcode’s debugging tools to troubleshoot any issues with URL handling or launching the App Clip.
  • Documentation: Refer to Apple’s official documentation on App Clips for more detailed information and best practices.

Google Play Instant

1. Set Up Instant Apps in Google Play Console

  • Enroll your app in the Google Play Instant program through the Google Play Console.
  • Create an instant app configuration and set up feature modules that can be launched instantly.

2. Configure Your Android Project

  • Create a new feature module in your Android project for the instant app experience.
  • Configure the AndroidManifest.xml to define the entry points and instant app capabilities.

Example AndroidManifest.xml for Instant App:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.yourcompany.instantapp">

    <application
        android:label="@string/app_name"
        android:icon="@mipmap/ic_launcher">
        <activity
            android:name=".MainActivity"
            android:launchMode="singleTask"
            android:theme="@style/AppTheme">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

3. Integrate React Native with Google Play Instant

Ensure your React Native app is modular to allow specific features to be extracted into instant experiences.

Example MainActivity.java for Instant App:

import com.facebook.react.ReactActivity;

public class MainActivity extends ReactActivity {
  @Override
  protected String getMainComponentName() {
    return "InstantApp"; // Your instant app specific component
  }
}

4. Handling URL Parameters in React Native

Add the following code to your InstantApp.js:

import React from 'react';
import { View, Text } from 'react-native';

const InstantApp = ({ url }) => {
  const params = new URLSearchParams(url.split('?')[1]);
  const someValue = params.get('someValue'); // Extract parameters as needed

  return (
    <View>
      <Text>Instant App Value: {someValue}</Text>
    </View>
  );
};

export default InstantApp;

5. Testing

  • Generate a QR Code: Use a QR code generator to create a QR code with the URL associated with your Instant App.
  • Scan the QR Code: Use the Android device’s camera app or a QR code scanning app to scan the QR code. It should prompt the user to open the Instant App if everything is set up correctly.

Additional Tips

  • Testing on Device: Ensure to test on a real device since Instant Apps might not fully function in the emulator.
  • Debugging: Use Android Studio’s debugging tools to troubleshoot any issues with URL handling or launching the Instant App.
  • Documentation: Refer to Google’s official documentation on Google Play Instant for more detailed information and best practices.
import qrcode
# Data to be encoded in the QR code
data = '{ "name": "test", "uuid": "3193beaf-0ff1-4f49-a528-49a713edc54f" }'
# Generate the QR code
qr = qrcode.QRCode(
version=1,
error_correction=qrcode.constants.ERROR_CORRECT_L,
box_size=10,
border=4,
)
qr.add_data(data)
qr.make(fit=True)
# Create an image from the QR Code instance
img = qr.make_image(fill='black', back_color='white')
# Save the image as a PNG file
img_path = "qr_code_test.png"
img.save(img_path)
img_path
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment