Share Content with other Apps: Cross Platform App Development
Tags: android, ios, scade, swift
Published: 2022-09-13 13:08:04

---
title: Share Content with other Apps: Cross Platform App Development
published: true
description: 
tags: android, ios, scade, swift
cover_image: https://dev-to-uploads.s3.amazonaws.com/uploads/articles/75h8zofdpcdz0sa3a8td.png
# Use a ratio of 100:42 for best results.
---

## Introduction

Hello everyone 👋

My name is Vedant, and today’s article is about sharing textual content with other apps using the swift-android compiler. It will work for both Android & iOS mobile platforms. As you already know that sharing content with other apps is a widespread use case and we often want the app users to share some content with the other installed apps on the device. For example, you may want to share some textual content or a link with your friend or colleague on WhatsApp or Slack.   


In this article (exclusively for the SCADE community), we will implement the textual content sharing feature that works well for Android and iOS platforms.

So let’s start 😎.

## Prerequisite

If you haven’t installed the SCADE IDE, [download](https://www.scade.io/download/) the SCADE IDE and [install](https://docs.scade.io/docs/installation) it on your macOS system. The only prerequisite for SCADE is the Swift language (at least basics). Also, please ensure the Android emulator or physical device is running if you want to run SCADE apps on an android device.

## Getting Started

To keep it simple, let’s understand the concepts by creating a new SCADE project,  <code>File -> New -> New Project<strong>. </strong></code>Then select the SCADE option, enter the project name, and click Create. 

## iOS Implementation

For the iOS implementation, we will use `UIActivityViewController` class and will pass the text string which we want to share to other apps. So let’s create an instance of this class, and then pass the `rootView` of the SCADE application to the activity view controller instance.


```swift
 // set up activity view controller
#if os(iOS)
  let textToShare = [ text ]
  let activityViewController = UIActivityViewController(activityItems: textToShare, applicationActivities: nil)
        activityViewController.popoverPresentationController?.sourceView = self.rootView 
     
  // present the view controller   
  UIApplication.shared.delegate?.window??.rootViewController?.present(activityViewController, animated:     
  true, completion: nil)
#endif
```


`popoverPresentationController` parameter will attach the source view to the SCADE application’s `rootView`. Finally, we will call the `present()` method of the current rootViewController of the UIApplication global object. The parameter to be passed in the present() method is basically the instance of the `UIActivityViewController` class that we have already created.

You can test the above code snippet. Yes, it works 😀! You can target a range of iOS devices and can easily share the textual content with other iOS apps.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/r5ugwkau6pw8vd51mlnq.gif)

## Let’s implement it for Android
 
Before getting started with the implementation of the Sharing of the textual content constants for the Android platform, let us briefly discuss the **[swift-android](https://github.com/scade-platform/swift-android/tree/android/24/Sources)** compiler. SCADE developers have developed internally the swift compiler for the Android SDK (currently it is supporting API 24). The repository is now open source and anyone can use the existing Swift classes to develop various Android use cases. 

[https://github.com/scade-platform/swift-android/tree/android/24](https://github.com/scade-platform/swift-android/tree/android/24)  
 
We will use the `Intent(action: Intent.ACTION_SEND)` method with the action intent type of `ACTION_SEND` parameter by creating an instance of the `Intent` class.  


```java
   /*Create an ACTION_SEND Intent*/
   Intent intent = new Intent(android.content.Intent.ACTION_SEND);

   /*This will be the actual content you wish you share.*/
   String shareBody = "Here is the share content body";

   /*The type of the content is text, obviously.*/
   intent.setType("text/plain");

   /*Applying information Subject and Body.*/
   intent.putExtra(android.content.Intent.EXTRA_SUBJECT, getString(R.string.share_subject));
   intent.putExtra(android.content.Intent.EXTRA_TEXT, shareBody);
   startActivity(Intent.createChooser(intent, getString(R.string.share_using)));
```


As the next step of native Android implementation, we will set the intent data type to plain text and pass the content’s subject and description using the `putExtra()` method. Finally, to display a list of apps to choose from, call the `startActivity()` method and pass the Intent instance.

## Swift compiler for Android code
 
The swift-android repository contains the swift classes which are generated from the Android SDK. We will inject the required classes into our SCADE app and achieve similar functionality accordingly. It uses SPM a lot to inject the necessary dependencies into the SCADE app.

For the Android code snippet above, we require the applicationContext and resources instance to call the necessary methods. We will add the below Android packages into `Package.swift`.


```swift
// swift-tools-version:5.3

import Foundation
import PackageDescription

let SCADE_SDK = ProcessInfo.processInfo.environment["SCADE_SDK"] ?? ""

let package = Package(
  name: "ScadeDemoApp",
  platforms: [
    .macOS(.v10_14)
  ],
  products: [
    .library(
      name: "ScadeDemoApp",
      type: .static,
      targets: [
        "ScadeDemoApp"
      ]
    )
  ],
  dependencies: [
    .package(
      name: "Android", url: "https://github.com/scade-platform/swift-android.git",
      .branch("android/24"))
  ],
  targets: [
    .target(
      name: "ScadeDemoApp",
      dependencies: [
        .product(name: "Android", package: "Android", condition: .when(platforms: [.android])),
        .product(name: "AndroidOS", package: "Android", condition: .when(platforms: [.android])),
        .product(name: "AndroidApp", package: "Android", condition: .when(platforms: [.android])),
        .product(
          name: "AndroidContent", package: "Android", condition: .when(platforms: [.android])),
      ],
      exclude: ["main.page"],
      swiftSettings: [
        .unsafeFlags(["-F", SCADE_SDK], .when(platforms: [.macOS, .iOS])),
        .unsafeFlags(["-I", "\(SCADE_SDK)/include"], .when(platforms: [.android])),
      ]
    )
  ]
)
```


As the next step, we will now import these packages for the Android in `main.page.swift` file. 



```swift
import ScadeKit

#if os(Android)
  import Android
  import AndroidApp
  import AndroidContent
#endif
```


We are now ready to call the respective swift methods for the given above java methods.  

```swift
 #if os(Android)
       let intent: Intent = Intent(action: Intent.ACTION_SEND)
       intent.setType(_type: "text/plain")
       intent.putExtra(name: Intent.EXTRA_SUBJECT, value: "The Subject of the Message")
       intent.putExtra(name: Intent.EXTRA_TEXT, value: text)
       Application.currentActivity?.startActivity(intent: intent)
 #endif
```


Let us understand the above code snippet and what it does basically. Firstly, we create an instance of `Intent` with the type of `ACTION_SEND`. We set the content to be plain text. 

In order to access the Application’s context, we use the `currentActivity` instance which returns the context. Using this context, we call `startActivity` method to launch the implicit intent that we created in the first step.

Now we are good to test the code on Android devices. Set the target to any Android device you like and test if it is working as expected.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lph9pxxpm0ewfnszh38j.gif)

Voila🎊! We have successfully implemented the sharing of content with other apps using the swift-android compiler. This is one of the examples of coding cross-platform apps using the SCADE platform. Integrating any Android-specific feature is very easy into the SCADE app using the swift-android library. You should definitely try the swift-android to build some of the useful components to build cross-platform apps. 
 
We will be releasing a series of articles related to the swift-android compiler. Thank you for reading and see you in the next article!

Happy Coding 😊