Encountering issues when trying to share an image on iOS through the WebShare API

I am currently working on implementing the WebShare API to share an image on both iOS and Android platforms. Surprisingly, my code functions perfectly on Android, but I encountered an issue on iOS where certain apps such as WhatsApp only share the URL instead of the actual image. To tackle this problem, I incorporated additional code to restrict sharing anything other than the image on iOS:

let data = IS_SAFARI ? { files: [], text: '', url: '', title: '' } : { files: [], text: text, url: URL, title: TITLE };

Despite these modifications, the image still fails to display and only the URL is shared:

const URL = 'https://upload.wikimedia.org/wikipedia/commons/2/27/Map_of_Spain_1490.jpg';
const TYPE = 'image/jpeg';
const EXT = '.jpg';
const TITLE = "yourTitle";
const IS_SAFARI = /^((?!chrome|android).)*safari/i.test(navigator.userAgent);

function webshare(text) {

    let navigator;
    navigator = window.navigator;
    let data = IS_SAFARI ? { files: [], text: '', url: '', title: '' } : { files: [], text: text, url: URL, title: TITLE };

    var xhr = new XMLHttpRequest();
    xhr.open('GET', URL, true);
    xhr.responseType = 'arraybuffer';
    xhr.onload = function() {
        if (xhr.status === 200) {
        var response = xhr.response;
        console.log(response);

        var blob = new File([response], text + EXT, { type: TYPE });
        data.files.push(blob);
        console.log(data);

        if (navigator.canShare && navigator.canShare(data)) {
            navigator.share(data)
            .then(function() {})
            .catch(function(err) {
                console.error('Unsuccessful share ' + err);
            });
        }
        } else {
        console.error('Failed to load ' + URL + ': ' + xhr.status);
        }
    };
    xhr.send();
}
<button onclick="webshare('Map_of_Spain_1490.jpg')">Share</button>

Are there any suggestions on how to resolve this issue?

Answer №1

The issue has been resolved after removing the url, text, and title values from the share function:

let data = IS_SAFARI ? { files: [] } : { files: [], text: text, url: URL, title: TITLE };

Interestingly, on iOS, it seems you cannot simultaneously share texts and files for some unknown reason. Therefore, to share a text, it had to be done in a separate share operation like this:

navigator.share(data)
.then(function() {
    if (IS_SAFARI) {
        let dataText = { files: [], text: text, url: URL, title: TITLE };
        navigator.share(dataText);
    }                                
})
.catch(function(err) {
    console.error('Unsuccessful share ' + err);
});

This peculiar bug in iOS is quite inconvenient. Hopefully, Apple will address these issues in the future.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Showing various pictures using numerous buttons in JavaScript

Looking for some assistance with my JavaScript project. I am new to coding and trying to create a program that displays different images when buttons are clicked. Can anyone lend a hand? Here's the code I have so far: <html> <body> < ...

What is the process for executing a "hello world" on webassembly using the most recent version of Node.js?

Having thoroughly gone through the official documentation, I am still unable to locate the necessary information. Currently operating on Node.js version v8.1.3, I am seeking guidance on compiling and executing a basic "hello world" program in wast (WebAss ...

Error: Typescript interface with immutable properties (Error: 'readonly' is not recognized)

I am encountering an error: "Cannot find name 'readonly'" while trying to define an interface with readonly properties. I have Typescript version 2.0.8 installed and I am working on Visual Studio 2015. Below is a snippet of the code: TypeScript ...

How can we dynamically update an environment variable for the IP address before running npm start in a React application?

Currently, I am attempting to automatically set the REACT_APP_DEPLOY_DOMAIN variable in the .env file. Here is one approach that I have implemented to address this challenge. In the script below, I am extracting my IP address: const os = require("os"); ...

I need help fetching the selected value instead of the option value in PHP

Can anyone offer some assistance with my issue? I have an HTML and PHP function where I am trying to echo a selected value, not the option value. Here is the desired output: "Patrick or any other name when I select and send the button" <?php ...

D3 not distinguishing between bars with identical data even when a key function is implemented

When attempting to create a Bar chart with mouseover and mouseout events on the bars using scaleBand(), I encountered an issue. After reviewing the solution here, which explains how ordinal scale treats repeated values as the same, I added a key to the dat ...

A discrepancy has arisen as the value within the array structure is being altered

I am facing an issue with the array structure of an object in my Angular front-end code. When I add values to the array based on selection, the object looks like this: todayRates: 10 yesterdayRates: 5 to: Array(1) 0: [3] length: 1 __proto__: Array(0) __pro ...

Show the current temperature data retrieved from the weather API

I'm trying to figure out what's causing the issue with the temperature not displaying. Can anyone help me identify and fix the error? <script type="text/javascript"> $.ajax({ url: 'https://api.weather.gov/gridpoints/EWX ...

Each time the app is started, the arc4random function consistently generates the same initial value

I am currently working on an iPhone application where I use arc4random in viewDidLoad to generate a random number. However, I've noticed that every time I start the application, it always gives me the same number from arc4random. This issue seems to o ...

Accessing an array of objects within nested objects results in an undefined value

I am facing an issue with my JavaScript object that is retrieved from MySQL. The object has a property which contains an array of other objects, as demonstrated below: parentObject = { ID: "1", Desc: "A description", chi ...

I'm encountering an issue where I receive the error `Cannot read property 'map' of undefined` while attempting to integrate the backend with React. What could be causing this error and how

I'm currently learning React and I've been using a tutorial here to connect my database to the front end of my React application. Unfortunately, every time I try running 'npm start' at 'localhost:3000' in express-react/backend ...

Is there a more efficient way to optimize my coding for this Cellular Automata project?

As a beginner in programming, I wanted to delve into the world of cellular automata and decided to create my own using JavaScript. The project involves a simple binary (black and white) 2D CA where each cell updates its color based on the colors of its 8 ...

Is it possible to consolidate React and React-DOM into a unified library instead of having them separate?

Is it possible to combine React.JS and React-DOM.JS into a single library? In all the web applications I've encountered, we always have to import both libraries separately. Have there been any cases where either of these libraries can be used on its ...

Choosing an option in react-select causes the page to unexpectedly shift

Encountering a problem with a mobile modal developed using react-select. The selectors are within a div with fixed height and overflow-y: scroll. Upon selecting an option for the 'Choose observer' select, the entire modal briefly jumps down in th ...

Adjust the minimum height and width when resizing the window

Apologies in advance if this has already been addressed, but I have tried solutions from other sources without success. My Objective: I aim to set the minimum height and width of a div based on the current dimensions of the document. This should trigger ...

Is there a way to align these side by side?

Is it a silly question? Perhaps. But I can't seem to figure out how to align my text and colorpicker on the same line instead of two. Take a look at my fiddle. I've tried removing display:block and clear:both, but that didn't do the trick. H ...

The NavigationController in the ViewController creates a continuous loop back to itself

Simply put, I created an app initially without a Navigation Controller and controlled everything through my ViewController. Upon adding the Navigation Controller, I implemented a button to switch to my main Navigation Controller window from the ViewControl ...

How can I access a component variable within a foreach loop in Typescript?

Can anyone please explain how I can access a component variable within a foreach loop? Check out my code on Plunker public exampleVariable:number; test(){ console.log('fired'); var x =[1,2,3,4]; x.forEach(function (e){ th ...

Find the size of the grid using the data attribute

I am currently working on a piece of code that involves fetching a data-attribute known as grid size from the HTML. My objective is to create a conditional statement that checks whether the value of grid size is "large" or "small", and assigns specific x a ...

JavaScript Conversion from Binary to Decimal

This code is meant to convert a binary string ("100101") to its decimal equivalent. I am struggling to identify the issue causing it to not work properly. Any assistance would be greatly appreciated. function BinaryConverter(str) { var num=str.split("") ...