NSScanner致力于WiFi而非3G

时间:2011-12-14 16:49:12

标签: iphone objective-c ios xcode nsscanner

我已经设置了一个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>&nbsp;<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。

为什么会这样?告诉我你是否还需要其他代码......

1 个答案:

答案 0 :(得分:2)

我有几件事要说。首先,你使用扫描仪和东西做的事情真是太烂了。如果您正在尝试解析HTML,那么您应该以正确的方式进行。使用类似libxml2(内置于iPhone中)的内容,实际上解析 html并从中获取内容。老实说,您的系统可能会因页面的小更新或故障而中断。

足够咆哮,但如果您决定离开现有系统,可以尝试从当前加载的页面中检索实际的html。

例如(正如我已在您的其他问题中发布的那样):

NSString *path = @"document.body.innerHTML";
NSString *html = [webView stringByEvaluatingJavaScriptFromString:path];

由于您的Web视图可以加载,因此您应该能够从加载的Web视图中获取内容。

如果我可以添加,这不会起作用:else if(Period1Data == @"(null)")。调试器打印(null),但如果字符串为null,则永远不会显示为(null)。如果你在比较字符串,你会做isEqualToString:,但我正在逃避自己。 ;)