apple watch http request
Sample Http request in apple watch.
Include below key iPhone app's info.plist and watchkit extension's info.plist
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
</dict>
In your pod file, add Alamofire to both target, i.e. iPhone and watchkit extension
source 'https://github.com/CocoaPods/Specs.git'
use_frameworks!
target 'MyApp' do
platform :ios, '9.0'
pod 'Alamofire', '~> 3.4'
end
target 'MyApp WatchKit Extension' do
platform :watchos, '2.0'
pod 'Alamofire', '~> 3.4'
end
Create your Network.swift file and add 'Target Membership' to both i.e. iPhone target and watchkit extension target.
Sample Network.swift will be,
import Foundation
import Alamofire
struct NetworkService
{
func executeRequest(method: Alamofire.Method,parameters:String:AnyObject]?, URLString:URLStringConvertible, completionHandler: Response<AnyObject, NSError> -> Void)
{
Alamofire.request(method, URLString, parameters: parameters,encoding: .JSON, headers: nil) .responseJSON { response in
completionHandler(response)
}
}
}
Now somewhere in your code you can call this method as,
var sampleNWRequest:NetworkService = NetworkService()
sampleNWRequest.executeRequest(.GET, parameters: nil, URLString:"your url", completionHandler: { response in
print(response.result.value)
)
Hope this helps !!!
Is it possible for an Apple Watch to get data directly from a Web Server without having an iPhone as an intermediary?
Yes. As long as the watch has a network connection, via WiFi, cellular or through Bluetooth to its paired phone then you can use networking features, such as URLSession
to fetch web content.
Even if the watch does piggyback off the phone's network connection, it doesn't require you to send data "through" the companion app.
As of watchOS 6.0, watch apps don't need a phone companion app; they can be stand-alone.
Apple Watch complication network requests
You'll run into issues related to asynchronously fetching data from within the complication data source, mostly due to the data being received after the timeline update is complete.
Apple recommends that you fetch the data from a different part of your app, and have it available in advance of any complication update:
The job of your data source class is to provide ClockKit with any requested data as quickly as possible. The implementations of your data source methods should be minimal. Do not use your data source methods to fetch data from the network, compute values, or do anything that might delay the delivery of that data. If you need to fetch or compute the data for your complication, do it in your iOS app or in other parts of your WatchKit extension, and cache the data in a place where your complication data source can access it. The only thing your data source methods should do is take the cached data and put it into the format that ClockKit requires.
Other ways to approach it:
The best way to update your complication (from your phone once you have received updated weather data) is to use
transferCurrentComplicationUserInfo
.Alternately, you could have your watch app or glance cache its most recent weather details to be on hand for the next scheduled update.
If you absolutely must handle it from the complication:
You could have the scheduled timeline update get the extension to start an NSURLSession background task to asynchronously download the information from your weather service. The first (scheduled) update will then end, with no new data. Once the new weather data is received, you could then perform a second (manual) update to reload the complication timeline using the just-received data.
I don't have any personal experience with that approach, mostly because of the unnecessary need for back-to-back timeline updates.
Swift HTTP request works on the simulator but not in a real device
Try using NSURLSession to get data...
//declare data task
var task: URLSessionDataTask?
//setup the session
let url = URL(string:"https://url.here")!
let session = URLSession(configuration: URLSessionConfiguration.default)
task = session.dataTask(with: url){ (data, res, error) -> Void in
if let e = error {
print("dataTaskWithURL fail: \(e.localizedDescription)")
return
}
if let d = data {
//do something
}
}
task!.resume()
Related Topics
How to Remove Top Space of 'Form' in Swiftui
Swift Packages and Conflicting Dependencies
Libicuuc.So.55: Cannot Open Shared Object File
Detect When Wkwebview Is Finished Loading
How to Implement Default Associated Values with Swift Enums
Xcode Takes Long Time to Print Debug Results
Swiftui Sheet Not Animating Dismissal on MACos Big Sur
Uirefreshcontrol() in iOS 11 Glitchy Effect
Why Does Using Dynamictype on a Force Unwrapped Nil Optional Value Type Work
Countforfetchrequest in Swift 2.0
How to Cast a Metaclass Object to a Protocol Type in Swift
Building for Arm64E on Apple Silicon
Swift Notification Fire from Datepicker
Content Blocker Extension with a String Instead of a File
Swift:How to Handle a Lot of Textures in Memory
Create Generic Delegate for Class