mirror of
https://gitlab.com/famedly/fluffychat.git
synced 2025-12-17 05:17:30 +01:00
refactor: Update native receive sharing intent code on iOS
This commit is contained in:
parent
47ce29d00d
commit
740ff8cf6d
@ -4,8 +4,8 @@ import MobileCoreServices
|
|||||||
import Photos
|
import Photos
|
||||||
|
|
||||||
class ShareViewController: SLComposeServiceViewController {
|
class ShareViewController: SLComposeServiceViewController {
|
||||||
// TODO: IMPORTANT: This should be your host app bundle identifier
|
var hostAppBundleIdentifier = ""
|
||||||
let hostAppBundleIdentifier = "im.fluffychat.app"
|
var appGroupId = ""
|
||||||
let sharedKey = "ShareKey"
|
let sharedKey = "ShareKey"
|
||||||
var sharedMedia: [SharedMediaFile] = []
|
var sharedMedia: [SharedMediaFile] = []
|
||||||
var sharedText: [String] = []
|
var sharedText: [String] = []
|
||||||
@ -19,8 +19,26 @@ class ShareViewController: SLComposeServiceViewController {
|
|||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private func loadIds() {
|
||||||
|
// loading Share extension App Id
|
||||||
|
let shareExtensionAppBundleIdentifier = Bundle.main.bundleIdentifier!;
|
||||||
|
|
||||||
|
|
||||||
|
// convert ShareExtension id to host app id
|
||||||
|
// By default it is remove last part of id after last point
|
||||||
|
// For example: com.test.ShareExtension -> com.test
|
||||||
|
let lastIndexOfPoint = shareExtensionAppBundleIdentifier.lastIndex(of: ".");
|
||||||
|
hostAppBundleIdentifier = String(shareExtensionAppBundleIdentifier[..<lastIndexOfPoint!]);
|
||||||
|
|
||||||
|
// loading custom AppGroupId from Build Settings or use group.<hostAppBundleIdentifier>
|
||||||
|
appGroupId = (Bundle.main.object(forInfoDictionaryKey: "AppGroupId") as? String) ?? "group.\(hostAppBundleIdentifier)";
|
||||||
|
}
|
||||||
|
|
||||||
override func viewDidLoad() {
|
override func viewDidLoad() {
|
||||||
super.viewDidLoad();
|
super.viewDidLoad();
|
||||||
|
|
||||||
|
// load group and app id from build info
|
||||||
|
loadIds();
|
||||||
}
|
}
|
||||||
|
|
||||||
override func viewDidAppear(_ animated: Bool) {
|
override func viewDidAppear(_ animated: Bool) {
|
||||||
@ -64,7 +82,7 @@ class ShareViewController: SLComposeServiceViewController {
|
|||||||
|
|
||||||
// If this is the last item, save imagesData in userDefaults and redirect to host app
|
// If this is the last item, save imagesData in userDefaults and redirect to host app
|
||||||
if index == (content.attachments?.count)! - 1 {
|
if index == (content.attachments?.count)! - 1 {
|
||||||
let userDefaults = UserDefaults(suiteName: "group.\(this.hostAppBundleIdentifier)")
|
let userDefaults = UserDefaults(suiteName: this.appGroupId)
|
||||||
userDefaults?.set(this.sharedText, forKey: this.sharedKey)
|
userDefaults?.set(this.sharedText, forKey: this.sharedKey)
|
||||||
userDefaults?.synchronize()
|
userDefaults?.synchronize()
|
||||||
this.redirectToHostApp(type: .text)
|
this.redirectToHostApp(type: .text)
|
||||||
@ -85,7 +103,7 @@ class ShareViewController: SLComposeServiceViewController {
|
|||||||
|
|
||||||
// If this is the last item, save imagesData in userDefaults and redirect to host app
|
// If this is the last item, save imagesData in userDefaults and redirect to host app
|
||||||
if index == (content.attachments?.count)! - 1 {
|
if index == (content.attachments?.count)! - 1 {
|
||||||
let userDefaults = UserDefaults(suiteName: "group.\(this.hostAppBundleIdentifier)")
|
let userDefaults = UserDefaults(suiteName: this.appGroupId)
|
||||||
userDefaults?.set(this.sharedText, forKey: this.sharedKey)
|
userDefaults?.set(this.sharedText, forKey: this.sharedKey)
|
||||||
userDefaults?.synchronize()
|
userDefaults?.synchronize()
|
||||||
this.redirectToHostApp(type: .text)
|
this.redirectToHostApp(type: .text)
|
||||||
@ -105,7 +123,7 @@ class ShareViewController: SLComposeServiceViewController {
|
|||||||
// Always copy
|
// Always copy
|
||||||
let fileName = this.getFileName(from: url, type: .image)
|
let fileName = this.getFileName(from: url, type: .image)
|
||||||
let newPath = FileManager.default
|
let newPath = FileManager.default
|
||||||
.containerURL(forSecurityApplicationGroupIdentifier: "group.\(this.hostAppBundleIdentifier)")!
|
.containerURL(forSecurityApplicationGroupIdentifier: this.appGroupId)!
|
||||||
.appendingPathComponent(fileName)
|
.appendingPathComponent(fileName)
|
||||||
let copied = this.copyFile(at: url, to: newPath)
|
let copied = this.copyFile(at: url, to: newPath)
|
||||||
if(copied) {
|
if(copied) {
|
||||||
@ -114,7 +132,7 @@ class ShareViewController: SLComposeServiceViewController {
|
|||||||
|
|
||||||
// If this is the last item, save imagesData in userDefaults and redirect to host app
|
// If this is the last item, save imagesData in userDefaults and redirect to host app
|
||||||
if index == (content.attachments?.count)! - 1 {
|
if index == (content.attachments?.count)! - 1 {
|
||||||
let userDefaults = UserDefaults(suiteName: "group.\(this.hostAppBundleIdentifier)")
|
let userDefaults = UserDefaults(suiteName: this.appGroupId)
|
||||||
userDefaults?.set(this.toData(data: this.sharedMedia), forKey: this.sharedKey)
|
userDefaults?.set(this.toData(data: this.sharedMedia), forKey: this.sharedKey)
|
||||||
userDefaults?.synchronize()
|
userDefaults?.synchronize()
|
||||||
this.redirectToHostApp(type: .media)
|
this.redirectToHostApp(type: .media)
|
||||||
@ -134,7 +152,7 @@ class ShareViewController: SLComposeServiceViewController {
|
|||||||
// Always copy
|
// Always copy
|
||||||
let fileName = this.getFileName(from: url, type: .video)
|
let fileName = this.getFileName(from: url, type: .video)
|
||||||
let newPath = FileManager.default
|
let newPath = FileManager.default
|
||||||
.containerURL(forSecurityApplicationGroupIdentifier: "group.\(this.hostAppBundleIdentifier)")!
|
.containerURL(forSecurityApplicationGroupIdentifier: this.appGroupId)!
|
||||||
.appendingPathComponent(fileName)
|
.appendingPathComponent(fileName)
|
||||||
let copied = this.copyFile(at: url, to: newPath)
|
let copied = this.copyFile(at: url, to: newPath)
|
||||||
if(copied) {
|
if(copied) {
|
||||||
@ -146,7 +164,7 @@ class ShareViewController: SLComposeServiceViewController {
|
|||||||
|
|
||||||
// If this is the last item, save imagesData in userDefaults and redirect to host app
|
// If this is the last item, save imagesData in userDefaults and redirect to host app
|
||||||
if index == (content.attachments?.count)! - 1 {
|
if index == (content.attachments?.count)! - 1 {
|
||||||
let userDefaults = UserDefaults(suiteName: "group.\(this.hostAppBundleIdentifier)")
|
let userDefaults = UserDefaults(suiteName: this.appGroupId)
|
||||||
userDefaults?.set(this.toData(data: this.sharedMedia), forKey: this.sharedKey)
|
userDefaults?.set(this.toData(data: this.sharedMedia), forKey: this.sharedKey)
|
||||||
userDefaults?.synchronize()
|
userDefaults?.synchronize()
|
||||||
this.redirectToHostApp(type: .media)
|
this.redirectToHostApp(type: .media)
|
||||||
@ -166,7 +184,7 @@ class ShareViewController: SLComposeServiceViewController {
|
|||||||
// Always copy
|
// Always copy
|
||||||
let fileName = this.getFileName(from :url, type: .file)
|
let fileName = this.getFileName(from :url, type: .file)
|
||||||
let newPath = FileManager.default
|
let newPath = FileManager.default
|
||||||
.containerURL(forSecurityApplicationGroupIdentifier: "group.\(this.hostAppBundleIdentifier)")!
|
.containerURL(forSecurityApplicationGroupIdentifier: this.appGroupId)!
|
||||||
.appendingPathComponent(fileName)
|
.appendingPathComponent(fileName)
|
||||||
let copied = this.copyFile(at: url, to: newPath)
|
let copied = this.copyFile(at: url, to: newPath)
|
||||||
if (copied) {
|
if (copied) {
|
||||||
@ -174,7 +192,7 @@ class ShareViewController: SLComposeServiceViewController {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if index == (content.attachments?.count)! - 1 {
|
if index == (content.attachments?.count)! - 1 {
|
||||||
let userDefaults = UserDefaults(suiteName: "group.\(this.hostAppBundleIdentifier)")
|
let userDefaults = UserDefaults(suiteName: this.appGroupId)
|
||||||
userDefaults?.set(this.toData(data: this.sharedMedia), forKey: this.sharedKey)
|
userDefaults?.set(this.toData(data: this.sharedMedia), forKey: this.sharedKey)
|
||||||
userDefaults?.synchronize()
|
userDefaults?.synchronize()
|
||||||
this.redirectToHostApp(type: .file)
|
this.redirectToHostApp(type: .file)
|
||||||
@ -200,7 +218,9 @@ class ShareViewController: SLComposeServiceViewController {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private func redirectToHostApp(type: RedirectType) {
|
private func redirectToHostApp(type: RedirectType) {
|
||||||
let url = URL(string: "ShareMedia://dataUrl=\(sharedKey)#\(type)")
|
// ids may not loaded yet so we need loadIds here too
|
||||||
|
loadIds();
|
||||||
|
let url = URL(string: "ShareMedia-\(hostAppBundleIdentifier)://dataUrl=\(sharedKey)#\(type)")
|
||||||
var responder = self as UIResponder?
|
var responder = self as UIResponder?
|
||||||
let selectorOpenURL = sel_registerName("openURL:")
|
let selectorOpenURL = sel_registerName("openURL:")
|
||||||
|
|
||||||
@ -291,7 +311,7 @@ class ShareViewController: SLComposeServiceViewController {
|
|||||||
private func getThumbnailPath(for url: URL) -> URL {
|
private func getThumbnailPath(for url: URL) -> URL {
|
||||||
let fileName = Data(url.lastPathComponent.utf8).base64EncodedString().replacingOccurrences(of: "==", with: "")
|
let fileName = Data(url.lastPathComponent.utf8).base64EncodedString().replacingOccurrences(of: "==", with: "")
|
||||||
let path = FileManager.default
|
let path = FileManager.default
|
||||||
.containerURL(forSecurityApplicationGroupIdentifier: "group.\(hostAppBundleIdentifier)")!
|
.containerURL(forSecurityApplicationGroupIdentifier: appGroupId)!
|
||||||
.appendingPathComponent("\(fileName).jpg")
|
.appendingPathComponent("\(fileName).jpg")
|
||||||
return path
|
return path
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user