草庐IT

URLSession

全部标签

ios - 如何在 SwiftUI 中使用 URLSession 请求后呈现 View ?

我想在收到请求的数据后呈现一个View,像这样varbody:someView{VStack{Text("CompanyID")TextField($companyID).textFieldStyle(.roundedBorder)URLSession.shared.dataTask(with:url){(data,_,_)inguardletdata=dataelse{return}DispatchQueue.main.async{self.presentation(Modal(LogonView(),onDismiss:{print("dismiss")}))}}.resume()

ios - 如何在 Swift 中 stub URLSession?

我一直在关注这个tutorialstubURLSession。Theexample通过创建协议(protocol)并扩展现有的URLSession来完成。protocolURLSessionProtocol{typealiasDataTaskResult=(Data?,URLResponse?,Error?)->VoidfuncdataTask(withrequest:NSURLRequest,completionHandler:@escapingDataTaskResult)->URLSessionDataTaskProtocol}extensionURLSession:URLSes

ios - 如何在 URLSession 的 HTTP header 中为 'Token xxxxxxxxxx' 设置 'Authorization'

我正在使用的后端API要求针对HTTPheaderkeyAuthorization的每个请求发送token,格式为Tokenxxxxxxxxxx。现在,我正在执行以下操作。vargetRequest=URLRequest(url:url)getRequest.addValue("Tokenxxxxxxxx",forHTTPHeaderField:"Authorization")这有时有效,有时有时有效,当请求发出时,header字段Authorization被剥离。我使用Charles代理检查了这个。Apple'sdocumentation陈述如下。AnNSURLSessionobje

ios - 如何将 URLSessionStreamTask 与 URLSession 一起用于分块编码传输

我正在尝试连接到Twitter流式API端点。看起来URLSession支持通过URLSessionStreamTask进行流式传输,但我不知道如何使用该API。我也找不到任何示例代码。我尝试测试以下内容,但没有记录网络流量:letsession=URLSession(configuration:.default,delegate:self,delegateQueue:nil)letstream=session.streamTask(withHostName:"https://stream.twitter.com/1.1/statuses/sample.json",port:22)st

ios - 带有未在后台调用的请求 block 的 URLSession.datatask

URLSession数据任务block在应用程序处于后台时未调用,它停留在dataTask请求。当我打开应用程序时,block被调用。顺便说一下,我正在使用https请求。这是我的代码:letrequest=NSMutableURLRequest(url:URL(string:urlasString)!,cachePolicy:.reloadIgnoringCacheData,timeoutInterval:20)request.httpMethod=methodasStringrequest.setValue("application/x-www-form-urlencoded",f

swift - URLSession 数据任务超时错误

我目前在尝试将数据发布到我的网络服务器时遇到一些有关URLSession的问题。然而,这非常有效。似乎不起作用的是我设置的超时。这对我的整个应用程序来说相当重要,因为我不希望用户永远“加载”,而没有任何类型的错误消息。这是我的代码:varrequest=URLRequest(url:URL(string:"https://www.mywebsite.com/file.php")!,cachePolicy:.reloadIgnoringLocalAndRemoteCacheData,timeoutInterval:20)leturlconfig=URLSessionConfigurati

ios - URLSession 不适用于 URLCredential

我有一个我正在尝试连接的API,服务器是Windows身份验证。我正在尝试通过委托(delegate)方法将URLSession与URLCredential结合使用funcurlSession(_session:URLSession,didReceivechallenge:URLAuthenticationChallenge,completionHandler:@escaping(URLSession.AuthChallengeDisposition,URLCredential?)->Swift.Void){vardisposition:URLSession.AuthChallenge

ios - URLSession 不适用于 URLCredential

我有一个我正在尝试连接的API,服务器是Windows身份验证。我正在尝试通过委托(delegate)方法将URLSession与URLCredential结合使用funcurlSession(_session:URLSession,didReceivechallenge:URLAuthenticationChallenge,completionHandler:@escaping(URLSession.AuthChallengeDisposition,URLCredential?)->Swift.Void){vardisposition:URLSession.AuthChallenge

ios - 从 (_,_,_) 类型的抛出函数 throws -> Void 到非抛出函数类型 (NSData?, NSURLResponse?, NSError?) -> Void 的无效转换

我写了这段代码:funcgetjson(){leturlPath="https://api.whitehouse.gov/v1/petitions.json?limit=100"leturl=NSURL(string:urlPath)letsession=NSURLSession.sharedSession()lettask=session.dataTaskWithURL(url!,completionHandler:{data,response,error->Voidinprint("Taskcompleted")if(error!=nil){print(error!.localiz

ios - 从 (_,_,_) 类型的抛出函数 throws -> Void 到非抛出函数类型 (NSData?, NSURLResponse?, NSError?) -> Void 的无效转换

我写了这段代码:funcgetjson(){leturlPath="https://api.whitehouse.gov/v1/petitions.json?limit=100"leturl=NSURL(string:urlPath)letsession=NSURLSession.sharedSession()lettask=session.dataTaskWithURL(url!,completionHandler:{data,response,error->Voidinprint("Taskcompleted")if(error!=nil){print(error!.localiz