mirror of
				https://gitlab.com/famedly/fluffychat.git
				synced 2025-10-31 12:07:24 +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 | ||||
| 
 | ||||
| class ShareViewController: SLComposeServiceViewController { | ||||
|     // TODO: IMPORTANT: This should be your host app bundle identifier | ||||
|     let hostAppBundleIdentifier = "im.fluffychat.app" | ||||
|     var hostAppBundleIdentifier = "" | ||||
|     var appGroupId = "" | ||||
|     let sharedKey = "ShareKey" | ||||
|     var sharedMedia: [SharedMediaFile] = [] | ||||
|     var sharedText: [String] = [] | ||||
| @ -19,8 +19,26 @@ class ShareViewController: SLComposeServiceViewController { | ||||
|         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() { | ||||
|         super.viewDidLoad(); | ||||
| 
 | ||||
|         // load group and app id from build info | ||||
|         loadIds(); | ||||
|     } | ||||
| 
 | ||||
|     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 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?.synchronize() | ||||
|                     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 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?.synchronize() | ||||
|                     this.redirectToHostApp(type: .text) | ||||
| @ -105,7 +123,7 @@ class ShareViewController: SLComposeServiceViewController { | ||||
|                 // Always copy | ||||
|                 let fileName = this.getFileName(from: url, type: .image) | ||||
|                 let newPath = FileManager.default | ||||
|                     .containerURL(forSecurityApplicationGroupIdentifier: "group.\(this.hostAppBundleIdentifier)")! | ||||
|                     .containerURL(forSecurityApplicationGroupIdentifier: this.appGroupId)! | ||||
|                     .appendingPathComponent(fileName) | ||||
|                 let copied = this.copyFile(at: url, to: newPath) | ||||
|                 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 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?.synchronize() | ||||
|                     this.redirectToHostApp(type: .media) | ||||
| @ -134,7 +152,7 @@ class ShareViewController: SLComposeServiceViewController { | ||||
|                 // Always copy | ||||
|                 let fileName = this.getFileName(from: url, type: .video) | ||||
|                 let newPath = FileManager.default | ||||
|                     .containerURL(forSecurityApplicationGroupIdentifier: "group.\(this.hostAppBundleIdentifier)")! | ||||
|                     .containerURL(forSecurityApplicationGroupIdentifier: this.appGroupId)! | ||||
|                     .appendingPathComponent(fileName) | ||||
|                 let copied = this.copyFile(at: url, to: newPath) | ||||
|                 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 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?.synchronize() | ||||
|                     this.redirectToHostApp(type: .media) | ||||
| @ -166,7 +184,7 @@ class ShareViewController: SLComposeServiceViewController { | ||||
|                 // Always copy | ||||
|                 let fileName = this.getFileName(from :url, type: .file) | ||||
|                 let newPath = FileManager.default | ||||
|                     .containerURL(forSecurityApplicationGroupIdentifier: "group.\(this.hostAppBundleIdentifier)")! | ||||
|                     .containerURL(forSecurityApplicationGroupIdentifier: this.appGroupId)! | ||||
|                     .appendingPathComponent(fileName) | ||||
|                 let copied = this.copyFile(at: url, to: newPath) | ||||
|                 if (copied) { | ||||
| @ -174,7 +192,7 @@ class ShareViewController: SLComposeServiceViewController { | ||||
|                 } | ||||
| 
 | ||||
|                 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?.synchronize() | ||||
|                     this.redirectToHostApp(type: .file) | ||||
| @ -200,7 +218,9 @@ class ShareViewController: SLComposeServiceViewController { | ||||
|     } | ||||
| 
 | ||||
|     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? | ||||
|         let selectorOpenURL = sel_registerName("openURL:") | ||||
| 
 | ||||
| @ -291,7 +311,7 @@ class ShareViewController: SLComposeServiceViewController { | ||||
|     private func getThumbnailPath(for url: URL) -> URL { | ||||
|         let fileName = Data(url.lastPathComponent.utf8).base64EncodedString().replacingOccurrences(of: "==", with: "") | ||||
|         let path = FileManager.default | ||||
|             .containerURL(forSecurityApplicationGroupIdentifier: "group.\(hostAppBundleIdentifier)")! | ||||
|             .containerURL(forSecurityApplicationGroupIdentifier: appGroupId)! | ||||
|             .appendingPathComponent("\(fileName).jpg") | ||||
|         return path | ||||
|     } | ||||
|  | ||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user
	 Christian Pauly
						Christian Pauly