|
| 1 | +export const metadata = { |
| 2 | + title: 'Getting Started with iOS', |
| 3 | + description: 'build a from-scratch iOS project that includes iroh as a swift module' |
| 4 | +} |
| 5 | + |
| 6 | +# Getting Started with iOS |
| 7 | + |
| 8 | +This guide will walk you through setting up an example iOS project that includes the iroh swift module. We'll use it to join a document created on a computer from the iroh console. |
| 9 | + |
| 10 | +## Create a new Xcode project |
| 11 | + |
| 12 | +Make sure you have [xcode](https://apps.apple.com/us/app/xcode/id497799835?mt=12) installed, and have run `xcode-select --install` to install the command line tools. |
| 13 | + |
| 14 | +Next, create a new Xcode project. Select "App" under "iOS" and click "Next". |
| 15 | + |
| 16 | +## Import the iroh swift module |
| 17 | + |
| 18 | +Select `File > Add Packages...` |
| 19 | + |
| 20 | + |
| 21 | +## Create ContentView |
| 22 | + |
| 23 | +```swift |
| 24 | + |
| 25 | +import SwiftUI |
| 26 | +import IrohLib |
| 27 | + |
| 28 | +struct ContentView: View { |
| 29 | + @State var state: IrohNode |
| 30 | + @State private var ticketString: String = "" |
| 31 | + @State private var contentList: String = "No document joined" |
| 32 | + |
| 33 | + var body: some View { |
| 34 | + let peer_id = state.peerId() |
| 35 | + |
| 36 | + VStack { |
| 37 | + Text("Hello \(peer_id)") |
| 38 | + TextField("Enter Doc Ticket", text: $ticketString) |
| 39 | + Button("Join Doc") { |
| 40 | + join() |
| 41 | + } |
| 42 | + Text("Current Document: \(contentList)") |
| 43 | + } |
| 44 | + .padding() |
| 45 | + } |
| 46 | + |
| 47 | + init() throws { |
| 48 | + _state = .init(wrappedValue: try IrohNode()) |
| 49 | + let _ = print("Starting..") |
| 50 | + |
| 51 | + let peer_id = state.peerId() |
| 52 | + let _ = print("created iroh node \(state) with peer id \(peer_id)") |
| 53 | + } |
| 54 | + |
| 55 | + func join() { |
| 56 | + print("joining \(ticketString)") |
| 57 | + let docTicket = try! DocTicket.fromString(content: ticketString) |
| 58 | + let doc = try! state.importDoc(ticket: docTicket) |
| 59 | + for _ in 1...5 { |
| 60 | + DispatchQueue.main.asyncAfter(deadline: .now() + 1) { |
| 61 | + let content = try! doc.latest() |
| 62 | + print("got \(content.count) elements for \(doc.id())") |
| 63 | + |
| 64 | + let formattedList = content.map { (entry) -> String in |
| 65 | + let content: String = { |
| 66 | + do { |
| 67 | + let c = try doc.getContentBytes(entry: entry) |
| 68 | + return String(decoding: c, as: UTF8.self) |
| 69 | + } catch { |
| 70 | + return "N/A" |
| 71 | + } |
| 72 | + }() |
| 73 | + let key = String(decoding: entry.key(), as: UTF8.self) |
| 74 | + let author_s = entry.author().toString() |
| 75 | + return "Author: \(author_s) wrote '\(key)' -> '\(content)'" |
| 76 | + }.joined(separator: "\n") |
| 77 | + contentList = "\(doc.id()) (\(content.count))\n\n\(formattedList)" |
| 78 | + } |
| 79 | + } |
| 80 | + } |
| 81 | +} |
| 82 | + |
| 83 | +struct ContentView_Previews: PreviewProvider { |
| 84 | + static var previews: some View { |
| 85 | + try! ContentView() |
| 86 | + } |
| 87 | +} |
| 88 | + |
| 89 | +extension StringProtocol { |
| 90 | + var data: Data { .init(utf8) } |
| 91 | + var bytes: [UInt8] { .init(utf8) } |
| 92 | +} |
| 93 | +``` |
| 94 | + |
| 95 | +## Create a document from the console |
| 96 | + |
0 commit comments