iOS SDK for integrating with NitroPing push notification service.
For integrating with an existing iOS project in the same repository:
- Open your iOS project in Xcode
- Add Local Package:
- File → Add Package Dependencies
- Click "Add Local..." button
- Navigate to and select:
/path/to/nitroping/ios/package - Click "Add Package"
- Select Target:
- Choose
NitroPingClientlibrary - Add to your app target
- Choose
- Import in your code:
import NitroPingClient
Add this to your Package.swift:
dependencies: [
.package(path: "../ios/package") // Adjust path as needed
]git submodule add https://github.com/your-org/nitroping-ios ios/nitroping-sdkThen add local package in Xcode pointing to ios/nitroping-sdk/package
import UIKit
import NitroPingClient
@main
class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterDelegate {
var nitroPingClient: NitroPingClient?
func application(
_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
) -> Bool {
// Initialize NitroPing
nitroPingClient = NitroPingClient(
apiURL: "http://localhost:3000/api/graphql",
appId: "your-app-id-from-nitroping"
)
// Set notification delegate
UNUserNotificationCenter.current().delegate = self
// Request permissions and register with your user ID
Task {
try await nitroPingClient?.initialize(userId: "user-123") // Use your actual user ID
}
return true
}
// Handle device token
func application(
_ application: UIApplication,
didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data
) {
Task {
await nitroPingClient?.handleDeviceToken(deviceToken)
}
}
// Handle foreground notifications
func userNotificationCenter(
_ center: UNUserNotificationCenter,
willPresent notification: UNNotification,
withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void
) {
nitroPingClient?.handleNotification(notification.request.content.userInfo)
completionHandler([.banner, .badge, .sound])
}
// Handle notification tap
func userNotificationCenter(
_ center: UNUserNotificationCenter,
didReceive response: UNNotificationResponse,
withCompletionHandler completionHandler: @escaping () -> Void
) {
nitroPingClient?.handleNotification(response.notification.request.content.userInfo)
completionHandler()
}
}import SwiftUI
import NitroPingClient
@main
struct MyApp: App {
var body: some Scene {
WindowGroup {
ContentView()
.nitroPing(
appId: "your-app-id-from-nitroping",
apiURL: "http://localhost:3000/api/graphql",
userId: "user-123" // Your actual user ID - this will be used for tracking
)
}
}
}Your app ID from NitroPing dashboard:
let client = NitroPingClient(
apiURL: "https://your-nitroping-server.com/api/graphql",
appId: "your-app-id-here"
)// Use localhost for development
let client = NitroPingClient(
apiURL: "http://localhost:3000/api/graphql",
appId: "dev-app-id"
)// Use production server
let client = NitroPingClient(
apiURL: "https://api.yourserver.com/api/graphql",
appId: "prod-app-id"
)// Initialize with specific user
try await nitroPingClient?.initialize(userId: "user-123")// Update user ID (useful for login/logout)
try await nitroPingClient?.updateUserId("new-user-456")// In your notification handlers
func userNotificationCenter(
_ center: UNUserNotificationCenter,
willPresent notification: UNNotification,
withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void
) {
// Let NitroPing handle the notification
nitroPingClient?.handleNotification(notification.request.content.userInfo)
// Show notification UI
completionHandler([.banner, .badge, .sound])
}// Track notification events manually
await nitroPingClient?.trackNotificationDelivered(
notificationId: "notification-123",
deviceId: "device-456"
)
await nitroPingClient?.trackNotificationOpened(
notificationId: "notification-123",
deviceId: "device-456"
)
await nitroPingClient?.trackNotificationClicked(
notificationId: "notification-123",
deviceId: "device-456",
action: "button_1"
)In Xcode:
- Select your target
- Go to "Signing & Capabilities"
- Add "Push Notifications" capability
Add "Background processing" and "Remote notifications":
<!-- In Info.plist -->
<key>UIBackgroundModes</key>
<array>
<string>remote-notification</string>
</array>Make sure your NitroPing server has the correct APNS configuration:
- Development: Use sandbox APNS certificates
- Production: Use production APNS certificates
- Bundle ID: Must match your app's bundle identifier
#if DEBUG
// Use development server for testing
let client = NitroPingClient(
apiURL: "http://localhost:3000/api/graphql",
appId: "test-app-id"
)
#endif#if targetEnvironment(simulator)
// Simulator doesn't receive real push notifications
// but you can test device registration
print("Running in simulator - push notifications won't work")
#endif// Enable debug logging
client.debugMode = true // If you add this feature- iOS device tokens are 64-character hex strings
- Tokens change when the app is restored from backup
- Tokens are different for development vs production builds
- Users must grant notification permission
- Handle permission denied gracefully
- Re-request permission if needed
- Requires internet connection for device registration
- Gracefully handle network failures
- Implement retry logic for failed registrations
// Check notification settings
let settings = await UNUserNotificationCenter.current().notificationSettings()
if settings.authorizationStatus != .authorized {
// Guide user to enable notifications in Settings
}// Verify your app ID from NitroPing dashboard
// Make sure it matches exactly// Check network connectivity
// Verify API URL is correct
// Check server logs for detailsinit(apiURL: String, appId: String)- Initialize clientinitialize(userId: String?) async throws- Setup notificationshandleDeviceToken(_ deviceToken: Data) async- Register device tokenhandleNotification(_ userInfo: [AnyHashable: Any])- Process notificationupdateUserId(_ userId: String) async throws- Update user ID
trackNotificationDelivered(notificationId: String, deviceId: String) async- Track deliverytrackNotificationOpened(notificationId: String, deviceId: String) async- Track opentrackNotificationClicked(notificationId: String, deviceId: String, action: String?) async- Track click
NitroPingError.permissionDenied- Notification permission deniedNitroPingError.invalidURL- Invalid API URLNitroPingError.httpError(Int)- HTTP error from serverNitroPingError.graphQLError(String)- GraphQL API error
The SDK automatically includes device metadata:
// Automatically included:
{
"deviceModel": "iPhone",
"systemName": "iOS",
"systemVersion": "17.0",
"appVersion": "1.0.0",
"bundleId": "com.yourapp.bundle"
}do {
try await nitroPingClient?.initialize(userId: "user-123")
} catch NitroPingError.permissionDenied {
// Handle permission denied
showPermissionAlert()
} catch NitroPingError.httpError(let code) {
// Handle server error
print("Server error: \(code)")
} catch {
// Handle other errors
print("Unexpected error: \(error)")
}This SDK is part of the NitroPing project.