mirror of
https://github.com/expo/config-plugins.git
synced 2026-11-13 20:28:07 -10:00
refactor(ios): migrate to new expo modules system
fix: fixed correct class name fix: fixed application:continue:restorationHandler
This commit is contained in:
7 files changed
+63
-135
No files matched your search
-4
@@ -3,8 +3,4 @@ import type { ExpoConfig } from "@expo/config-types";
|
||||
import type { ConfigData } from "./types";
|
||||
export declare function getBranchApiKey(config: Pick<ExpoConfig, "ios">): string | null;
|
||||
export declare function setBranchApiKey(apiKey: string, infoPlist: InfoPlist): InfoPlist;
|
||||
export declare function updateApplicationDidFinishLaunchingWithOptions(contents: string): string;
|
||||
export declare function updateApplicationOpenURLWithOptions(contents: string): string;
|
||||
export declare function updateApplicationContinueUserActivity(contents: string): string;
|
||||
export declare const modifyAppDelegateObjc: (contents: string) => string;
|
||||
export declare const withBranchIOS: ConfigPlugin<ConfigData>;
|
||||
+1
-47
@@ -1,8 +1,7 @@
|
||||
"use strict";
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.withBranchIOS = exports.modifyAppDelegateObjc = exports.updateApplicationContinueUserActivity = exports.updateApplicationOpenURLWithOptions = exports.updateApplicationDidFinishLaunchingWithOptions = exports.setBranchApiKey = exports.getBranchApiKey = void 0;
|
||||
exports.withBranchIOS = exports.setBranchApiKey = exports.getBranchApiKey = void 0;
|
||||
const config_plugins_1 = require("@expo/config-plugins");
|
||||
const codeMod_1 = require("@expo/config-plugins/build/ios/codeMod");
|
||||
function getBranchApiKey(config) {
|
||||
var _a, _b, _c, _d;
|
||||
return (_d = (_c = (_b = (_a = config.ios) === null || _a === void 0 ? void 0 : _a.config) === null || _b === void 0 ? void 0 : _b.branch) === null || _c === void 0 ? void 0 : _c.apiKey) !== null && _d !== void 0 ? _d : null;
|
||||
@@ -20,44 +19,6 @@ function setBranchApiKey(apiKey, infoPlist) {
|
||||
};
|
||||
}
|
||||
exports.setBranchApiKey = setBranchApiKey;
|
||||
function updateApplicationDidFinishLaunchingWithOptions(contents) {
|
||||
// application:didFinishLaunchingWithOptions:
|
||||
const initSessionWithLaunchOptions = "[RNBranch initSessionWithLaunchOptions:launchOptions isReferrable:YES];";
|
||||
if (!contents.includes(` ${initSessionWithLaunchOptions}`)) {
|
||||
contents = codeMod_1.insertContentsInsideObjcFunctionBlock(contents, "application:didFinishLaunchingWithOptions:", initSessionWithLaunchOptions, { position: "head", indent: 2 });
|
||||
}
|
||||
return contents;
|
||||
}
|
||||
exports.updateApplicationDidFinishLaunchingWithOptions = updateApplicationDidFinishLaunchingWithOptions;
|
||||
function updateApplicationOpenURLWithOptions(contents) {
|
||||
// application:openURL:options:
|
||||
const branchOpenURLWithOptions = "[RNBranch application:application openURL:url options:options];";
|
||||
if (!contents.includes(` ${branchOpenURLWithOptions}`)) {
|
||||
contents = codeMod_1.insertContentsInsideObjcFunctionBlock(contents, "application:openURL:options:", branchOpenURLWithOptions, { position: "tailBeforeLastReturn", indent: 2 });
|
||||
}
|
||||
return contents;
|
||||
}
|
||||
exports.updateApplicationOpenURLWithOptions = updateApplicationOpenURLWithOptions;
|
||||
function updateApplicationContinueUserActivity(contents) {
|
||||
// application:continueUserActivity:restorationHandler:
|
||||
const branchContinueUserActivity = "if ([RNBranch continueUserActivity:userActivity]) { return YES; }";
|
||||
if (!contents.includes(` ${branchContinueUserActivity}`)) {
|
||||
contents = codeMod_1.insertContentsInsideObjcFunctionBlock(contents, "application:continueUserActivity:restorationHandler:", branchContinueUserActivity, { position: "tailBeforeLastReturn", indent: 2 });
|
||||
}
|
||||
return contents;
|
||||
}
|
||||
exports.updateApplicationContinueUserActivity = updateApplicationContinueUserActivity;
|
||||
const modifyAppDelegateObjc = (contents) => {
|
||||
// Add imports if needed
|
||||
if (!contents.match(/^#import\s+<RNBranch\/RNBranch\.h>\s*$/m)) {
|
||||
contents = codeMod_1.addObjcImports(contents, ["<RNBranch/RNBranch.h>"]);
|
||||
}
|
||||
contents = updateApplicationDidFinishLaunchingWithOptions(contents);
|
||||
contents = updateApplicationOpenURLWithOptions(contents);
|
||||
contents = updateApplicationContinueUserActivity(contents);
|
||||
return contents;
|
||||
};
|
||||
exports.modifyAppDelegateObjc = modifyAppDelegateObjc;
|
||||
const withBranchIOS = (config, data) => {
|
||||
var _a;
|
||||
// Ensure object exist
|
||||
@@ -86,13 +47,6 @@ const withBranchIOS = (config, data) => {
|
||||
}
|
||||
return config;
|
||||
});
|
||||
config = config_plugins_1.withAppDelegate(config, (config) => {
|
||||
if (config.modResults.language === "swift") {
|
||||
throw new Error("Branch is not supported in Swift");
|
||||
}
|
||||
config.modResults.contents = exports.modifyAppDelegateObjc(config.modResults.contents);
|
||||
return config;
|
||||
});
|
||||
return config;
|
||||
};
|
||||
exports.withBranchIOS = withBranchIOS;
|
||||
@@ -1,5 +1,9 @@
|
||||
{
|
||||
"platforms": [
|
||||
"android"
|
||||
]
|
||||
"android",
|
||||
"ios"
|
||||
],
|
||||
"ios": {
|
||||
"appDelegateSubscribers": ["AppLifecycleDelegate"]
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
require 'json'
|
||||
|
||||
package = JSON.parse(File.read(File.join(__dir__, '..', 'package.json')))
|
||||
|
||||
Pod::Spec.new do |s|
|
||||
s.name = 'EXBranch'
|
||||
s.version = package['version']
|
||||
s.summary = package['description']
|
||||
s.description = package['description']
|
||||
s.license = package['license']
|
||||
s.author = package['author']
|
||||
s.homepage = package['homepage']
|
||||
s.platform = :ios, '12.0'
|
||||
s.swift_version = '5.4'
|
||||
s.source = { git: 'https://github.com/expo/config-plugins.git' }
|
||||
s.static_framework = true
|
||||
|
||||
s.compiler_flags = %[-DRNBRANCH_VERSION=@\\"#{package["dependencies"]["react-native-branch"]}\\"]
|
||||
|
||||
s.dependency 'ExpoModulesCore'
|
||||
s.dependency 'React-Core'
|
||||
s.dependency 'react-native-branch'
|
||||
|
||||
# Swift/Objective-C compatibility
|
||||
s.pod_target_xcconfig = {
|
||||
'DEFINES_MODULE' => 'YES'
|
||||
}
|
||||
|
||||
if !$ExpoUseSources&.include?(package['name']) && ENV['EXPO_USE_SOURCE'].to_i == 0 && File.exist?("#{s.name}.xcframework") && Gem::Version.new(Pod::VERSION) >= Gem::Version.new('1.10.0')
|
||||
s.source_files = "#{s.name}/**/*.h"
|
||||
s.vendored_frameworks = "#{s.name}.xcframework"
|
||||
else
|
||||
s.source_files = "#{s.name}/**/*.{h,m,swift}"
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,17 @@
|
||||
import ExpoModulesCore
|
||||
import RNBranch
|
||||
|
||||
public class AppLifecycleDelegate: ExpoAppDelegateSubscriber {
|
||||
public func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool {
|
||||
RNBranch.initSession(launchOptions: launchOptions, isReferrable: true)
|
||||
return true
|
||||
}
|
||||
|
||||
public func application(_ application: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey : Any] = [:]) -> Bool {
|
||||
return RNBranch.application(application, open:url, options:options)
|
||||
}
|
||||
|
||||
public func application(_ application: UIApplication, continue userActivity: NSUserActivity, restorationHandler: @escaping ([UIUserActivityRestoring]?) -> Void) -> Bool {
|
||||
return RNBranch.continue(userActivity)
|
||||
}
|
||||
}
|
||||
@@ -2,6 +2,9 @@
|
||||
"name": "@config-plugins/react-native-branch",
|
||||
"version": "0.0.1",
|
||||
"description": "Config plugin to auto configure react-native-branch on prebuild",
|
||||
"author": "650 Industries, Inc.",
|
||||
"license": "MIT",
|
||||
"homepage": "https://docs.expo.dev/versions/latest/sdk/branch/",
|
||||
"main": "build/withBranch.js",
|
||||
"types": "build/withBranch.d.ts",
|
||||
"sideEffects": false,
|
||||
|
||||
@@ -1,11 +1,7 @@
|
||||
import { withAppDelegate, withInfoPlist } from "@expo/config-plugins";
|
||||
import { withInfoPlist } from "@expo/config-plugins";
|
||||
import type { ConfigPlugin, InfoPlist } from "@expo/config-plugins";
|
||||
import type { ExpoConfig } from "@expo/config-types";
|
||||
import type { ConfigData } from "./types";
|
||||
import {
|
||||
addObjcImports,
|
||||
insertContentsInsideObjcFunctionBlock,
|
||||
} from "@expo/config-plugins/build/ios/codeMod";
|
||||
|
||||
export function getBranchApiKey(config: Pick<ExpoConfig, "ios">) {
|
||||
return config.ios?.config?.branch?.apiKey ?? null;
|
||||
@@ -27,71 +23,6 @@ export function setBranchApiKey(
|
||||
};
|
||||
}
|
||||
|
||||
export function updateApplicationDidFinishLaunchingWithOptions(
|
||||
contents: string
|
||||
): string {
|
||||
// application:didFinishLaunchingWithOptions:
|
||||
const initSessionWithLaunchOptions =
|
||||
"[RNBranch initSessionWithLaunchOptions:launchOptions isReferrable:YES];";
|
||||
if (!contents.includes(` ${initSessionWithLaunchOptions}`)) {
|
||||
contents = insertContentsInsideObjcFunctionBlock(
|
||||
contents,
|
||||
"application:didFinishLaunchingWithOptions:",
|
||||
initSessionWithLaunchOptions,
|
||||
{ position: "head", indent: 2 }
|
||||
);
|
||||
}
|
||||
|
||||
return contents;
|
||||
}
|
||||
|
||||
export function updateApplicationOpenURLWithOptions(contents: string): string {
|
||||
// application:openURL:options:
|
||||
const branchOpenURLWithOptions =
|
||||
"[RNBranch application:application openURL:url options:options];";
|
||||
if (!contents.includes(` ${branchOpenURLWithOptions}`)) {
|
||||
contents = insertContentsInsideObjcFunctionBlock(
|
||||
contents,
|
||||
"application:openURL:options:",
|
||||
branchOpenURLWithOptions,
|
||||
{ position: "tailBeforeLastReturn", indent: 2 }
|
||||
);
|
||||
}
|
||||
|
||||
return contents;
|
||||
}
|
||||
|
||||
export function updateApplicationContinueUserActivity(
|
||||
contents: string
|
||||
): string {
|
||||
// application:continueUserActivity:restorationHandler:
|
||||
const branchContinueUserActivity =
|
||||
"if ([RNBranch continueUserActivity:userActivity]) { return YES; }";
|
||||
if (!contents.includes(` ${branchContinueUserActivity}`)) {
|
||||
contents = insertContentsInsideObjcFunctionBlock(
|
||||
contents,
|
||||
"application:continueUserActivity:restorationHandler:",
|
||||
branchContinueUserActivity,
|
||||
{ position: "tailBeforeLastReturn", indent: 2 }
|
||||
);
|
||||
}
|
||||
|
||||
return contents;
|
||||
}
|
||||
|
||||
export const modifyAppDelegateObjc = (contents: string): string => {
|
||||
// Add imports if needed
|
||||
if (!contents.match(/^#import\s+<RNBranch\/RNBranch\.h>\s*$/m)) {
|
||||
contents = addObjcImports(contents, ["<RNBranch/RNBranch.h>"]);
|
||||
}
|
||||
|
||||
contents = updateApplicationDidFinishLaunchingWithOptions(contents);
|
||||
contents = updateApplicationOpenURLWithOptions(contents);
|
||||
contents = updateApplicationContinueUserActivity(contents);
|
||||
|
||||
return contents;
|
||||
};
|
||||
|
||||
export const withBranchIOS: ConfigPlugin<ConfigData> = (config, data) => {
|
||||
// Ensure object exist
|
||||
if (!config.ios) {
|
||||
@@ -120,17 +51,5 @@ export const withBranchIOS: ConfigPlugin<ConfigData> = (config, data) => {
|
||||
return config;
|
||||
});
|
||||
|
||||
config = withAppDelegate(config, (config) => {
|
||||
if (config.modResults.language === "swift") {
|
||||
throw new Error("Branch is not supported in Swift");
|
||||
}
|
||||
|
||||
config.modResults.contents = modifyAppDelegateObjc(
|
||||
config.modResults.contents
|
||||
);
|
||||
|
||||
return config;
|
||||
});
|
||||
|
||||
return config;
|
||||
};
|
||||
Reference in new issue
Block a user