How to Use Pod in Notification Service Extension

Unable to use pod in Notification service extension

First of all MORichNotification is meant only for the Notification Service Extension, I can see from your podfile that you are including it in your app's target too.

And also you can use MORichNotifications, only if you are using MoEngage-iOS-SDK in your app, as it will process only the notifications sent via MoEngage.

Now, follow the below steps to use MORichNotifications :

  1. Install the MORichNotification pod in your Notification Service Extension. And please make sure that you are using version 1.1.1.
  2. Add Bridging Header to your Notification Service Extension
    Sample Image
  3. Import MORichNotification in Bridging Header as shown in the image below:

Sample Image


  1. Use MORichNotification in your Notification Service Extension as shown below :
    Sample Image
    Let me know if still you are facing any issue.

How can I add an app-extension into a private pod?

My understanding is that you can't. Nor you should. The parent app should know of all its targets itself. And given that every App Extension's bundleId is prefixed with with the bundleId of it parent app, it would require a lot of build tool wizardly to get the bundleId, plist, entitlements, provisioning profile, code signing right from the pod itself.

What you should do is intercept the notification from the host app, and then based on whatever parameter you have, you pass it onto your pod's serviceExtensionHandler.

Yet still if you don't want the headache of having multiple targets in your app then you can have a single target within the parent app and use some scripts to change the all the bundleId, plist, entitlements, provisioning profile, code signing

The best answer I found online was written by damian-rzeszot from this gist originally written and referenced from here:

alias plistbuddy=/usr/libexec/PlistBuddy
alias codesign=/usr/bin/codesign

#
# Bundle identifier
#

set_plist_bundle_identifier() {
local bundle_identifier="$1"
local plist_file="$2"

plistbuddy \
-c "set :CFBundleIdentifier $bundle_identifier" \
"$plist_file"
}

set_appex_bundle_identifier() {
local appex_target_name="$1"
local bundle_identifier_suffix="$2"

local bundle_identifier="$PRODUCT_BUNDLE_IDENTIFIER.$bundle_identifier_suffix"
local plist_file="$BUILT_PRODUCTS_DIR/$FULL_PRODUCT_NAME/$BUNDLE_PLUGINS_FOLDER_PATH/$appex_target_name.appex/Info.plist"

set_plist_bundle_identifier "$bundle_identifier" "$plist_file"
}

#
# Bundle version
#

set_plist_bundle_version() {
local bundle_version="$1"
local plist_file="$2"

plistbuddy \
-c "set :CFBundleShortVersionString $bundle_version" \
"$plist_file"
}

get_plsit_bundle_version() {
local plist_file="$1"

plistbuddy \
-c "Print :CFBundleShortVersionString" \
"$plist_file"
}

get_app_bundle_version() {
local plist_file="$BUILT_PRODUCTS_DIR/$INFOPLIST_PATH"
get_plsit_bundle_version "$plist_file"
}

set_appex_bundle_version() {
local appex_target_name="$1"
local bundle_version="$2"

local plist_file="$BUILT_PRODUCTS_DIR/$FULL_PRODUCT_NAME/$BUNDLE_PLUGINS_FOLDER_PATH/$appex_target_name.appex/Info.plist"

set_plist_bundle_version "$bundle_version" "$plist_file"
}

#
# Bundle build
#

set_plist_bundle_build() {
local bundle_build="$1"
local plist_file="$2"

plistbuddy \
-c "set :CFBundleVersion $bundle_build" \
"$plist_file"
}

get_plist_bundle_build() {
local plist_file="$1"

plistbuddy \
-c "Print :CFBundleVersion" \
"$plist_file"
}

set_appex_bundle_build() {
local appex_target_name="$1"
local bundle_version="$2"

local plist_file="$BUILT_PRODUCTS_DIR/$FULL_PRODUCT_NAME/$BUNDLE_PLUGINS_FOLDER_PATH/$appex_target_name.appex/Info.plist"

set_plist_bundle_build "$bundle_version" "$plist_file"
}

get_app_bundle_build() {
local plist_file="$BUILT_PRODUCTS_DIR/$INFOPLIST_PATH"
get_plist_bundle_build "$plist_file"
}

#
# Code signing
#

prepare_entitlements_file() {
local appex_target_name="$1"
local bundle_identifier_suffix="$2"
local output_file="$3"

local original_entitlements="$CONFIGURATION_TEMP_DIR/$appex_target_name.build/$appex_target_name.appex.xcent"
local bundle_identifier="$DEVELOPMENT_TEAM.$PRODUCT_BUNDLE_IDENTIFIER.$bundle_identifier_suffix"

cp "$original_entitlements" "$output_file"

if [[ $CONFIGURATION == "Release" ]]
then
plistbuddy \
-c "set :application-identifier $bundle_identifier" \
"$output_file"

plistbuddy \
-c "set :keychain-access-groups:0 $bundle_identifier" \
"$output_file"
fi
}

copy_provisioning_profile() {
local appex_target_name="$1"
local provision_source="$2"

local provision_destination="$BUILT_PRODUCTS_DIR/$FULL_PRODUCT_NAME/$BUNDLE_PLUGINS_FOLDER_PATH/$appex_target_name.appex/$EMBEDDED_PROFILE_NAME"

cp "$provision_source" "$provision_destination"
}

resign_appex() {
local appex_target_name="$1"
local entitlements_file="$2"

codesign \
--force \
--sign "$EXPANDED_CODE_SIGN_IDENTITY" \
--entitlements "$entitlements_file" \
--timestamp=none \
"$BUILT_PRODUCTS_DIR/$FULL_PRODUCT_NAME/$BUNDLE_PLUGINS_FOLDER_PATH/$appex_target_name.appex"
}

#
#
#
#
#

set_appex_bundle_identifier \
"NotificationService" \
"notification-service"

set_appex_bundle_version \
"NotificationService" \
`get_app_bundle_version`

set_appex_bundle_build \
"NotificationService" \
`get_app_bundle_build`

# Be careful if using `keychain-access-groups` entitlement
prepare_entitlements_file \
"NotificationService" \
"notification-service" \
"$DERIVED_SOURCES_DIR/NotificationService-Entitlements.plist"

copy_provisioning_profile \
"NotificationService" \
"$SOURCE_ROOT/../.github/appstore/$TARGET_NAME/profiles/notification-service.mobileprovision"

resign_appex \
"NotificationService" \
"$DERIVED_SOURCES_DIR/NotificationService-Entitlements.plist"

Swift - Can I use realm in Notification Service Extension?

Yes it works, you need to create an AppGroup in order to share data between your app & the Extension.

Use Cocoapods with an App Extension

The proper way to do this is to update your podfile to add just 1 line :

link_with 'yourApp', 'yourAppExtension'

and a pod update should resolve the issue.

How do you incorporate/access pod files (MMWormhole) in an extension?

I got it to work by adding the pod to both the main app and the extension, how to do that itself is not necessarily intuitive, so here's my pod file for anybody else in the same situation:

workspace 'MyWorkspace'
target 'My App' do
use_frameworks!
pod 'MMWormhole', '~> 2.0.0'
...

target 'My Extension' do
use_frameworks!
pod 'MMWormhole', '~> 2.0.0'
project 'Path to the .xcodeproj'
end

After adding Extension I am getting Lexical and PreProcessor error Xcode 9.3?

Finally:

The issue is because of the created extension carrying the header path as Parent file header path , So if you mentioned any pod file header path in you prefix then it will give you the error , So remove the header path and kept it as empty .

Build Setting -> HeaderPath -> Remove it .

Make sure you have selected the extension target while removing the path .



Related Topics



Leave a reply



Submit