Skip to content

Commit

Permalink
Fixed keychain helper to be available in extension too
Browse files Browse the repository at this point in the history
  • Loading branch information
BreckoEC committed Mar 8, 2023
1 parent cf5507c commit 1367c02
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 14 deletions.
13 changes: 9 additions & 4 deletions ios/KeychainHelper.swift
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
import Foundation

/**
This class is used to share data with the main app
*/
struct KeychainHelper {
static func getValue(forKey key: String, inGroup group: String) -> String? {
private static let group = (Bundle.main.object(forInfoDictionaryKey: "ShareExtensionKeychainAccessGroup") as? String)!

static func getValue(forKey key: String) -> String? {
let query: [String: Any] = [
kSecClass as String: kSecClassGenericPassword,
kSecAttrAccount as String: key,
Expand All @@ -25,8 +30,8 @@ struct KeychainHelper {
return result
}

static func setValue(_ value: String, forKey key: String, inGroup group: String) {
removeValue(forKey: key, inGroup: group)
static func setValue(_ value: String, forKey key: String) {
removeValue(forKey: key)

let valueData = value.data(using: .utf8)
let query: [String: Any] = [
Expand All @@ -40,7 +45,7 @@ struct KeychainHelper {
SecItemAdd(query as CFDictionary, nil)
}

static func removeValue(forKey key: String, inGroup group: String) {
static func removeValue(forKey key: String) {
let query: [String: Any] = [
kSecClass as String: kSecClassGenericPassword,
kSecAttrAccount as String: key,
Expand Down
12 changes: 3 additions & 9 deletions ios/ShareExtensionModule.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,19 @@ import ExpoModulesCore
import Foundation

public class ShareExtensionNativeModule: Module {
var group: String?

public func definition() -> ModuleDefinition {
Name("ShareExtensionNative")

OnCreate {
group = (Bundle.main.object(forInfoDictionaryKey: "ShareExtensionKeychainAccessGroup") as? String)!
}

Function("getKeychainValue") { (key: String) -> String? in
return KeychainHelper.getValue(forKey: key, inGroup: group!)
return KeychainHelper.getValue(forKey: key)
}

Function("setKeychainValue") { (key: String, value: String) -> Void in
KeychainHelper.setValue(value, forKey: key, inGroup: group!)
KeychainHelper.setValue(value, forKey: key)
}

Function("removeKeychainValue") { (key: String) -> Void in
KeychainHelper.removeValue(forKey: key, inGroup: group!)
KeychainHelper.removeValue(forKey: key)
}

Function("sendMessageIntent") { (recipients: [User], sender: User, conversationId: String, groupName: String?, groupPicture: String?) -> Void in
Expand Down
2 changes: 2 additions & 0 deletions plugin/assets/ios/ShareExtension-Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,7 @@
<key>NSExtensionPointIdentifier</key>
<string>com.apple.share-services</string>
</dict>
<key>ShareExtensionKeychainAccessGroup</key>
<string>{{ShareExtensionKeychainAccessGroup}}</string>
</dict>
</plist>
8 changes: 7 additions & 1 deletion plugin/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ const EXTENSION_TARGET_NAME = 'ShareExtension'
const ENTITLEMENTS_FILENAME = `${EXTENSION_TARGET_NAME}.entitlements`
const INFO_PLIST_FILENAME = `${EXTENSION_TARGET_NAME}-Info.plist`

const KEYCHAIN_HELPER_FILENAME = 'KeychainHelper.swift'
const KEYCHAIN_HELPER_PATH = `../../ios/${KEYCHAIN_HELPER_FILENAME}`

const DEFAULT_DEPLOYMENT_TARGET = '15.0'
const DEFAULT_SWIFT_VERSION = '5.7'

Expand All @@ -28,6 +31,7 @@ const REGEX_BUNDLE_SHORT_VERSION = /{{CFBundleShortVersionString}}/gm
const REGEX_BUNDLE_VERSION = /{{CFBundleVersion}}/gm
const REGEX_EXTENSION_ACTIVATION_RULE = /{{NSExtensionActivationRule}}/gm
const REGEX_EXTENSION_MAIN_STORYBOARD = /{{NSExtensionMainStoryboard}}/gm
const REGEX_SHARE_EXTENTION_KEYCHAIN_ACCESS_GROUP = /{{ShareExtensionKeychainAccessGroup}}/gm

function getKeychainAccessGroup(config: ExpoConfig): string {
return `$(AppIdentifierPrefix)${config.ios!.bundleIdentifier!}`
Expand Down Expand Up @@ -135,6 +139,7 @@ const withShareExtensionTarget: ConfigPlugin<ShareExtensionPluginProps> = (confi
const allFiles = [
`${source}/${ENTITLEMENTS_FILENAME}`,
`${source}/${INFO_PLIST_FILENAME}`,
KEYCHAIN_HELPER_PATH,
...sourceFiles,
...resourceFiles,
]
Expand All @@ -156,6 +161,7 @@ const withShareExtensionTarget: ConfigPlugin<ShareExtensionPluginProps> = (confi
infoPlistFile = infoPlistFile.replace(REGEX_BUNDLE_VERSION, bundleVersion)
infoPlistFile = infoPlistFile.replace(REGEX_EXTENSION_MAIN_STORYBOARD, mainStoryboardName)
infoPlistFile = infoPlistFile.replace(REGEX_EXTENSION_ACTIVATION_RULE, activationRule)
infoPlistFile = infoPlistFile.replace(REGEX_SHARE_EXTENTION_KEYCHAIN_ACCESS_GROUP, getKeychainAccessGroup(config))
jetpack.write(infoPlistPath, infoPlistFile)

// Add assets in an xcode 'PBXGroup' (xcode folders)
Expand Down Expand Up @@ -194,7 +200,7 @@ const withShareExtensionTarget: ConfigPlugin<ShareExtensionPluginProps> = (confi

// Add build phases to the new target
xcodeProject.addBuildPhase(
sourceFiles.map((x) => path.basename(x)),
[KEYCHAIN_HELPER_FILENAME, ...sourceFiles.map((x) => path.basename(x))],
'PBXSourcesBuildPhase',
'Sources',
target.uuid
Expand Down

0 comments on commit 1367c02

Please sign in to comment.