我有一个带有项目列表的UITableView.选择一个项目会推送一个viewController,然后继续执行以下操作.从方法viewDidLoad我为我的子视图所需的数据启动了一个URLRequest - 一个覆盖了drawRect的UIView子类.当数据从云端到达时,我开始构建我的视图层次结构.有问题的子类传递数据,它的drawRect方法现在具有渲染所需的一切.
但.
因为我没有显式调用drawRect - Cocoa-Touch处理 - 我无法通知Cocoa-Touch我真的非常希望这个UIView子类呈现.什么时候?现在会好的!
我试过[myView setNeedsDisplay].有时这种方式有效.非常参差不齐.
我已经和它搏斗了好几个小时.有人请求我提供坚如磐石,保证强制UIView重新渲染的方法.
以下是将数据提供给视图的代码片段:
// Create the subview
self.chromosomeBlockView = [[[ChromosomeBlockView alloc] initWithFrame:frame] autorelease];
// Set some properties
self.chromosomeBlockView.sequenceString = self.sequenceString;
self.chromosomeBlockView.nucleotideBases = self.nucleotideLettersDictionary;
// Insert the view in the view hierarchy
[self.containerView addSubview:self.chromosomeBlockView];
[self.containerView bringSubviewToFront:self.chromosomeBlockView];
// A vain attempt to convince Cocoa-Touch that this view is worthy of being displayed ;-)
[self.chromosomeBlockView setNeedsDisplay];
Run Code Online (Sandbox Code Playgroud)
干杯,道格
我有一个Flex文件上传脚本,它使用URLRequest将文件上传到服务器.我想添加对http身份验证(服务器上受密码保护的目录)的支持,但我不知道如何实现这一点 - 我假设我需要以某种方式扩展类,但是如何让我有点迷失.
我试图修改以下内容(用URLRequest替换HTTPService),但这不起作用.
private function authAndSend(service:HTTPService):void{
var encoder:Base64Encoder = new Base64Encoder();
encoder.encode("someusername:somepassword");
service.headers = {Authorization:"Basic " + encoder.toString()};
service.send();
}
Run Code Online (Sandbox Code Playgroud)
我应该指出,在ActionScript/Flex方面我并不知情,尽管我已经设法成功地修改了上传脚本.
[编辑] - 根据下面的答案,这里是我的进度的更新,虽然我仍然无法让这个工作:
谢谢您的帮助.我试图实现你的代码,但我没有运气.
我在处理HTTP身份验证位置时遇到的一般行为是,IE7一切都很好,但在Firefox中,当我尝试将文件上传到服务器时,它会显示一个HTTP身份验证提示 - 即使给出了正确的详细信息,也只是停顿上传过程.
我相信IE7正常的原因在于浏览器和Flash组件共享的会话/身份验证信息 - 但是,在Firefox中并非如此,我遇到了上述行为.
这是我更新的上传功能,包含您的更改:
private function pergress():void
{
if (fileCollection.length == 0)
{
var urlString:String = "upload_process.php?folder="+folderId+"&type="+uploadType+"&feid="+formElementId+"&filetotal="+fileTotal;
if (ExternalInterface.available)
{
ExternalInterface.call("uploadComplete", urlString);
}
}
if (fileCollection.length > 0)
{
fileTotal++;
var urlRequest:URLRequest = new URLRequest("upload_file.php?folder="+folderId+"&type="+uploadType+"&feid="+formElementId+"&obfuscate="+obfuscateHash+"&sessidpass="+sessionPass);
urlRequest.method = URLRequestMethod.POST;
urlRequest.data = new URLVariables("name=Bryn+Jones");
var encoder:Base64Encoder = new Base64Encoder();
encoder.encode("testuser:testpass");
var credsHeader:URLRequestHeader = …
Run Code Online (Sandbox Code Playgroud) apache-flex base64 actionscript urlrequest http-authentication
我正在尝试使用本机URL和URLRequest类从iOS客户端检索OAuth令牌以使用Yelp的Fusion API,但它在"tokenInfo"变量中给出了这个错误:
client_id or client_secret parameters not found. Make sure to provide
client_id and client_secret in the body with the
application/x-www-form-urlencoded content-type
Run Code Online (Sandbox Code Playgroud)
这是我的代码:
func getToken(){
var yelpTokenEndpoint = "https://api.yelp.com/oauth2/token"
var tokenURL = URL(string: yelpTokenEndpoint)
let requestJSON: [String:String] = ["client_id":"Not showing actual client id", "client_secret":"Not Gonna Show My Actual Client Secret either","grant_type":"client_credentials"]
let requestData = try? JSONSerialization.data(withJSONObject: requestJSON)
print(try? JSONSerialization.jsonObject(with: requestData!, options: []))
var tokenURLRequest = URLRequest(url: tokenURL!)
tokenURLRequest.httpMethod = "POST"
tokenURLRequest.httpBody = requestData!
tokenURLRequest.setValue("application/x-www-form-urlencoded", forHTTPHeaderField: "content-type")
let tokenSession = URLSession.shared …
Run Code Online (Sandbox Code Playgroud) 我有一个简单的类使用URLSession发送HTTP POST请求(到Neo4j服务器,但这没关系),以及测试它成功的集成测试.
通过Xcode,通过iOS模拟器,我可以运行这个测试一百万次 - 每次都成功.
但是,当我swift test
在Ubuntu Linux上运行时(我使用IBM提供的docker镜像),我经常会遇到测试失败,说我收到了401响应.
通过容器中的cURL复制它根本不会失败,所以我不认为这是我的容器的问题.
我添加了一个数据包转储(我通过Charles Proxy检查),其中15个测试运行,7个测试运行(因此7个请求)失败.所有失败的请求都抱怨没有提供身份验证标头.从转储中,这是正确的,转储不包含那些失败的请求的身份验证标头.但为什么不呢?实际上,所有标头标志都是不同的:成功运行具有以下标头:
POST /db/data/cypher HTTP/1.1
Host: 192.168.0.18:7474
Accept-Encoding: deflate, gzip
Authorization: Basic bmVvNGo6c3RhY2swdmVyRmxvdw==
Content-Type: application/json; charset=utf-8
Accept: application/json; charset=utf-8
Connection: keep-alive
User-Agent: urlsessionTestPackageTests.xctest (unknown version) curl/7.35.0
Content-Length: 135
Run Code Online (Sandbox Code Playgroud)
而一个不成功的人有这些:
POST /db/data/cypher HTTP/1.1
Host: 192.168.0.18:7474
Accept: */*
Accept-Encoding: deflate, gzip
Connection: keep-alive
User-Agent: urlsessionTestPackageTests.xctest (unknown version) curl/7.35.0
Content-Length: 135
Run Code Online (Sandbox Code Playgroud)
所有200个结果都具有相同的标题,并且所有401结果具有相同的标题.你能在我的代码中看到任何应该保证这样随机请求的内容吗?
我对这篇文章的问题非常相似,但我不完全理解答案.我已经创建了一个完成处理程序,但它似乎没有按预期工作.
func updateTeam(teamID: Int) {
startConnection {NSArray, Int in
//Do things with NSArray
}
}
func startConnection(completion: (NSArray, Int) -> Void) {
let url = URL(string: "http://www.example.com/path")
var request : URLRequest = URLRequest(url: url!)
request.httpMethod = "POST"
let postString = "a=\(Int(teamInput.text!)!)"
request.httpBody = postString.data(using: .utf8)
let dataTask = URLSession.shared.dataTask(with: request) {
data,response,error in
print("anything")
do {
if let jsonResult = try JSONSerialization.jsonObject(with: data!, options: []) as? NSDictionary {
self.teamResult = jsonResult
print(jsonResult)
}
} catch let error as …
Run Code Online (Sandbox Code Playgroud) 我想要完成的是使用URLLoader类和URLRequest将一些二进制数据,特别是表示PNG图像的ByteArray上传到服务器.
当我将contentType
URLRequest 的属性设置为'multipart/form-data'而不是默认值时,调用会urlLoader.load()
导致安全异常.
当我将该contentType
属性保留为默认属性时,它可以正常工作,但需要很长时间(与PNG文件的长度成比例)才能将文件上载到服务器.
所以,我的问题是为什么我得到这个安全例外?我怎么能避免它呢?
请注意,我的SWF是从开发服务器提供的,而不是本地文件系统(准确地说是Google App Engine开发服务器).
这是代码:
var pngFile:ByteArray = PNGEncoder.encode(bitmapData);
var urlRequest:URLRequest = new URLRequest('/API/uploadImage');
// With this line of code, the call to urlLoader.load() throws the following security exception:
// 'SecurityError: Error #2176: Certain actions, such as those that display a pop-up window, may only be invoked upon user interaction, for example by a mouse click or button press.'
urlRequest.contentType = 'multipart/form-data';
urlRequest.method = URLRequestMethod.POST;
urlRequest.data = pngFile;
urlRequest.requestHeaders.push(new URLRequestHeader('Cache-Control', …
Run Code Online (Sandbox Code Playgroud) securityexception file-upload urlrequest urlloader actionscript-3
我将尽可能具体和冗长,并包括我正在使用的一些代码.我已经进行了搜索,发现了这个问题,看似相似; 但是作者使用的是ActionScript 2而不是3,而我似乎无法有效地应用任何给出我自己情况的答案.
我试图通过Flash/ActionScript 3模拟(以有限的方式)JavaScript的XMLHttpRequest对象的行为,以克服相同的域限制.但我发现ActionScript在这方面有其自身的局限性.我承认我可能会弄错,但据我所知,理论上仍然可以使用ActionScript进行这种跨域脚本编写,只要你获得所有权限.那就是我遇到麻烦的地方.
首先,我为一个名为AjaxRequest的类借用了一些开源代码,我将其保存为/ajax/AjaxRequest.as
.然后我创建了一个名为/jsajax.fla
导出到最终SWF文件的Flash文件/jsajax.swf
.现在,这是包含Flash文件的第一个也是唯一一个框架的ActionScript代码:
import ajax.AjaxRequest;
Security.allowDomain("domainone.com");
Security.allowDomain("domaintwo.com");
function jsAjax(stringURL:String, stringMethod:String, stringData:String):void
{
var xhr:AjaxRequest = new AjaxRequest(stringURL);
xhr.contentType = "application/x-www-form-urlencoded";
xhr.dataFormat = URLLoaderDataFormat.TEXT;
if ( stringMethod.toUpperCase() == "POST" ) {
xhr.method = URLRequestMethod.POST;
} else {
xhr.method = URLRequestMethod.GET;
}
xhr.addEventListener("complete", jsAjaxResponse);
xhr.send(stringData);
return;
}
function jsAjaxResponse(evt:Event):void
{
ExternalInterface.call("jsAjaxResponse", evt.currentTarget.data.toString());
return;
}
ExternalInterface.addCallback("jsAjax", jsAjax);
ExternalInterface.call("jsAjaxReady");
Run Code Online (Sandbox Code Playgroud)
到现在为止还挺好.我觉得Security.allowDomain
不需要这些调用中的一个或多个,但他们试图解决这个问题是我的(不成功)尝试.
在我的JavaScript,我已经得到了定义了三个功能:jsAjax
,jsAjaxResponse
,和jsAjaxReady
.最后一个是一个非常基本的函数,用于指示Flash对象成功加载(仅在加载时立即调用一次),而另外两个用于发送和接收数据.如您所见,它们具有相应的ActionScript对应物.
最后,我创建了一个简单的HTML页面/test.html …
我目前在尝试将数据发布到Web服务器时遇到有关URLSession的一些问题。但是,这很完美。似乎无效的是我设置的超时时间。这对于我的整个应用程序至关重要,因为我不希望用户永远被“加载”,而不会出现任何类型的错误消息。这是我的代码:
var request = URLRequest(url: URL(string: "https://www.mywebsite.com/file.php")!, cachePolicy: .reloadIgnoringLocalAndRemoteCacheData, timeoutInterval: 20)
let urlconfig = URLSessionConfiguration.default
urlconfig.timeoutIntervalForRequest = 20
urlconfig.timeoutIntervalForResource = 20
request.httpMethod = "POST"
let session = URLSession(configuration: urlconfig, delegate: self, delegateQueue: nil)//URLSession.shared
let body = "receiver=\(receiverID)"
request.httpBody = body.data(using: String.Encoding.utf8, allowLossyConversion: true)
request.timeoutInterval = 20
session.dataTask(with: request) {data, response, err in
if err == nil {
do {
let jsonResult:NSDictionary? = try JSONSerialization.jsonObject(with: data!, options: JSONSerialization.ReadingOptions.mutableContainers) as? NSDictionary
let jsonComp = jsonResult?.value(forKey: "completion") as! String
if jsonComp == "done" …
Run Code Online (Sandbox Code Playgroud) 我想知道你们在应用程序中使用 URLRequest 时如何处理错误。您如何通知用户发生了错误?您甚至通知过您的用户吗?您是否尝试再次重新加载 URLRequest?您是否告诉用户关闭当前屏幕并再次打开并显示警告框?我没有任何线索。
一旦出现错误,您的应用程序就会停止。那么,当发生这种情况并且您遇到网络问题、Json 数据错误时该怎么办?
当您收到“网络连接不良(服务器已关闭)”或 URLSession 返回错误但互联网连接正常时,您该怎么办?
请查看下面的代码并帮助我弄清楚发生错误时需要做什么。
let url = URL(string:"http://example/jsonFile.php")
var request = URLRequest(url:url!)
request.httpMethod = "POST"
let postingString = "id=\(id)"
request.httpBody = postingString.data(using: String.Encoding.utf8)
let task = URLSession.shared.dataTask(with: request as URLRequest){(data, response, error) -> Void in
if error != nil {
print("error \(error)")
// *****
// What do you do here? Do you tell your users anything?
// *****
return
}
// Check for Error
if let urlContent = data {
do{
let jsonResult = …
Run Code Online (Sandbox Code Playgroud) 我有一个fla(使用ActionScript 3.0)我在Flash中编译.我正在使用URLRequest和URLLoader来访问http web服务.
var loader:URLLoader = new URLLoader();
var request:URLRequest = new URLRequest("http:test.webservice.com");
try {
loader.load(request);
} catch (error:Error) {
trace("Unable to load requested document.");
}
Run Code Online (Sandbox Code Playgroud)
这很好 - 但是如果我尝试访问我得到的https地址
httpStatusHandler: [HTTPStatusEvent type="httpStatus" bubbles=false cancelable=false eventPhase=2 status=0]
ioErrorHandler: [IOErrorEvent type="ioError" bubbles=false cancelable=false eventPhase=2 text="Error #2032: Stream Error. URL: https://test.webservice.com"]
Run Code Online (Sandbox Code Playgroud)
如何从https Web服务检索数据?SWF是否必须托管在SSL安全页面上?
urlrequest ×10
swift ×4
ios ×3
flash ×2
nsurlsession ×2
swift3 ×2
urlloader ×2
actionscript ×1
apache-flex ×1
base64 ×1
cocoa-touch ×1
cross-domain ×1
file-upload ×1
https ×1
oauth ×1
server-side ×1
ssl ×1
uiview ×1
urlsession ×1
yelp ×1