xtool.git
Publicgit clone https://git.maproj.org/xtool.gitxtool.git / Documentation / xtool.docc / Appex.md
| 1 | # Create App Extensions |
| 2 | |
| 3 | Add app extensions to your iOS app. |
| 4 | |
| 5 | ## Overview |
| 6 | |
| 7 | iOS includes several [App Extension Points](https://developer.apple.com/documentation/technologyoverviews/app-extensions) to deeply integrate with system functionality: such as Widgets, Share Extensions, and Safari Extensions. With a little setup, you can create App Extensions with xtool. |
| 8 | |
| 9 | This guide will show you how to add a **Widget Extension** to your app. The steps are similar for other extensions as well. |
| 10 | |
| 11 | > Note: This guide assumes you already have an xtool-based application up and running. If you don't, create one first with <doc:First-app>. |
| 12 | |
| 13 | ## Step 1: Add a new product |
| 14 | |
| 15 | Start by adding a new product declaration to your `Package.swift`. |
| 16 | |
| 17 | ```diff |
| 18 | // swift-tools-version: 6.0 |
| 19 | |
| 20 | import PackageDescription |
| 21 | |
| 22 | let package = Package( |
| 23 | name: "Hello", |
| 24 | platforms: [.iOS(.v17)], |
| 25 | products: [ |
| 26 | .library( |
| 27 | name: "Hello", |
| 28 | targets: ["Hello"] |
| 29 | ), |
| 30 | + .library( |
| 31 | + name: "HelloWidget", |
| 32 | + targets: ["HelloWidget"] |
| 33 | + ), |
| 34 | ], |
| 35 | targets: [ |
| 36 | .target(name: "Hello"), |
| 37 | + .target(name: "HelloWidget"), |
| 38 | ] |
| 39 | ) |
| 40 | ``` |
| 41 | |
| 42 | ## Step 2: Update xtool.yml |
| 43 | |
| 44 | Now that we have two products, we'll need to tell xtool which one corresponds to the _application_ and which one corresponds to the _extension_. |
| 45 | |
| 46 | You should already have a bare-bones `xtool.yml` file that describes metadata about your package (the bundle ID, at the bare minimum.) We'll add the new information to this file. |
| 47 | |
| 48 | ```diff |
| 49 | version: 1 |
| 50 | bundleID: com.example.Hello |
| 51 | + product: Hello |
| 52 | + extensions: |
| 53 | + - product: HelloWidget |
| 54 | + infoPath: HelloWidget-Info.plist |
| 55 | ``` |
| 56 | |
| 57 | ## Step 3: Add an Info.plist |
| 58 | |
| 59 | App extensions require an `Info.plist` file that tells the system what kind of extension they are, amongst other things. In the previous step, we promised xtool that this file will be located at `./HelloWidget-Info.plist` in your project. We'll now create this file. |
| 60 | |
| 61 | ```xml |
| 62 | <?xml version="1.0" encoding="UTF-8"?> |
| 63 | <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> |
| 64 | <plist version="1.0"> |
| 65 | <dict> |
| 66 | <key>NSExtension</key> |
| 67 | <dict> |
| 68 | <key>NSExtensionPointIdentifier</key> |
| 69 | <string>com.apple.widgetkit-extension</string> |
| 70 | </dict> |
| 71 | </dict> |
| 72 | </plist> |
| 73 | ``` |
| 74 | |
| 75 | > Note: |
| 76 | > |
| 77 | > The value `com.apple.widgetkit-extension` tells the system that this is a Widget Extension. If you're creating a different type of Extension, your `Info.plist` might look different. |
| 78 | > |
| 79 | > For a list of possible `NSExtensionPointIdentifier` values, see [Apple's documentation](https://developer.apple.com/documentation/bundleresources/information-property-list/nsextension/nsextensionpointidentifier) on the subject. You may also need other keys like `NSExtensionPrincipalClass`: refer to Apple's ~better~ legacy [documentation library](https://developer.apple.com/library/archive/documentation/General/Reference/InfoPlistKeyReference/Articles/AppExtensionKeys.html) for details. |
| 80 | > |
| 81 | > Finally, note that the above refers to the old paradigm of **Foundation Extensions**. Apple has started moving towards a newer framework called **ExtensionKit** for recent extension types. The main difference for extension consumers is that you replace `NSExtension -> EXAppExtensionAttributes`, and `NSExtensionPointIdentifier -> EXExtensionPointIdentifier`. xtool doesn't support ExtensionKit yet, but it's [planned](https://github.com/xtool-org/xtool/issues/138). |
| 82 | |
| 83 | ## Step 4: Code your widget |
| 84 | |
| 85 | We can finally write the code for the widget. Create a new file at `Sources/HelloWidget/Widget.swift` with these contents: |
| 86 | |
| 87 | ```swift |
| 88 | import WidgetKit |
| 89 | import SwiftUI |
| 90 | |
| 91 | @main struct Bundle: WidgetBundle { |
| 92 | var body: some Widget { |
| 93 | HelloWidget() |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | struct HelloWidget: Widget { |
| 98 | var body: some WidgetConfiguration { |
| 99 | StaticConfiguration( |
| 100 | kind: "HelloWidget", |
| 101 | provider: Provider() |
| 102 | ) { entry in |
| 103 | VStack { |
| 104 | Text(entry.date, style: .date) |
| 105 | } |
| 106 | .containerBackground(.fill.tertiary, for: .widget) |
| 107 | } |
| 108 | .configurationDisplayName("HelloWidget") |
| 109 | .description("This is an example widget.") |
| 110 | } |
| 111 | |
| 112 | struct Entry: TimelineEntry { |
| 113 | var date = Date() |
| 114 | } |
| 115 | |
| 116 | struct Provider: TimelineProvider { |
| 117 | func placeholder(in context: Context) -> Entry { |
| 118 | Entry() |
| 119 | } |
| 120 | |
| 121 | func getSnapshot( |
| 122 | in context: Context, |
| 123 | completion: @escaping (Entry) -> Void |
| 124 | ) { |
| 125 | completion(Entry()) |
| 126 | } |
| 127 | |
| 128 | func getTimeline( |
| 129 | in context: Context, |
| 130 | completion: @escaping (Timeline<Entry>) -> Void |
| 131 | ) { |
| 132 | completion(Timeline( |
| 133 | entries: [Entry()], |
| 134 | policy: .after(.now + 3600) |
| 135 | )) |
| 136 | } |
| 137 | } |
| 138 | } |
| 139 | ``` |
| 140 | |
| 141 | > Note: Blindly refreshing once an hour isn't a great strategy in practice but it makes for a short snippet. I never said this was a tutorial on writing _good_ WidgetKit code; there's plenty of other resources online if that's your goal, including Apple's own [documentation](https://developer.apple.com/documentation/widgetkit). |
| 142 | |
| 143 | Finally, build and run with `xtool dev`. You should be able to locate the widget in your widget library and add it to your home screen! |