苹果手表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文件,并将目标成员身份"添加到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屋!