我正在向UIWebView发送请求.加载的网页上有AJAX调用.我需要分析AJAX流量,以便在用户登录时确定.为此,我在AppDelegate中安装了一个NSURLCache:
MYURLCache *cache = [[MYURLCache alloc] init];
[NSURLCache setSharedURLCache:cache];
Run Code Online (Sandbox Code Playgroud)
此MYURLCache类正确接收通过webview运行的流量.我只能终止AJAX调用.然后我产生了一个自己的AJAX调用请求.这个返回来自网络服务器的完美回复.所以现在要做的最后一步是构建NSCachedURLResponse返回对象.我也管理过这个,但是webview在返回对象时什么都不做.如果我只返回nil,则WebView会加载所有内容(nil是NSURLCache的消息,没有任何内容被缓存,因此webview应该开始自行加载).
- (NSCachedURLResponse *)cachedResponseForRequest:(NSURLRequest *)request {
if ([[[request URL] absoluteString] rangeOfString:@"/ajax/"].location == NSNotFound) {
return nil;
} else {
ASIHTTPRequest *asirequest = [ASIHTTPRequest requestWithURL:[request URL]];
[asirequest setValidatesSecureCertificate:NO];
[asirequest startSynchronous];
NSError *error = [asirequest error];
NSData* data = [[asirequest responseString] dataUsingEncoding:NSUTF8StringEncoding];
// Create the cacheable response
NSURLResponse *urlresponse = [[NSURLResponse alloc] initWithURL:[request URL] MIMEType:@"application/json" expectedContentLength:[data length] textEncodingName:@"UTF-8"];
NSCachedURLResponse *cachedResponse = [[NSCachedURLResponse alloc] initWithResponse:urlresponse data:data];
NSLog(@"cachedResponse %@", cachedResponse);
NSLog(@"cachedResponse data %@", [[NSString alloc] …Run Code Online (Sandbox Code Playgroud) 可能重复:
为什么要将全局Lua函数设为本地?
在社区的许多lua脚本中,我看到类似于向脚本添加模块的内容:
local module = require("module")
Run Code Online (Sandbox Code Playgroud)
但根据lua-manual,这不是加载模块的方法.模块本身内部有模块名称,所以
require("module")
Run Code Online (Sandbox Code Playgroud)
就像这样使用模块就足够了:module.myfunction().使用local-definition的第一个示例只是将"true"写入module-var - 表示模块已成功加载.
这个问题是,我看到这种加载"本地模块=需要("模块")"在网络的任何地方.来自lua-community的大多数脚本由于此错误而无法正常工作.我也想知道为什么我还没有在网上发现任何相关问题.
接下来的事情是,实际加载模块有时会导致奇怪的错误.例如:我有一个脚本
require("purexml.lua")
Run Code Online (Sandbox Code Playgroud)
比我收到这个错误:
no field package.preload['purexml.lua']
no file './purexml/lua.lua'
no file '/usr/local/share/lua/5.1/purexml/lua.lua'
no file '/usr/local/share/lua/5.1/purexml/lua/init.lua'
no file '/usr/local/lib/lua/5.1/purexml/lua.lua'
no file '/usr/local/lib/lua/5.1/purexml/lua/init.lua'
no file './purexml/lua.so'
no file '/usr/local/lib/lua/5.1/purexml/lua.so'
no file '/usr/local/lib/lua/5.1/loadall.so'
no file './purexml.so'
no file '/usr/local/lib/lua/5.1/purexml.so'
no file '/usr/local/lib/lua/5.1/loadall.so'
Run Code Online (Sandbox Code Playgroud)
但这样做很好:
require("purexml")
Run Code Online (Sandbox Code Playgroud)
我错过了一些非常明显的东西吗?我顺便使用Lua 5.1.4 ......
当我在iPhone上使用NSXMLParser解析XML时,我有一个奇怪的问题.启动应用程序时,我想预先加载4个表视图,这些视图在后台由RSS-Feeds填充.
当我逐个启动表视图时,而不是加载,解析和显示所有的工作,如魅力.但是当我尝试一次(同时)初始化所有视图时,看起来XML-parser-instances互相干扰.不知何故,来自一个XML-Feed的数据被"广播"到其他不属于的xml-parser实例中.示例:有一个"团队成员"项目,"这是我的名字".当这个错误发生时,添加了另一个xml-feed的字符串,即导致:"这是我的名字58",其中58是来自另一个视图的图表位置.在另一个例子中,"58"似乎错过了.
在我看来,由于NSXMLParser委托方法,这个错误发生了:
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string {
if (!currentStringValue) {
currentStringValue = [[NSMutableString alloc] initWithCapacity:50];
}
[currentStringValue appendString:string];
}
Run Code Online (Sandbox Code Playgroud)
在这种情况下,"通过巧合"字节被附加到不属于它们的字符串.
奇怪的是,NSXMLParser的每个实例都是唯一的,有自己独特的委托,附加到自己的ViewController.每个解析请求产生它自己的后台任务,它有自己的(也是唯一命名的)自动释放池.
我在ViewController中调用NSXMLParser:
// prepare XML saving and parsing
currentStringValue = [[[NSMutableString alloc] initWithCapacity:50] retain];
charts = [[NSMutableArray alloc] init];
NSURL *url = [[NSURL alloc] initWithString:@"http://(SOME XML URL)"];
xmlParser = [[[NSXMLParser alloc] initWithContentsOfURL:url] retain];
//Set delegate
[xmlParser setDelegate:self];
//loading indicator
progressWheel = [[[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(150.0,170.0,20.0,20.0)] autorelease];
progressWheel.activityIndicatorViewStyle = UIActivityIndicatorViewStyleGray;
[self.view addSubview:progressWheel];
[progressWheel startAnimating];
// start loading and …Run Code Online (Sandbox Code Playgroud) background ×1
ios ×1
iphone ×1
lua ×1
module ×1
nsurlcache ×1
nsxmlparser ×1
objective-c ×1
require ×1
uiwebview ×1