0

I'm currently trying to send an https request from an Apple Watch to an iPhone using Alamofire, which I understand is the only way to do this. I've tried installing using Cocoapods, but this hasn't worked and has resulted in an error each time. So, I've tried installing manually, and, while I believe that installed, it keeps giving me a "module not found". I've attached the current state of the project with the networking client and the view controller. If anyone has any advice related to the installation process or the coding in general, that would be wonderful.

import Foundation
import Alamofire

class NetworkingClient {
    
    typealias WebServiceResponse = ([[String:Any]]?, Error?) -> Void
    
    func execute( url: URL, completion: WebServiceResponse){
        

        
        Alamofire.request(url).validate().responseJSON {response in
            if let error = response.error {
                completion(nil,error)
            } else if let jsonArray = response.result.value as? [[String: Any]]{
                completion(jsonArray, nil)
            } else if let jsonDict = response.result.value as? [[String: Any]]{
                completion([jsonDict],nil)
            }
            
        }
    }
}



import UIKit

class ViewController: UIViewController {
    
    @IBOutlet weak var textView: UITextView!
    
    private let networkingClient = NetworkingClient()
    
    override func viewDidLoad() {
        super.viewDidLoad()
    }
    
    @IBAction func executeRequest( sender: Any){
        
        guard let urlToExecute = URL(string: "https://jsonplaceholder.typicode.com/posts/1") else{
            return
        }
        
        networkingClient.execute (url: urlToExecute) { (json, error) in
            if let error = error {
                self.textView.text = error.localizedDescription
            } else if let json = json {
                self.textView.text = json.description
            }
            
        }
        
    }
}
Paulw11
  • 108,386
  • 14
  • 159
  • 186
  • Why would you use AlamoFire (or any http network operation) to communicate between the watch and the phone? You would use [`WCSession`](https://developer.apple.com/documentation/watchconnectivity/wcsession). If you want to make a network request from a watch app to some other server (and indeed this may be handled via the paired phone but this is transparent to you) then you can use `URLSession` . The code you have show is ios code, so I am not sure how the watch is even relevant here. – Paulw11 Jun 29 '23 at 20:29
  • If Xcode is telling you that the module isn't found then you haven't successfully added the library to your project. If you do want to use it, I suggest you add it as a Swift package in Xcode. But personally I would just use URLSession – Paulw11 Jun 29 '23 at 20:33

0 Answers0