简体   繁体   中英

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?

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

  1. Add New target in your application

在此处输入图片说明

  1. Select Share extension -> Click next

在此处输入图片说明

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

在此处输入图片说明

  1. Check your app's bundle identifier

在此处输入图片说明

  1. Check your extension's bundle identifier

在此处输入图片说明

  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

在此处输入图片说明

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

在此处输入图片说明

  1. Create New App ID for your application

在此处输入图片说明

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

在此处输入图片说明

  1. Enable App Group -> Click Done

在此处输入图片说明

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

在此处输入图片说明

  1. Click Edit just next to App Group

在此处输入图片说明

  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

在此处输入图片说明

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

在此处输入图片说明

  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

在此处输入图片说明

  1. Select iOS App Development -> Click Continue

在此处输入图片说明

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

在此处输入图片说明

  1. Set that profile in you project target

在此处输入图片说明

  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

在此处输入图片说明

  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/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM