Skip to content

Instantly share code, notes, and snippets.

@bellbind
Last active April 15, 2021 09:03
Show Gist options
  • Save bellbind/20b59a4ab86d11e5e358a472c7ae5986 to your computer and use it in GitHub Desktop.
Save bellbind/20b59a4ab86d11e5e358a472c7ae5986 to your computer and use it in GitHub Desktop.
[macosx][ios][swift3]build and run minimal ios app for simulator with command line only (Xcode-8.1 env, without Storyboard and InterfaceBuilder)
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>CFBundleDevelopmentRegion</key>
<string>en</string>
<key>CFBundleExecutable</key>
<string>webkit</string>
<key>CFBundleIdentifier</key>
<string>bellbind.example.webkit</string>
<key>CFBundleInfoDictionaryVersion</key>
<string>6.0</string>
<key>CFBundleName</key>
<string>webkitexample</string>
<key>CFBundlePackageType</key>
<string>APPL</string>
<key>CFBundleShortVersionString</key>
<string>1.0</string>
<key>CFBundleVersion</key>
<string>1</string>
<key>LSRequiresIPhoneOS</key>
<true/>
<key>UIRequiresFullScreen</key>
<true/>
<key>UISupportedInterfaceOrientations</key>
<array>
<string>UIInterfaceOrientationPortrait</string>
<string>UIInterfaceOrientationLandscapeLeft</string>
<string>UIInterfaceOrientationLandscapeRight</string>
</array>
</dict>
</plist>
phone:="iPhone 7"
#phone:="iPhone 7 Plus"
udid:=$(shell xcrun simctl list|ruby -ne 'exit(print($$1).to_i) if /^ '$(phone)' \(([\-a-fA-F0-9]+)\)/')
#bin:=webkit
bin:=$(shell xmllint --xpath '//key[text()="CFBundleExecutable"]/following::string[1]/text()' Info.plist)
src:=$(bin).swift
appdir:=webkit.app/
#appid:=bellbind.example.webkit
appid:=$(shell xmllint --xpath '//key[text()="CFBundleIdentifier"]/following::string[1]/text()' Info.plist)
echo:
echo "$(phone)"
echo "$(udid)"
simapp:
xcrun -sdk iphonesimulator swiftc -target x86_64-apple-ios10.2 $(src)
mkdir -p $(appdir)
cp Info.plist $(bin) $(appdir)
phoneapp:
xcrun -sdk iphoneos swiftc -target arm64-apple-ios10.2 $(src)
mkdir -p $(appdir)
cp Info.plist $(bin) $(appdir)
open:
open -a Simulator --args -CurrentDeviceUDID $(udid)
# install and uninstall app to the booted simulator (require to open)
install:
xcrun simctl install $(udid) $(appdir)
uninstall:
xcrun simctl uninstall $(udid) $(appid)
showlog:
tail -f ~/Library/Logs/CoreSimulator/$(udid)/system.log
//[build for simulator]
// xcrun -sdk iphonesimulator swiftc -target x86_64-apple-ios10.1 webkit.swift
//[build for iphoneos]
// xcrun -sdk iphoneos swiftc -target arm64-apple-ios10.1 webkit.swift
//[app file layout - copy as them]
// - ./webkit.app/webkit
// - ./webkit.app/Info.plist
//[simulator]
// xcrun simctl list # print "Devices" as:
// # iPhone 7 (692EF6C3-3ED6-4782-AF31-01376D781311) (Shutdown)
//[boot with GUI]
// open -a Simulator \
// --args -CurrentDeviceUDID 692EF6C3-3ED6-4782-AF31-01376D781311
//[shutdown]
// xcrun simctl shutdown 692EF6C3-3ED6-4782-AF31-01376D781311
//[install app (with directory name)]
// xcrun simctl install 692EF6C3-3ED6-4782-AF31-01376D781311 ./webkit.app
//[uninstall app (with CFBundleIdentifier)]
// xcrun simctl install 692EF6C3-3ED6-4782-AF31-01376D781311 \
// bellbind.example.webkit
// [file path of the simulator log]
// ~/Library/Logs/CoreSimulator/692EF6C3-3ED6-4782-AF31-01376D781311/system.log
import UIKit
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(
_ application: UIApplication,
didFinishLaunchingWithOptions launchingOptions:
[UIApplicationLaunchOptionsKey : Any]? = nil) -> Bool {
let frame = UIScreen.main.bounds
let win = UIWindow(frame: frame)
// empty rootViewController is required
win.rootViewController = UIViewController(nibName: nil, bundle: nil)
win.backgroundColor = UIColor.white
win.makeKeyAndVisible()
let webview = UIWebView(frame: frame)
webview.scalesPageToFit = true
win.addSubview(webview)
// http url is blocked by default security
let req = URLRequest(url: URL(string: "https://google.com/")!)
webview.loadRequest(req)
// it should keep as member (avoid gc?)
window = win
return true
}
}
CommandLine.unsafeArgv.withMemoryRebound(
to: UnsafeMutablePointer<Int8>.self, capacity: Int(CommandLine.argc)) {
argv -> Void in
UIApplicationMain(
CommandLine.argc, argv, nil, NSStringFromClass(AppDelegate.self))
}
// references:
// http://onebigfunction.com/tools/2015/02/03/xcrun-execute-xcode-tasks-from-command-line/
// https://forums.developer.apple.com/thread/46405
// http://takuya-1st.hatenablog.jp/entry/2016/05/26/230613
// https://gist.github.com/bellbind/3175073
@bellbind
Copy link
Author

bellbind commented Nov 30, 2016

process on terminal:

make simapp 
# webkit.app/ made

make open 
# open iOS Simulator GUI

make install 
# after finished, "webkitexample" icon added to the second pane

# click the icon, then  the google page displayed
# return iOS screen with  macos menu bar: "Hardware"-"Home"

make uninstall
# the icon removed

# shutdown the Simulator with macos menu bar: "Simulator"-"Quit Simulator"

@bellbind
Copy link
Author

bellbind commented Nov 30, 2016

FYI: Simulator.app is at "/Applications/Xcode.app/Contents/Developer/Applications/Simulator.app/Contents/MacOS/Simulator "

Type of swift3 CommandLine.unsafeArgv would be a bug because these documentations have mismatch:

@bellbind
Copy link
Author

bellbind commented Dec 13, 2016

Updated Makefile for the Xcode8.2 environment

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment