苹果手表http请求 [英] apple watch http request
问题描述
使用导入,我可以将这个模块用于 iPhone,但不能用于 Apple Watch 应用程序.我也想使用这个库来编写 Apple Watch 应用程序.是否可以?如果可以,怎么做?如果不可能,您能否提供替代方案?
Using import, I can use this module for iPhone, but not for apple watch app.I also want to use this library for writing apple watch app. Is it possible? If possible, how? Can you please provide an alternative if it is not possible?
先谢谢了
iPhone 的 http 请求简单示例
Simple example of http request for iPhone
import Alamofire
Alamofire.request(.GET, requestUrl, headers: self.headers(), encoding:.JSON).responseJSON
{
(rJ) -> Void in
let data = rJ.result.value
let err = rJ.result.error
}
推荐答案
Apple Watch 中的 Http 请求示例.
包括以下关键 iPhone 应用的 info.plist 和 watchkit 扩展的 info.plist
Include below key iPhone app's info.plist and watchkit extension's info.plist
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
</dict>
在您的 pod 文件中,将 Alamofire 添加到两个目标,即 iPhone 和 watchkit 扩展
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
创建您的 Network.swift 文件并将Target Membership"添加到 iPhone 目标和 watchkit 扩展目标.
Create your Network.swift file and add 'Target Membership' to both i.e. iPhone target and watchkit extension target.
示例 Network.swift 将是,
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)
)
希望这有帮助!!!
这篇关于苹果手表http请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!