Skip to content

Commit

Permalink
Added a flag to disable the automatic closing of the share extension …
Browse files Browse the repository at this point in the history
…after sharing.
  • Loading branch information
KasemJaffer committed Jan 26, 2024
1 parent a557867 commit 2d09313
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 16 deletions.
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,13 @@ end
import receive_sharing_intent

class ShareViewController: RSIShareViewController {
// That's it. You don't need to add any code here :)

// Use this method to return false if you don't want to redirect to host app automatically.
// Default is true
override func shouldAutoRedirect() -> Bool {
return false
}

}
```

Expand Down
8 changes: 7 additions & 1 deletion example/ios/Share Extension/ShareViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,11 @@
import receive_sharing_intent

class ShareViewController: RSIShareViewController {


// Use this method to return false if you don't want to redirect to host app automatically.
// Default is true
override func shouldAutoRedirect() -> Bool {
return false
}

}
26 changes: 21 additions & 5 deletions ios/Classes/RSIShareViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@ open class RSIShareViewController: SLComposeServiceViewController {
var hostAppBundleIdentifier = ""
var appGroupId = ""
var sharedMedia: [SharedMediaFile] = []

/// Override this method to return false if you don't want to redirect to host app automatically
/// Default is true
open func shouldAutoRedirect() -> Bool {
return true
}

open override func isContentValid() -> Bool {
return true
Expand All @@ -27,6 +33,11 @@ open class RSIShareViewController: SLComposeServiceViewController {
loadIds()
}

// Redirect to host app when user click on Post
open override func didSelectPost() {
saveAndRedirect(message: contentText)
}

open override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)

Expand Down Expand Up @@ -98,7 +109,9 @@ open class RSIShareViewController: SLComposeServiceViewController {
type: type
))
if index == (content.attachments?.count ?? 0) - 1 {
saveAndRedirect()
if shouldAutoRedirect() {
saveAndRedirect()
}
}
}

Expand Down Expand Up @@ -128,15 +141,18 @@ open class RSIShareViewController: SLComposeServiceViewController {
}

if index == (content.attachments?.count ?? 0) - 1 {
saveAndRedirect()
if shouldAutoRedirect() {
saveAndRedirect()
}
}
}


// Save shared media and redirect to host app
private func saveAndRedirect() {
private func saveAndRedirect(message: String? = nil) {
let userDefaults = UserDefaults(suiteName: appGroupId)
userDefaults?.set(toData(data: sharedMedia), forKey: kUserDefaultsKey)
userDefaults?.set(message, forKey: kUserDefaultsMessageKey)
userDefaults?.synchronize()
redirectToHostApp()
}
Expand Down Expand Up @@ -187,7 +203,7 @@ open class RSIShareViewController: SLComposeServiceViewController {
return name
}

func copyFile(at srcURL: URL, to dstURL: URL) -> Bool {
private func copyFile(at srcURL: URL, to dstURL: URL) -> Bool {
do {
if FileManager.default.fileExists(atPath: dstURL.path) {
try FileManager.default.removeItem(at: dstURL)
Expand Down Expand Up @@ -233,7 +249,7 @@ open class RSIShareViewController: SLComposeServiceViewController {
return path
}

func toData(data: [SharedMediaFile]) -> Data {
private func toData(data: [SharedMediaFile]) -> Data {
let encodedData = try? JSONEncoder().encode(data)
return encodedData!
}
Expand Down
26 changes: 18 additions & 8 deletions ios/Classes/SwiftReceiveSharingIntentPlugin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import Photos

public let kSchemePrefix = "ShareMedia"
public let kUserDefaultsKey = "ShareKey"
public let kUserDefaultsMessageKey = "ShareMessageKey"
public let kAppGroupIdKey = "AppGroupId"

public class SwiftReceiveSharingIntentPlugin: NSObject, FlutterPlugin, FlutterStreamHandler {
Expand Down Expand Up @@ -113,7 +114,7 @@ public class SwiftReceiveSharingIntentPlugin: NSObject, FlutterPlugin, FlutterSt
let defaultGroupId = "group.\(Bundle.main.bundleIdentifier!)"
let userDefaults = UserDefaults(suiteName: appGroupId ?? defaultGroupId)


let message = userDefaults?.string(forKey: kUserDefaultsMessageKey)
if let json = userDefaults?.object(forKey: kUserDefaultsKey) as? Data {
let sharedArray = decode(data: json)
let sharedMediaFiles: [SharedMediaFile] = sharedArray.compactMap {
Expand All @@ -127,6 +128,7 @@ public class SwiftReceiveSharingIntentPlugin: NSObject, FlutterPlugin, FlutterSt
mimeType: $0.mimeType,
thumbnail: getAbsolutePath(for: $0.thumbnail),
duration: $0.duration,
message: message,
type: $0.type
)
}
Expand Down Expand Up @@ -206,16 +208,24 @@ public class SharedMediaFile: Codable {
var mimeType: String?
var thumbnail: String? // video thumbnail
var duration: Double? // video duration in milliseconds
var message: String? // post message
var type: SharedMediaType


public init(path: String, mimeType: String? = nil, thumbnail: String? = nil, duration: Double? = nil, type: SharedMediaType) {
self.path = path
self.mimeType = mimeType
self.thumbnail = thumbnail
self.duration = duration
self.type = type
}
public init(
path: String,
mimeType: String? = nil,
thumbnail: String? = nil,
duration: Double? = nil,
message: String?=nil,
type: SharedMediaType) {
self.path = path
self.mimeType = mimeType
self.thumbnail = thumbnail
self.duration = duration
self.message = message
self.type = type
}
}

public enum SharedMediaType: String, Codable, CaseIterable {
Expand Down
8 changes: 7 additions & 1 deletion lib/receive_sharing_intent.dart
Original file line number Diff line number Diff line change
Expand Up @@ -89,20 +89,25 @@ class SharedMediaFile {
/// i.e. image/jpeg, video/mp4, text/plain
final String? mimeType;

/// Post message iOS ONLY
final String? message;

SharedMediaFile({
required this.path,
required this.type,
this.thumbnail,
this.duration,
this.mimeType,
this.message,
});

SharedMediaFile.fromMap(Map<String, dynamic> json)
: path = json['path'],
thumbnail = json['thumbnail'],
duration = json['duration'],
type = SharedMediaType.fromValue(json['type']),
mimeType = json['mimeType'];
mimeType = json['mimeType'],
message = json['message'];

Map<String, dynamic> toMap() {
return {
Expand All @@ -111,6 +116,7 @@ class SharedMediaFile {
'duration': duration,
'type': type.value,
'mimeType': mimeType,
'message': message,
};
}
}
Expand Down

0 comments on commit 2d09313

Please sign in to comment.