NSScanner 可在 WiFi 上工作,但不能在 3G 上工作
我已经设置了一个 NSScanner,可以通过点击 UIButton 来工作,代码如下:
-(IBAction)doLoadTTData
{
NSString *Period1String = [NSString stringWithContentsOfFile:[[NSBundle mainBundle] pathForResource: @"Period1String" ofType: @"txt"] usedEncoding:nil error:nil];
NSString *Period2String = [NSString stringWithContentsOfFile:[[NSBundle mainBundle] pathForResource: @"Period2String" ofType: @"txt"] usedEncoding:nil error:nil];
NSString *Period3String = [NSString stringWithContentsOfFile:[[NSBundle mainBundle] pathForResource: @"Period3String" ofType: @"txt"] usedEncoding:nil error:nil];
NSString *Period4String = [NSString stringWithContentsOfFile:[[NSBundle mainBundle] pathForResource: @"Period4String" ofType: @"txt"] usedEncoding:nil error:nil];
NSString *Period5String = [NSString stringWithContentsOfFile:[[NSBundle mainBundle] pathForResource: @"Period5String" ofType: @"txt"] usedEncoding:nil error:nil];
NSURL *currentURL = [NSURL URLWithString:webView.request.URL.absoluteString];
NSLog(@"Loaded Timetable");
NSError *loaderror;
NSString *page = [NSString stringWithContentsOfURL:currentURL
encoding:NSASCIIStringEncoding
error:&loaderror];
[webView loadHTMLString:page baseURL:currentURL];
NSString *Period1Data = nil;
NSScanner *htmlScanner = [NSScanner scannerWithString:page];
[htmlScanner scanUpToString:Period1String intoString:NULL];
[htmlScanner scanString:Period1String intoString:NULL];
[htmlScanner scanUpToString:@"</FONT>" intoString:&Period1Data];
NSLog(@"Collected Period 1 Data: %@", Period1Data);
if(Period1Data == NULL)
{
Period1Data = @"Not Available";
[period1label setText:@"Not Available"];
}
else if(Period1Data == @"(null)")
{
Period1Data = @"Not Available";
[period1label setText:@"Not Available"];
}
else
{
[period1label setText:Period1Data];
}
NSString *Period2Data = nil;
[htmlScanner scanUpToString:Period2String intoString:NULL];
[htmlScanner scanString:Period2String intoString:NULL];
[htmlScanner scanUpToString:@"</FONT>" intoString:&Period2Data];
if(Period2Data == NULL)
{
Period2Data = @"Not Available";
[period2label setText:@"Not Available"];
}
else if(Period2Data == @"(null)")
{
Period2Data = @"Not Available";
[period2label setText:@"Not Available"];
}
else
{
[period2label setText:Period2Data];
[period2label setText:@"Not Available"];
}
NSLog(@"Collected Period 2 Data: %@", Period2Data);
NSString *Period3Data = nil;
[htmlScanner scanUpToString:Period3String intoString:NULL];
[htmlScanner scanString:Period3String intoString:NULL];
[htmlScanner scanUpToString:@"</FONT>" intoString:&Period3Data];
if(Period3Data == NULL)
{
Period3Data = @"Not Available";
[period3label setText:@"Not Available"];
}
else if(Period3Data == @"(null)")
{
Period3Data = @"Not Available";
[period3label setText:@"Not Available"];
}
else
{
[period3label setText:Period3Data];
}
NSLog(@"Collected Period 3 Data: %@", Period3Data);
NSString *Period4Data = nil;
[htmlScanner scanUpToString:Period4String intoString:NULL];
[htmlScanner scanString:Period4String intoString:NULL];
[htmlScanner scanUpToString:@"</FONT>" intoString:&Period4Data];
if(Period4Data == NULL)
{
Period4Data = @"Not Available";
[period4label setText:@"Not Available"];
}
else if(Period4Data == @"(null)")
{
Period4Data = @"Not Available";
[period4label setText:@"Not Available"];
}
else
{
[period4label setText:Period4Data];
}
NSLog(@"Collected Period 4 Data: %@", Period4Data);
NSString *Period5Data = nil;
[htmlScanner scanUpToString:Period5String intoString:NULL];
[htmlScanner scanString:Period5String intoString:NULL];
[htmlScanner scanUpToString:@"</FONT>" intoString:&Period5Data];
if(Period5Data == NULL)
{
Period5Data = @"Not Available";
[period5label setText:@"Not Available"];
}
else if(Period5Data == @"(null)")
{
Period5Data = @"Not Available";
[period5label setText:@"Not Available"];
}
else
{
[period5label setText:Period5Data];
}
NSLog(@"Collected Period 5 Data: %@", Period5Data);
NSString* TimetableDate = nil;
NSString *DateString = [NSString stringWithFormat:@"<FONT size=2> <STRONG><FONT size=3>"];
[htmlScanner scanUpToString:DateString intoString:NULL];
[htmlScanner scanString:DateString intoString:NULL];
[htmlScanner scanUpToString:@"</FONT>" intoString:&TimetableDate];
NSString *timetablefulldate = [NSString stringWithFormat:@"Timetable for %@", TimetableDate];
[timetabledate setText:timetablefulldate];
[TimetableLoading setHidden:YES];
[TimetableLoadingBG setHidden:YES];
if(Period1Data == NULL && Period2Data == NULL && Period3Data == NULL && Period4Data == NULL && Period5Data == NULL)
{
if(ttbacking.tag == 10)
{
NSLog(@"Already showing message");
}
else
{
UIAlertView *message = [[UIAlertView alloc] initWithTitle:@"Timetable"
message:@"Looks like you don't have any lessons today! Maybe you'd like to get some homework out of the way?"
delegate:nil
cancelButtonTitle:@"I'll have a think"
otherButtonTitles:nil];
[message show];
[ttbacking setTag:10];
}
[period1label setText:@"Free Time"];
[period2label setText:@"Free Time"];
[period3label setText:@"Free Time"];
[period4label setText:@"Free Time"];
[period5label setText:@"Free Time"];
period1backing.image = [UIImage imageNamed:@"redperiodbacking.png"];
period2backing.image = [UIImage imageNamed:@"redperiodbacking.png"];
period3backing.image = [UIImage imageNamed:@"redperiodbacking.png"];
period4backing.image = [UIImage imageNamed:@"redperiodbacking.png"];
period5backing.image = [UIImage imageNamed:@"redperiodbacking.png"];
}
if(TimetableDate == NULL)
{
[timetabledate setText:@"No timetable today"];
}
[prevDay setHidden:NO];
[nextDay setHidden:NO];
NSLog(@"Student Timetable collected and published");
}
不幸的是,这只适用于 WiFi,不适用于 3G。
如果我在 3G 连接上按下按钮,所有标签都会显示“不可用”,因为结果为“NULL”。这很奇怪,因为我一次将 UIWebView 放在所有内容的前面来测试页面是否实际加载,并且确实加载了,就像在 WiFi 上一样。由于某些原因,NSScanner 无法在 3G 上运行。
为什么会这样呢?只要告诉我您是否需要查看其他代码...
I have set up an NSScanner to work on the tap of a UIButton with the following code:
-(IBAction)doLoadTTData
{
NSString *Period1String = [NSString stringWithContentsOfFile:[[NSBundle mainBundle] pathForResource: @"Period1String" ofType: @"txt"] usedEncoding:nil error:nil];
NSString *Period2String = [NSString stringWithContentsOfFile:[[NSBundle mainBundle] pathForResource: @"Period2String" ofType: @"txt"] usedEncoding:nil error:nil];
NSString *Period3String = [NSString stringWithContentsOfFile:[[NSBundle mainBundle] pathForResource: @"Period3String" ofType: @"txt"] usedEncoding:nil error:nil];
NSString *Period4String = [NSString stringWithContentsOfFile:[[NSBundle mainBundle] pathForResource: @"Period4String" ofType: @"txt"] usedEncoding:nil error:nil];
NSString *Period5String = [NSString stringWithContentsOfFile:[[NSBundle mainBundle] pathForResource: @"Period5String" ofType: @"txt"] usedEncoding:nil error:nil];
NSURL *currentURL = [NSURL URLWithString:webView.request.URL.absoluteString];
NSLog(@"Loaded Timetable");
NSError *loaderror;
NSString *page = [NSString stringWithContentsOfURL:currentURL
encoding:NSASCIIStringEncoding
error:&loaderror];
[webView loadHTMLString:page baseURL:currentURL];
NSString *Period1Data = nil;
NSScanner *htmlScanner = [NSScanner scannerWithString:page];
[htmlScanner scanUpToString:Period1String intoString:NULL];
[htmlScanner scanString:Period1String intoString:NULL];
[htmlScanner scanUpToString:@"</FONT>" intoString:&Period1Data];
NSLog(@"Collected Period 1 Data: %@", Period1Data);
if(Period1Data == NULL)
{
Period1Data = @"Not Available";
[period1label setText:@"Not Available"];
}
else if(Period1Data == @"(null)")
{
Period1Data = @"Not Available";
[period1label setText:@"Not Available"];
}
else
{
[period1label setText:Period1Data];
}
NSString *Period2Data = nil;
[htmlScanner scanUpToString:Period2String intoString:NULL];
[htmlScanner scanString:Period2String intoString:NULL];
[htmlScanner scanUpToString:@"</FONT>" intoString:&Period2Data];
if(Period2Data == NULL)
{
Period2Data = @"Not Available";
[period2label setText:@"Not Available"];
}
else if(Period2Data == @"(null)")
{
Period2Data = @"Not Available";
[period2label setText:@"Not Available"];
}
else
{
[period2label setText:Period2Data];
[period2label setText:@"Not Available"];
}
NSLog(@"Collected Period 2 Data: %@", Period2Data);
NSString *Period3Data = nil;
[htmlScanner scanUpToString:Period3String intoString:NULL];
[htmlScanner scanString:Period3String intoString:NULL];
[htmlScanner scanUpToString:@"</FONT>" intoString:&Period3Data];
if(Period3Data == NULL)
{
Period3Data = @"Not Available";
[period3label setText:@"Not Available"];
}
else if(Period3Data == @"(null)")
{
Period3Data = @"Not Available";
[period3label setText:@"Not Available"];
}
else
{
[period3label setText:Period3Data];
}
NSLog(@"Collected Period 3 Data: %@", Period3Data);
NSString *Period4Data = nil;
[htmlScanner scanUpToString:Period4String intoString:NULL];
[htmlScanner scanString:Period4String intoString:NULL];
[htmlScanner scanUpToString:@"</FONT>" intoString:&Period4Data];
if(Period4Data == NULL)
{
Period4Data = @"Not Available";
[period4label setText:@"Not Available"];
}
else if(Period4Data == @"(null)")
{
Period4Data = @"Not Available";
[period4label setText:@"Not Available"];
}
else
{
[period4label setText:Period4Data];
}
NSLog(@"Collected Period 4 Data: %@", Period4Data);
NSString *Period5Data = nil;
[htmlScanner scanUpToString:Period5String intoString:NULL];
[htmlScanner scanString:Period5String intoString:NULL];
[htmlScanner scanUpToString:@"</FONT>" intoString:&Period5Data];
if(Period5Data == NULL)
{
Period5Data = @"Not Available";
[period5label setText:@"Not Available"];
}
else if(Period5Data == @"(null)")
{
Period5Data = @"Not Available";
[period5label setText:@"Not Available"];
}
else
{
[period5label setText:Period5Data];
}
NSLog(@"Collected Period 5 Data: %@", Period5Data);
NSString* TimetableDate = nil;
NSString *DateString = [NSString stringWithFormat:@"<FONT size=2> <STRONG><FONT size=3>"];
[htmlScanner scanUpToString:DateString intoString:NULL];
[htmlScanner scanString:DateString intoString:NULL];
[htmlScanner scanUpToString:@"</FONT>" intoString:&TimetableDate];
NSString *timetablefulldate = [NSString stringWithFormat:@"Timetable for %@", TimetableDate];
[timetabledate setText:timetablefulldate];
[TimetableLoading setHidden:YES];
[TimetableLoadingBG setHidden:YES];
if(Period1Data == NULL && Period2Data == NULL && Period3Data == NULL && Period4Data == NULL && Period5Data == NULL)
{
if(ttbacking.tag == 10)
{
NSLog(@"Already showing message");
}
else
{
UIAlertView *message = [[UIAlertView alloc] initWithTitle:@"Timetable"
message:@"Looks like you don't have any lessons today! Maybe you'd like to get some homework out of the way?"
delegate:nil
cancelButtonTitle:@"I'll have a think"
otherButtonTitles:nil];
[message show];
[ttbacking setTag:10];
}
[period1label setText:@"Free Time"];
[period2label setText:@"Free Time"];
[period3label setText:@"Free Time"];
[period4label setText:@"Free Time"];
[period5label setText:@"Free Time"];
period1backing.image = [UIImage imageNamed:@"redperiodbacking.png"];
period2backing.image = [UIImage imageNamed:@"redperiodbacking.png"];
period3backing.image = [UIImage imageNamed:@"redperiodbacking.png"];
period4backing.image = [UIImage imageNamed:@"redperiodbacking.png"];
period5backing.image = [UIImage imageNamed:@"redperiodbacking.png"];
}
if(TimetableDate == NULL)
{
[timetabledate setText:@"No timetable today"];
}
[prevDay setHidden:NO];
[nextDay setHidden:NO];
NSLog(@"Student Timetable collected and published");
}
Unfortunately, this only works with WiFi and not 3G.
If I press the button on a 3G connection, all the labels show "Not Available" because the results are "NULL". This is weird because I put the UIWebView in front of everything one time to test if the page actually loaded and it did, just as if it were on WiFi. For some reason the NSScanner isn't working on 3G.
Why could this be? Just tell me if there is any other code you need to see...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我有几件事要说。首先,你用扫描仪之类的东西所做的事情确实很hacky。如果您尝试解析 HTML,则应该以正确的方式进行。使用诸如 libxml2(iPhone 内置)之类的东西来实际解析 html 并从中获取内容。老实说,您的系统可能会因小更新或页面故障而崩溃。
咆哮已经够多了,但是如果您决定保留当前系统,您可以尝试从当前加载的页面中检索实际的 html。
例如(正如我已经在您的其他问题中发布的那样):
由于您的网络视图可以加载,那么您应该能够像这样从加载的网络视图中获取内容。
如果我可以补充一下,这是行不通的:
else if(Period1Data == @"(null)")
。调试器打印(null)
,但如果字符串为 null,则它永远不会显示为 (null)。如果你要比较字符串,你会做isEqualToString:
,但我正在逃避自己。 ;)I have a couple things to say. First of all, what you're doing with scanners and stuff is really quite hacky. If you're trying to parse HTML, you should be doing it the right way. Use something like
libxml2
(which comes built into the iPhone), to actually parse the html and get stuff from it. Quite honestly your system could break with a small update or malfunction with the page.Enough with the ranting, but if you decide to leave your current system in place, you could try retrieving the actual html from the currently-loaded page.
For example (as I already posted in your other question):
Since your web view can load, then you should be able to grab the contents from the loaded webview like so.
And if I may add, this is not going to work:
else if(Period1Data == @"(null)")
. The debugger prints(null)
, but if a string is null it's never going to appear as (null). And if you were comparing strings, you would be doingisEqualToString:
, but I'm running away with myself. ;)