11#! /bin/sh
2- # Build the OneSignal Unity demo for iOS simulator.
2+ # Build the OneSignal Unity demo for iOS simulator or device .
33#
44# Usage:
5- # ./build_ios.sh [--no-install] [--install-only] [--open]
5+ # ./build_ios.sh [--device] [--no-install] [--install-only] [--open]
6+ #
7+ # By default targets the iOS Simulator. Pass --device to build for a
8+ # connected physical device instead.
69set -eu
710
811SCRIPT_DIR=" $( cd " $( dirname " $0 " ) " && pwd) "
@@ -15,9 +18,11 @@ APP_BUNDLE_ID="com.onesignal.example"
1518INSTALL=true
1619SKIP_BUILD=false
1720OPEN_XCODE=false
21+ DEVICE_BUILD=false
1822
1923for arg in " $@ " ; do
2024 case " $arg " in
25+ --device) DEVICE_BUILD=true ;;
2126 --no-install) INSTALL=false ;;
2227 --install-only) SKIP_BUILD=true ;;
2328 --open) OPEN_XCODE=true; INSTALL=false; SKIP_BUILD=true ;;
@@ -53,9 +58,63 @@ for r,devs in d['devices'].items():
5358 SIM_NAME=$( echo " $LINE " | cut -d' |' -f2)
5459}
5560
56- SIM_UDID=" "
57- SIM_NAME=" "
58- [ " $INSTALL " = true ] && pick_simulator && echo " Target: $SIM_NAME ($SIM_UDID )" && echo " "
61+ pick_device () {
62+ LIST=$( python3 -c "
63+ import subprocess,json,sys,tempfile,os
64+ tmp = tempfile.mktemp(suffix='.json')
65+ subprocess.run(['xcrun','devicectl','list','devices','--json-output',tmp],
66+ capture_output=True, text=True)
67+ try:
68+ with open(tmp) as f: data = json.load(f)
69+ finally:
70+ try: os.unlink(tmp)
71+ except: pass
72+ for d in data.get('result',{}).get('devices',[]):
73+ identifier = d.get('identifier','')
74+ name = d.get('deviceProperties',{}).get('name','')
75+ platform = d.get('hardwareProperties',{}).get('platform','')
76+ if identifier and name and platform in ('iOS','iPadOS'):
77+ print(identifier + '|' + name)
78+ " 2> /dev/null || true)
79+ COUNT=$( printf ' %s\n' " $LIST " | grep -c . || true)
80+
81+ [ " $COUNT " -eq 0 ] && echo " No connected devices found. Plug in a device or check Xcode." && exit 1
82+ [ " $COUNT " -eq 1 ] && DEV_UDID=$( echo " $LIST " | cut -d' |' -f1) && DEV_NAME=$( echo " $LIST " | cut -d' |' -f2) && return
83+
84+ echo " Multiple devices found — pick one:"
85+ i=1
86+ printf ' %s\n' " $LIST " | while IFS=' |' read -r UDID NAME; do
87+ printf " [%d] %s (%s)\n" " $i " " $NAME " " $UDID "
88+ i=$(( i + 1 ))
89+ done
90+ printf " Choice [1-%d]: " " $COUNT "
91+ read -r CHOICE
92+ LINE=$( printf ' %s\n' " $LIST " | sed -n " ${CHOICE} p" )
93+ [ -z " $LINE " ] && echo " Invalid choice." && exit 1
94+ DEV_UDID=$( echo " $LINE " | cut -d' |' -f1)
95+ DEV_NAME=$( echo " $LINE " | cut -d' |' -f2)
96+ }
97+
98+ TARGET_UDID=" "
99+ TARGET_NAME=" "
100+
101+ if [ " $INSTALL " = true ]; then
102+ if [ " $DEVICE_BUILD " = true ]; then
103+ DEV_UDID=" "
104+ DEV_NAME=" "
105+ pick_device
106+ TARGET_UDID=" $DEV_UDID "
107+ TARGET_NAME=" $DEV_NAME "
108+ else
109+ SIM_UDID=" "
110+ SIM_NAME=" "
111+ pick_simulator
112+ TARGET_UDID=" $SIM_UDID "
113+ TARGET_NAME=" $SIM_NAME "
114+ fi
115+ echo " Target: $TARGET_NAME ($TARGET_UDID )"
116+ echo " "
117+ fi
59118
60119if [ " $OPEN_XCODE " = true ]; then
61120 WS=" $XCODE_DIR /Unity-iPhone.xcworkspace"
@@ -71,13 +130,20 @@ if [ "$SKIP_BUILD" = true ]; then
71130else
72131 [ ! -x " $UNITY " ] && echo " Unity not found at $UNITY — set UNITY_PATH" && exit 1
73132 mkdir -p " $XCODE_DIR "
74- echo " Generating Xcode project (IL2CPP / Simulator)..."
133+
134+ if [ " $DEVICE_BUILD " = true ]; then
135+ BUILD_METHOD=" BuildScript.BuildiOSDevice"
136+ echo " Generating Xcode project (IL2CPP / Device)..."
137+ else
138+ BUILD_METHOD=" BuildScript.BuildiOSSimulator"
139+ echo " Generating Xcode project (IL2CPP / Simulator)..."
140+ fi
75141 echo " Log: $LOG "
76142 echo " "
77143
78144 START=$( date +%s)
79145 " $UNITY " -batchmode -nographics -quit -buildTarget iOS \
80- -projectPath " $SCRIPT_DIR " -executeMethod BuildScript.BuildiOSSimulator \
146+ -projectPath " $SCRIPT_DIR " -executeMethod " $BUILD_METHOD " \
81147 -logFile " $LOG "
82148 ELAPSED=$(( $(date +% s) - START ))
83149
@@ -90,22 +156,28 @@ if [ -f "$XCODE_DIR/Podfile" ]; then
90156 (cd " $XCODE_DIR " && pod install --repo-update)
91157fi
92158
93- if [ " $INSTALL " = true ] && [ -n " $SIM_UDID " ]; then
159+ if [ " $INSTALL " = true ] && [ -n " $TARGET_UDID " ]; then
94160 echo " "
95- echo " Building with xcodebuild for simulator ..."
161+ echo " Building with xcodebuild..."
96162 WS=" $XCODE_DIR /Unity-iPhone.xcworkspace"
97- [ ! -d " $WS " ] && WS=" " # fall back to xcodeproj if no workspace
163+ [ ! -d " $WS " ] && WS=" "
164+
165+ if [ " $DEVICE_BUILD " = true ]; then
166+ DESTINATION=" generic/platform=iOS"
167+ else
168+ DESTINATION=" id=$TARGET_UDID "
169+ fi
98170
99171 BUILD_START=$( date +%s)
100172 if [ -n " $WS " ]; then
101173 xcodebuild -workspace " $WS " -scheme " $SCHEME " \
102- -destination " id= $SIM_UDID " \
174+ -destination " $DESTINATION " \
103175 -derivedDataPath " $DERIVED " \
104176 -quiet \
105177 build
106178 else
107179 xcodebuild -project " $XCODE_DIR /Unity-iPhone.xcodeproj" -scheme " $SCHEME " \
108- -destination " id= $SIM_UDID " \
180+ -destination " $DESTINATION " \
109181 -derivedDataPath " $DERIVED " \
110182 -quiet \
111183 build
@@ -116,7 +188,13 @@ if [ "$INSTALL" = true ] && [ -n "$SIM_UDID" ]; then
116188 APP_PATH=$( find " $DERIVED " -name " *.app" -path " */Build/Products/*" | head -1)
117189 [ -z " $APP_PATH " ] && echo " Could not find .app bundle in derived data." && exit 1
118190
119- echo " Installing on $SIM_NAME ..."
120- xcrun simctl install " $SIM_UDID " " $APP_PATH "
121- xcrun simctl launch " $SIM_UDID " " $APP_BUNDLE_ID "
191+ if [ " $DEVICE_BUILD " = true ]; then
192+ echo " Installing on $TARGET_NAME ..."
193+ xcrun devicectl device install app --device " $TARGET_UDID " " $APP_PATH "
194+ xcrun devicectl device process launch --device " $TARGET_UDID " " $APP_BUNDLE_ID "
195+ else
196+ echo " Installing on $TARGET_NAME ..."
197+ xcrun simctl install " $TARGET_UDID " " $APP_PATH "
198+ xcrun simctl launch " $TARGET_UDID " " $APP_BUNDLE_ID "
199+ fi
122200fi
0 commit comments