Skip to content

Commit 6cfb193

Browse files
authored
Add an iOS Demo with Signin With Apple (#37)
* initial commit of ios demo * rename ios project and update its dependencies * add public API to provide image as Data * simplify parameter management with STSWebIdentityAWSCredentialIdentityResolver * do not delete the token file as the identity provider needs it at API call time * update iOS code to use the BEdrockService instead of the AWS SDK * license header * ignore license in entitlement file
1 parent e3ab72d commit 6cfb193

File tree

23 files changed

+1664
-16
lines changed

23 files changed

+1664
-16
lines changed

.licenseignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,5 @@ Package.resolved
3737
**/frontend/*
3838
**/*.code-snippets
3939
.DS_Store
40-
.licenseignore
40+
.licenseignore
41+
**/*.entitlements
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
.DS_Store
2+
/.build
3+
/Packages
4+
xcuserdata/
5+
DerivedData/
6+
.swiftpm/configuration/registries.json
7+
.swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata
8+
.netrc

Examples/ios-math-solver/README.md

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# Math Problem Solver
2+
3+
An iOS application that uses AWS Bedrock and Claude to solve math and physics problems from images.
4+
5+
## Features
6+
7+
- Take a photo or select an image from your photo library
8+
- Analyze math and physics problems using Claude AI
9+
- Get step-by-step solutions with explanations
10+
- Secure authentication using Sign in with Apple (SIWA) and AWS STS
11+
12+
## Screenshot
13+
14+
![Math Problem Solver App Screenshot](screenshot.png)
15+
16+
## Technology Stack
17+
18+
- Swift bedrock Library for AI image analysis and problem solving
19+
- Swift and SwiftUI for the iOS app
20+
- Claude 3 Sonnet model for high-quality responses
21+
- Sign in with Apple (SIWA) for authentication
22+
- Streaming responses for real-time feedback
23+
24+
## How It Works
25+
26+
1. The app captures or selects an image containing a math or physics problem
27+
2. The image is processed and sent to AWS Bedrock
28+
3. Claude analyzes the problem and generates a step-by-step solution
29+
4. The solution is streamed back to the app in real-time
30+
5. The app displays the formatted solution with mathematical notation
31+
32+
## Requirements
33+
34+
- iOS 16.0+
35+
- Xcode 15.0+
36+
- AWS account with Bedrock configured
37+
- Sign in with Apple enabled in your Apple Developer account
38+
39+
## License
40+
41+
This project is licensed under the Apache License 2.0 - see the LICENSE file for details.
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"colors" : [
3+
{
4+
"idiom" : "universal"
5+
}
6+
],
7+
"info" : {
8+
"author" : "xcode",
9+
"version" : 1
10+
}
11+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
{
2+
"images" : [
3+
{
4+
"idiom" : "universal",
5+
"platform" : "ios",
6+
"size" : "1024x1024"
7+
},
8+
{
9+
"appearances" : [
10+
{
11+
"appearance" : "luminosity",
12+
"value" : "dark"
13+
}
14+
],
15+
"idiom" : "universal",
16+
"platform" : "ios",
17+
"size" : "1024x1024"
18+
},
19+
{
20+
"appearances" : [
21+
{
22+
"appearance" : "luminosity",
23+
"value" : "tinted"
24+
}
25+
],
26+
"idiom" : "universal",
27+
"platform" : "ios",
28+
"size" : "1024x1024"
29+
}
30+
],
31+
"info" : {
32+
"author" : "xcode",
33+
"version" : 1
34+
}
35+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"info" : {
3+
"author" : "xcode",
4+
"version" : 1
5+
}
6+
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the Swift Bedrock Library open source project
4+
//
5+
// Copyright (c) 2025 Amazon.com, Inc. or its affiliates
6+
// and the Swift Bedrock Library project authors
7+
// Licensed under Apache License v2.0
8+
//
9+
// See LICENSE.txt for license information
10+
// See CONTRIBUTORS.txt for the list of Swift Bedrock Library project authors
11+
//
12+
// SPDX-License-Identifier: Apache-2.0
13+
//
14+
//===----------------------------------------------------------------------===//
15+
16+
import AuthenticationServices
17+
import Foundation
18+
import SwiftUI
19+
20+
class AuthenticationManager: ObservableObject {
21+
@Published var isAuthenticated = false // FIXME: check if the token is still valid
22+
@Published var userId: String? = nil
23+
@Published var error: String? = nil
24+
25+
var jwtToken: String? = nil
26+
27+
// Trigger for credential changes
28+
@Published var credentialsUpdated = false
29+
30+
func signOut() {
31+
isAuthenticated = false
32+
userId = nil
33+
}
34+
35+
func authenticate(with userIdentifier: String, identityToken: Data) {
36+
userId = userIdentifier
37+
38+
// Convert identity token to string
39+
guard let tokenString = String(data: identityToken, encoding: .utf8) else {
40+
handleAuthError(
41+
NSError(
42+
domain: "AuthenticationError",
43+
code: 1001,
44+
userInfo: [NSLocalizedDescriptionKey: "Failed to convert identity token to string"]
45+
)
46+
)
47+
return
48+
}
49+
50+
self.jwtToken = tokenString
51+
52+
// Get AWS credentials using the Apple identity token
53+
Task {
54+
// Update UI on main thread
55+
await MainActor.run {
56+
self.isAuthenticated = true
57+
self.error = nil
58+
}
59+
}
60+
}
61+
62+
func handleAuthError(_ error: Error) {
63+
self.error = error.localizedDescription
64+
isAuthenticated = false
65+
}
66+
}
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the Swift Bedrock Library open source project
4+
//
5+
// Copyright (c) 2025 Amazon.com, Inc. or its affiliates
6+
// and the Swift Bedrock Library project authors
7+
// Licensed under Apache License v2.0
8+
//
9+
// See LICENSE.txt for license information
10+
// See CONTRIBUTORS.txt for the list of Swift Bedrock Library project authors
11+
//
12+
// SPDX-License-Identifier: Apache-2.0
13+
//
14+
//===----------------------------------------------------------------------===//
15+
16+
let content = """
17+
### Solution
18+
19+
#### Concepts Involved
20+
This problem involves the Doppler effect, which describes the change in frequency of a wave in relation to an observer who is moving relative to the wave source. In this case, the source is a rotating sound emitter, and the observer is a microphone.
21+
22+
#### Given Data
23+
- Frequency of the sound source, \\( f_0 = 1500 \\, \\text{Hz} \\)
24+
- Radius of the circular path, \\( r = 40.0 \\, \\text{cm} = 0.4 \\, \\text{m} \\)
25+
- The sound source is rotating in an anti-clockwise direction.
26+
27+
#### Steps to Solve the Problem
28+
29+
1. **Determine the angular velocity (\\( \\omega \\)) of the source:**
30+
31+
Since the source is rotating, we need to find its angular velocity. However, the problem does not provide the rotational speed directly. We will assume the tangential velocity \\( v \\) at the point of the source is given by \\( v = r \\omega \\).
32+
33+
2. **Calculate the Doppler shift for each position (a, b, c):**
34+
35+
The Doppler effect formula for sound is given by:
36+
37+
\\(f = f_0 \\left( \\frac{v + v_o}{v + v_s} \\right)\\)
38+
39+
where:
40+
- \\( f \\) is the observed frequency.
41+
- \\( f_0 \\) is the source frequency.
42+
- \\( v \\) is the speed of sound in the medium.
43+
- \\( v_o \\) is the observer's velocity relative to the medium.
44+
- \\( v_s \\) is the source's velocity relative to the medium.
45+
46+
Since the observer (microphone) is stationary, \\( v_o = 0 \\).
47+
48+
3. **Frequency at point a:**
49+
50+
At point a, the source is moving towards the observer.
51+
\\[f_a = f_0 \\left( \\frac{v}{v - v_s} \\right)\\]
52+
where \\( v_s = r \\omega \\).
53+
54+
4. **Frequency at point b:**
55+
56+
At point b, the source is moving perpendicular to the line of sight of the observer.
57+
\\[f_b = f_0\\]
58+
There is no Doppler shift because the component of the velocity towards the observer is zero.
59+
60+
5. **Frequency at point c:**
61+
62+
At point c, the source is moving away from the observer.
63+
\\[f_c = f_0 \\left( \\frac{v}{v + v_s} \\right)\\]
64+
where \\( v_s = r \\omega \\).
65+
66+
6. **Compare the frequencies:**
67+
68+
From the Doppler effect formulas:
69+
- \\( f_a > f_0 \\) because the source is moving towards the observer.
70+
- \\( f_b = f_0 \\) because there is no relative motion towards or away from the observer.
71+
- \\( f_c < f_0 \\) because the source is moving away from the observer.
72+
73+
Therefore, the relationship between the frequencies is:
74+
\\(f_a > f_b = f_c\\)
75+
76+
### Conclusion
77+
The frequencies perceived by the microphone are such that \\( f_a > f_b = f_c \\).
78+
"""

0 commit comments

Comments
 (0)