TechBee
TechBee

Reputation: 1941

Data sharing issue in iOS ShareExtension app

I am working on a ShareExtension app.
I want to share data from Container App to ShareExtension app.

Following is the code I am using In Container App View Controller:

let userDefaultx = NSUserDefaults(suiteName: "group.myapps.userConnect")
userDefaultx!.setObject("new user", forKey:"user")
userDefaultx!.synchronize()

Now I am trying to capture the data in ShareExtension View Controller:

let userDefaults = NSUserDefaults(suiteName: "group.myapps.userConnect")
let user = userDefaults?.valueForKey("user")
print(" user \(user) ") // returns nil

I am using simulator and I have not used the default SLComposeServiceViewController.
Instead, I have used regular UIViewController.

What could be the issue I am missing?

Upvotes: 0

Views: 291

Answers (1)

iGatiTech
iGatiTech

Reputation: 2268

Follow below steps to successfully integrate share extension with your app,

  1. Add New target in your application

enter image description here

  1. Select Share extension -> Click next

enter image description here

  1. You will receive one pop up alert -> Click on Activate button

enter image description here

  1. Check your app's bundle identifier

enter image description here

  1. Check your extension's bundle identifier

enter image description here

  1. Login to your Apple Developer Portal -> Click on Certificates, Identifiers & Profiles section -> Select App Groups under Identifiers sections -> Click on Add New App Group

enter image description here

  1. Enter your App Group Name -> Enter it's identifier which should start with the word group.

enter image description here

  1. Create New App ID for your application

enter image description here

  1. Enter App ID name and your app's bundle identifier.

enter image description here

  1. Enable App Group -> Click Done

enter image description here

  1. Now select the App Id which you just created -> Click Edit

enter image description here

  1. Click Edit just next to App Group

enter image description here

  1. Select the App Group which you create in Step 7 -> Click Done

  2. Generate Provisioning Profile with the App ID which you created and use that in your project

  3. Create New App ID for your application's extension

enter image description here

  1. Enter App ID name and your app's extension's bundle identifier.

enter image description here

  1. Follow Step no.10 to Step no.14 again

  2. You must be generated two(development/distribution) profiles for your App as well as for your extension

  3. Here I am showing one example of generating development profile for your application -> Select Development under Provisioning Profiles -> Click Add

enter image description here

  1. Select iOS App Development -> Click Continue

enter image description here

  1. Select the App ID which you created for your app in Step 10 -> Click Continue -> Name it -> Download it -> Install it

enter image description here

  1. Set that profile in you project target

enter image description here

  1. Follow Step 19 to 22 for generating development provisioning profile for your extension. Here select the App ID which you created for your extension. you need to follow these same steps for generating your distribution provisioning profile when you need to live your application on App Store.

  2. Set All Development/Distribution Profiles in your project

  3. Open your project -> Select app target -> Capabilities -> Enable App Group -> Select the App Group which you created in Step 7

enter image description here

  1. Select Extension -> Capabilities -> Enable App Group -> Select the App Group which you created in Step 7

Now if you want to share data between your App and your extension, use below code,

In your App,

let defaults = UserDefaults(suiteName: "group.com.myapp") // Your App Group name
defaults?.set(str_user_token, forKey:"user_token")
defaults?.synchronize()

In your Share Extension File, access it using below code,

let defaults = UserDefaults(suiteName: "group.com.myapp") // Your App Group name
let restoredValue = defaults!.string(forKey: "user_token")

You are All set for Share Extension! Happy Coding. Cheers!

UPDATE:

Find the Updated answer here with latest screenshots and every small detail to integrate Share Extension successfully in your iOS Application: https://igati.tech/ios-share-extension/

Upvotes: 3

Related Questions