KasemJaffer / receive_sharing_intent

A Flutter plugin that enables flutter apps to receive sharing photos, text and url from other apps.
Apache License 2.0
321 stars 369 forks source link

image intent doesn't work for iOS when app is closed #310

Open JackBeStrong opened 2 weeks ago

JackBeStrong commented 2 weeks ago

How to reproduce:

I literally started a new project, just to test this out.

  1. I created a new project intent_test (flutter name)
  2. Used the example main.dart exactly.
  3. Followed the steps in iOS exactly, and I was able to get intent to work when the app is just minimized: IMG_4113

Now, if I swipe up, and close the app. And share the file again, the app just crashes, with the attached crash report. Runner-2024-06-30-142648.txt

What have I done wrong or is this an actual issue with the package?

Below is my dart code:

import 'package:flutter/material.dart'; import 'dart:async';

import 'package:receive_sharing_intent/receive_sharing_intent.dart';

void main() => runApp(MyApp());

class MyApp extends StatefulWidget { @override _MyAppState createState() => _MyAppState(); }

class _MyAppState extends State { late StreamSubscription _intentSub; final _sharedFiles = [];

@override void initState() { super.initState();

// Listen to media sharing coming from outside the app while the app is in the memory.
_intentSub = ReceiveSharingIntent.instance.getMediaStream().listen((value) {
  setState(() {
    _sharedFiles.clear();
    _sharedFiles.addAll(value);

    print(_sharedFiles.map((f) => f.toMap()));
  });
}, onError: (err) {
  print("getIntentDataStream error: $err");
});

// Get the media sharing coming from outside the app while the app is closed.
ReceiveSharingIntent.instance.getInitialMedia().then((value) {
  setState(() {
    _sharedFiles.clear();
    _sharedFiles.addAll(value);
    print(_sharedFiles.map((f) => f.toMap()));

    // Tell the library that we are done processing the intent.
    ReceiveSharingIntent.instance.reset();
  });
});

}

@override void dispose() { _intentSub.cancel(); super.dispose(); }

@override Widget build(BuildContext context) { const textStyleBold = const TextStyle(fontWeight: FontWeight.bold); return MaterialApp( home: Scaffold( appBar: AppBar( title: const Text('Plugin example app'), ), body: Center( child: Column( children: [ Text("Shared files:", style: textStyleBold), Text(_sharedFiles .map((f) => f.toMap()) .join(",\n****\n")), ], ), ), ), ); } }

Let me know if you need any additional information.

JackBeStrong commented 2 weeks ago

I'd also want to add that Android works fine. It's just iOS when the app is closed, that it crashes. I feed the crash report to AI, and this is what I got:

The crash log you provided indicates that the app "Runner" encountered a segmentation fault (EXC_BAD_ACCESS) due to an invalid memory address (KERN_INVALID_ADDRESS at 0x0000000000000000). This typically happens when the app attempts to access memory that has not been allocated or is no longer valid.

Here are some key points from the crash log and potential areas to investigate:

Exception Codes:

The exception codes are 0x0000000000000001, 0x0000000000000000, indicating a segmentation fault at address 0x0000000000000000. Faulting Thread:

The crash occurred in the main thread (thread 0). The pc (program counter) value indicates the address where the crash happened: swift_getObjectType. Relevant Symbols:

swift_getObjectType: This symbol appears to be where the crash originated, which is related to Swift object type retrieval. static SwiftReceiveSharingIntentPlugin.register(with:): This method is part of the receive_sharing_intent plugin and is mentioned multiple times in the call stack. Call Stack:

The call stack shows the sequence of method calls leading to the crash. Key methods include SwiftReceiveSharingIntentPlugin.register(with:) and GeneratedPluginRegistrant.registerWithRegistry. Plugin Involvement:

The receive_sharing_intent plugin is heavily referenced in the call stack, suggesting that it might be the source of the issue. Steps to Troubleshoot: Check Plugin Registration:

Ensure that the receive_sharing_intent plugin is properly registered in the app. Verify that the plugin setup is correct in both the Dart and native code. Inspect Plugin Code:

Review the implementation of the SwiftReceiveSharingIntentPlugin and ReceiveSharingIntentPlugin classes. Look for any potential issues with memory management or improper access to objects. Verify Dependencies:

Ensure that all dependencies and plugins are up-to-date and compatible with your current Flutter and Dart versions. Debugging:

Use breakpoints and logging to trace the execution flow in the SwiftReceiveSharingIntentPlugin.register(with:) method to pinpoint where the invalid memory access occurs. Reproduce the Crash:

Try to reproduce the crash consistently by following the same steps or actions that led to the crash. This can help narrow down the exact cause. Update iOS Version:

Ensure that you are testing on a device or simulator with the latest iOS version, as there might be fixes or improvements in newer releases. By following these steps, you should be able to identify and resolve the cause of the crash in the Runner app related to the receive_sharing_intent plugin. If you need further assistance, consider reaching out to the plugin's maintainers or community for support.