Hi,
Here I am going to explain the different steps of implementing the Share Extension in iOS with swift language. Before going to the implementation part, we should know what share extension is and what its purpose is. Share Extension first came into light from “iOS 8.0”, which is used to share the content from the device on the app. These content are basically image, text, video etc.
According to Apple, “Share extensions give users a convenient way to share content with other entities, such as social sharing websites or upload services“. This feature gives a user to post the content easily on the app, they just have to click on the share button and select the app from the sharing area, that open the System generate UI.
Implementation of Share Extension with Steps
Step1=> Basically, share extension is not a stand-alone application. So, first, we have to create a project called “Share Extension Demo”. Make UI which looks in the following way:-
Step2 => Now we have to add the share extension into our project. For adding share extension, Go to File > New > Target, create the share Extension file called ShareExt.
Step3 => For sharing data between extension and host-app we need to setup App-Groups in capabilities section. Open Share Extension Demo target > Capabilities. Enable App groups and select group.YOUR_BUNDLE_ID.
Step4=> Add the same in shareExt, as shown in the image below:-
Step5=> Now configuration of the share extension is almost done. From your project, you will see a Share Extension folder, which consists of shareViewController that inherits the SLComposeViewController.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
import UIKit import Social import MobileCoreServices class ShareViewController: SLComposeServiceViewController { var imageType = "" override func isContentValid() -> Bool { // Do validation of contentText and/or NSExtensionContext attachments here return true } override func didSelectPost() { // saving the data which is shared byu user or whatever the action we want to perform will be write in this methos } } |
In this project, we share the image from the photos app and show into the Main app. To achieve this, we use UserDefault to save the image data.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
override func didSelectPost() { print("In Did Post") if let item = self.extensionContext?.inputItems[0] as? NSExtensionItem{ print("Item \(item)") for ele in item.attachments!{ print("item.attachments!======>>> \(ele as! NSItemProvider)") let itemProvider = ele as! NSItemProvider print(itemProvider) if itemProvider.hasItemConformingToTypeIdentifier("public.jpeg"){ imageType = "public.jpeg" } if itemProvider.hasItemConformingToTypeIdentifier("public.png"){ imageType = "public.png" } print("imageType\(imageType)") if itemProvider.hasItemConformingToTypeIdentifier(imageType){ print("True") itemProvider.loadItem(forTypeIdentifier: imageType, options: nil, completionHandler: { (item, error) in var imgData: Data! if let url = item as? URL{ imgData = try! Data(contentsOf: url) } if let img = item as? UIImage{ imgData = UIImagePNGRepresentation(img) } print("Item ===\(item)") print("Image Data=====. \(imgData))") let dict: [String : Any] = ["imgData" : imgData, "name" : self.contentText] let savedata = UserDefaults.init(suiteName: "group.com.YourAppBundleID") savedata?.set(dict, forKey: "img") savedata?.synchronize() print("ImageData \(String(describing: savedata?.value(forKey: "img")))") }) } } } // self.extensionContext!.completeRequest(returningItems: [], completionHandler: nil) } |
Step=>6 Now, In View Controller class we try to access the shared image from user default.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
import UIKit class ViewController: UIViewController { @IBOutlet weak var imgView: UIImageView! @IBOutlet weak var lblText: UILabel! override func viewDidLoad() { super.viewDidLoad() print("In View Did Load") } override func viewWillAppear(_ animated: Bool) { super.viewWillAppear(animated) print("InAppear") let savedata = UserDefaults.init(suiteName: "group.com.jft.ShareExt.ShareExt") print("ImageData \(String(describing: savedata?.value(forKey: "img")))") if savedata?.value(forKey: "img") != nil { print("Available Data") let data = ((savedata?.value(forKey: "img")as! NSDictionary).value(forKey: "imgData")as! Data) let str = ((savedata?.value(forKey: "img")as! NSDictionary).value(forKey: "name")as! String) self.imgView.image = UIImage(data: data) self.lblText.text = str } } |
Great, we completed all the steps to integrate the share extension in our app. Now, Select the ShareExt and run the project. You will get the following screen:-
After selecting the photos, which will redirect you to the native photos application of iOS, select any image and click on the share button below, then you will see that your app exists there.
Hey, Now you have to run your main app i.e. ShareExtensionDemo. After successfully run, stop the application and follow these steps:-
- Go to the gallery into your iPhone.
- Select an image.
- Click on the share button.
- Select your app to share.
- After Share, the image opens your app.
Note:- You have to create the certificate and provisioning for both ShareExt and ShareExtensionDemo on Apple Developer Account.
Hurray, You have successfully implemented the Share Extension to your app. If you have any queries, then please ask in the comment.
Thank you, Good Day ?
Recent Comments