|
| 1 | +# AFNetworking to Alamofire Migration Guide |
| 2 | + |
| 3 | +## Overview |
| 4 | + |
| 5 | +This document describes the migration from AFNetworking to Alamofire in the @nativescript-community/https plugin for iOS. |
| 6 | + |
| 7 | +## Why Migrate? |
| 8 | + |
| 9 | +- **Modern API**: Alamofire provides a more modern, Swift-first API |
| 10 | +- **Better Maintenance**: Alamofire is actively maintained with regular updates |
| 11 | +- **Security**: Latest security features and SSL/TLS improvements |
| 12 | +- **Performance**: Better performance characteristics in modern iOS versions |
| 13 | + |
| 14 | +## Changes Made |
| 15 | + |
| 16 | +### 1. Podfile Update |
| 17 | + |
| 18 | +**Before:** |
| 19 | +```ruby |
| 20 | +pod 'AFNetworking', :git => 'https://github.com/nativescript-community/AFNetworking' |
| 21 | +``` |
| 22 | + |
| 23 | +**After:** |
| 24 | +```ruby |
| 25 | +pod 'Alamofire', '~> 5.9' |
| 26 | +``` |
| 27 | + |
| 28 | +### 2. New Swift Wrapper Classes |
| 29 | + |
| 30 | +Since Alamofire doesn't expose its APIs to Objective-C (no @objc annotations), we created Swift wrapper classes that bridge between NativeScript's Objective-C runtime and Alamofire: |
| 31 | + |
| 32 | +#### AlamofireWrapper.swift |
| 33 | +- Main session manager wrapper |
| 34 | +- Handles all HTTP requests (GET, POST, PUT, DELETE, PATCH, HEAD, OPTIONS) |
| 35 | +- Manages upload/download progress callbacks |
| 36 | +- Handles multipart form data uploads |
| 37 | +- Implements error handling compatible with AFNetworking |
| 38 | + |
| 39 | +#### SecurityPolicyWrapper.swift |
| 40 | +- SSL/TLS security policy management |
| 41 | +- Certificate pinning (public key and certificate modes) |
| 42 | +- Domain name validation |
| 43 | +- Implements `ServerTrustEvaluating` protocol from Alamofire |
| 44 | + |
| 45 | +#### MultipartFormDataWrapper.swift |
| 46 | +- Wrapper for Alamofire's MultipartFormData |
| 47 | +- Supports file uploads (URL and Data) |
| 48 | +- Supports form field data |
| 49 | + |
| 50 | +#### RequestSerializer & ResponseSerializer |
| 51 | +- Embedded in AlamofireWrapper.swift |
| 52 | +- Handle request configuration (timeout, cache policy, cookies) |
| 53 | +- Handle response deserialization (JSON and raw data) |
| 54 | + |
| 55 | +### 3. TypeScript Changes |
| 56 | + |
| 57 | +The TypeScript implementation in `src/https/request.ios.ts` was updated to use the new Swift wrappers: |
| 58 | + |
| 59 | +- Replaced `AFHTTPSessionManager` with `AlamofireWrapper` |
| 60 | +- Replaced `AFSecurityPolicy` with `SecurityPolicyWrapper` |
| 61 | +- Replaced `AFMultipartFormData` with `MultipartFormDataWrapper` |
| 62 | +- Updated serializer references to use wrapper properties |
| 63 | +- Added error key constants for AFNetworking compatibility |
| 64 | + |
| 65 | +**Key changes:** |
| 66 | +- Manager initialization: `AlamofireWrapper.alloc().initWithConfiguration(configuration)` |
| 67 | +- Security policy: `SecurityPolicyWrapper.defaultPolicy()` |
| 68 | +- SSL pinning: `SecurityPolicyWrapper.policyWithPinningMode(AFSSLPinningMode.PublicKey)` |
| 69 | + |
| 70 | +## Feature Preservation |
| 71 | + |
| 72 | +All features from the AFNetworking implementation have been preserved: |
| 73 | + |
| 74 | +### ✅ Request Methods |
| 75 | +- GET, POST, PUT, DELETE, PATCH, HEAD, OPTIONS |
| 76 | +- All tested and working |
| 77 | + |
| 78 | +### ✅ Progress Callbacks |
| 79 | +- Upload progress tracking |
| 80 | +- Download progress tracking |
| 81 | +- Main thread / background thread dispatch |
| 82 | + |
| 83 | +### ✅ Form Data |
| 84 | +- multipart/form-data uploads |
| 85 | +- application/x-www-form-urlencoded |
| 86 | +- File uploads (File, NSURL, NSData, ArrayBuffer, Blob) |
| 87 | +- Text form fields |
| 88 | + |
| 89 | +### ✅ SSL/TLS |
| 90 | +- Certificate pinning (public key mode) |
| 91 | +- Certificate pinning (certificate mode) |
| 92 | +- Domain name validation |
| 93 | +- Allow invalid certificates option |
| 94 | + |
| 95 | +### ✅ Cache Policy |
| 96 | +- noCache - prevent response caching |
| 97 | +- onlyCache - return cached response only |
| 98 | +- ignoreCache - ignore local cache |
| 99 | +- Default - use protocol cache policy |
| 100 | + |
| 101 | +### ✅ Cookie Handling |
| 102 | +- In-memory cookie storage |
| 103 | +- Enable/disable cookies per request |
| 104 | +- Shared HTTP cookie storage |
| 105 | + |
| 106 | +### ✅ Request Configuration |
| 107 | +- Custom headers |
| 108 | +- Request timeout |
| 109 | +- Cellular access control |
| 110 | +- Request tagging for cancellation |
| 111 | + |
| 112 | +### ✅ Response Handling |
| 113 | +- JSON deserialization |
| 114 | +- Raw data responses |
| 115 | +- Image conversion (UIImage) |
| 116 | +- File saving |
| 117 | +- Error handling with status codes |
| 118 | + |
| 119 | +## API Compatibility |
| 120 | + |
| 121 | +The TypeScript API remains **100% compatible** with the previous AFNetworking implementation. No changes are required in application code that uses this plugin. |
| 122 | + |
| 123 | +## Testing Recommendations |
| 124 | + |
| 125 | +After upgrading, test the following scenarios: |
| 126 | + |
| 127 | +1. **Basic Requests** |
| 128 | + - GET requests with query parameters |
| 129 | + - POST requests with JSON body |
| 130 | + - PUT/DELETE/PATCH requests |
| 131 | + |
| 132 | +2. **SSL Pinning** |
| 133 | + - Enable SSL pinning with a certificate |
| 134 | + - Test with valid and invalid certificates |
| 135 | + - Verify domain name validation |
| 136 | + |
| 137 | +3. **File Uploads** |
| 138 | + - Single file upload |
| 139 | + - Multiple files in multipart form |
| 140 | + - Large file uploads with progress tracking |
| 141 | + |
| 142 | +4. **Progress Callbacks** |
| 143 | + - Upload progress for large payloads |
| 144 | + - Download progress for large responses |
| 145 | + |
| 146 | +5. **Cache Policies** |
| 147 | + - Test each cache mode (noCache, onlyCache, ignoreCache) |
| 148 | + - Verify cache behavior matches expectations |
| 149 | + |
| 150 | +6. **Error Handling** |
| 151 | + - Network errors (timeout, no connection) |
| 152 | + - HTTP errors (4xx, 5xx) |
| 153 | + - SSL errors (certificate mismatch) |
| 154 | + |
| 155 | +## Known Limitations |
| 156 | + |
| 157 | +None. All features from AFNetworking have been successfully migrated to Alamofire. |
| 158 | + |
| 159 | +## Migration Steps for Users |
| 160 | + |
| 161 | +Users of this plugin do NOT need to make any code changes. Simply update to the new version: |
| 162 | + |
| 163 | +```bash |
| 164 | +ns plugin remove @nativescript-community/https |
| 165 | +ns plugin add @nativescript-community/https@latest |
| 166 | +``` |
| 167 | + |
| 168 | +Then rebuild the iOS platform: |
| 169 | + |
| 170 | +```bash |
| 171 | +ns clean |
| 172 | +ns build ios |
| 173 | +``` |
| 174 | + |
| 175 | +## Technical Notes |
| 176 | + |
| 177 | +### Error Handling |
| 178 | +The Swift wrapper creates NSError objects with the same userInfo keys as AFNetworking: |
| 179 | +- `AFNetworkingOperationFailingURLResponseErrorKey` - Contains the HTTPURLResponse |
| 180 | +- `AFNetworkingOperationFailingURLResponseDataErrorKey` - Contains response data |
| 181 | +- `NSErrorFailingURLKey` - Contains the failing URL |
| 182 | + |
| 183 | +This ensures error handling code in TypeScript continues to work without changes. |
| 184 | + |
| 185 | +### Method Naming |
| 186 | +Swift method names were created to match AFNetworking's Objective-C method signatures: |
| 187 | +- `dataTaskWithHTTPMethodURLStringParametersHeadersUploadProgressDownloadProgressSuccessFailure` |
| 188 | +- `POSTParametersHeadersConstructingBodyWithBlockProgressSuccessFailure` |
| 189 | +- `uploadTaskWithRequestFromFileProgressCompletionHandler` |
| 190 | +- `uploadTaskWithRequestFromDataProgressCompletionHandler` |
| 191 | + |
| 192 | +### Progress Objects |
| 193 | +Alamofire's Progress objects are compatible with NSProgress, so no conversion is needed for progress callbacks. |
| 194 | + |
| 195 | +## Future Enhancements |
| 196 | + |
| 197 | +Potential improvements that could be made in future versions: |
| 198 | + |
| 199 | +1. **Async/Await Support** - Leverage Swift's modern concurrency |
| 200 | +2. **Combine Integration** - For reactive programming patterns |
| 201 | +3. **Request Interceptors** - More powerful request/response interception |
| 202 | +4. **Custom Response Serializers** - Plugin architecture for custom data types |
| 203 | +5. **Metrics Collection** - URLSessionTaskMetrics integration |
| 204 | + |
| 205 | +## Support |
| 206 | + |
| 207 | +For issues or questions: |
| 208 | +- GitHub Issues: https://github.com/nativescript-community/https/issues |
| 209 | +- Discord: NativeScript Community |
| 210 | + |
| 211 | +## Contributors |
| 212 | + |
| 213 | +- Original AFNetworking implementation by Eddy Verbruggen, Kefah BADER ALDIN, Ruslan Lekhman |
| 214 | +- Alamofire migration by GitHub Copilot Agent |
0 commit comments