Reputation: 1941
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
Reputation: 2268
Follow below steps to successfully integrate share extension with your app,
- Add New target in your application
- Select Share extension -> Click next
- You will receive one pop up alert -> Click on Activate button
- Check your app's bundle identifier
- Check your extension's bundle identifier
- 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 your App Group Name -> Enter it's identifier which should start with the word group.
- Create New App ID for your application
- Enter App ID name and your app's bundle identifier.
- Enable App Group -> Click Done
- Now select the App Id which you just created -> Click Edit
- Click Edit just next to App Group
Select the App Group which you create in Step 7 -> Click Done
Generate Provisioning Profile with the App ID which you created and use that in your project
Create New App ID for your application's extension
- Enter App ID name and your app's extension's bundle identifier.
Follow Step no.10 to Step no.14 again
You must be generated two(development/distribution) profiles for your App as well as for your extension
Here I am showing one example of generating development profile for your application -> Select Development under Provisioning Profiles -> Click Add
- Select iOS App Development -> Click Continue
- Select the App ID which you created for your app in Step 10 -> Click Continue -> Name it -> Download it -> Install it
- Set that profile in you project target
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.
Set All Development/Distribution Profiles in your project
Open your project -> Select app target -> Capabilities -> Enable App Group -> Select the App Group which you created in Step 7
- 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