0

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>&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");

}

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...

pixelbitlabs
  • 1,934
  • 6
  • 36
  • 65

1 Answers1

2

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):

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

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 doing isEqualToString:, but I'm running away with myself. ;)

sudo rm -rf
  • 29,408
  • 19
  • 102
  • 161
  • thanks for that! I've got this so far, what more do I need to do? http://pastie.org/3026712 – pixelbitlabs Dec 16 '11 at 15:11
  • 2
    Well that's something you're going to have to figure out! You're the developer, you know. ;) – sudo rm -rf Dec 16 '11 at 15:15
  • I know, just need a bit of guidance and then I can get on with perfecting it. I found pretty much all the code (slightly different method) here: http://stackoverflow.com/questions/1343515/how-to-save-the-content-in-uiwebview-for-faster-loading-on-next-launch but not sure which code actually saves the file - is it the code at the very bottom of the first answer? – pixelbitlabs Dec 16 '11 at 15:39
  • What are you trying to do? You're trying to actually save the html to the disk? And I just looked at your code and you're completely misunderstanding what I just posted above. The `*html` variable that we created by evaluating the javascript **is** the html content. You don't need to do `stringWithContentsOfURL:`. http://pastie.org/3026856 – sudo rm -rf Dec 16 '11 at 15:40
  • is there a way I can handle the timeout process, because if I have bad connection, the whole app crashes as it can't retrieve the data? Could you post an example please? Thanks! – pixelbitlabs Dec 19 '11 at 16:58
  • If your file is rather big, you should look into a networking library, such as `AFNetworkingKit` or `MKNetworkKit`. – sudo rm -rf Dec 19 '11 at 17:11
  • Would this be able to do exactly what NSScanner does though and look for a certain piece of text between two pieces of code and then put that into an NSString? Is there a small example of this? It's just that I need to implement it quickly as the app needs to be submitted to Apple tomorrow. Thank you for your help! – pixelbitlabs Dec 19 '11 at 17:20
  • P.S. I'm talking about the main question, not saving the file to disk :-) – pixelbitlabs Dec 19 '11 at 17:24
  • Please join me here: http://chat.stackoverflow.com/rooms/5987/nsscanner-working-on-wifi-but-not-3g – sudo rm -rf Dec 19 '11 at 17:47
  • Hi, when I go to build using the simulator these errors come up... http://file.reddexuk.com/3s1n1E3c2j050U3u1T1m why is this and what can I do to fix this? Doesn't happen when building on a device. – pixelbitlabs Dec 21 '11 at 11:27
  • Either you recently added a framework or library and you didn't read how to use it correctly (maybe you need a dylib or something), or you're adding your frameworks incorrectly. Your paths look wrong... – sudo rm -rf Dec 21 '11 at 14:48
  • OK, all fixed now - thanks! Just one last thing, is there any way to programatically change the orientation of the app? I know there are private APIs but then Apple may reject the app... I've seen Stack Overflow questions on it, but I didn't find any answers which actually did what I wanted. – pixelbitlabs Dec 21 '11 at 17:05
  • I'm not sure. You could try rotating the status bar? Like this: `[[UIApplication sharedApplication] setStatusBarOrientation:yourDesiredOrientation]`. I don't know if that works, though. – sudo rm -rf Dec 21 '11 at 17:17
  • come onto chat, need to ask a really urgent question :) - http://chat.stackoverflow.com/rooms/6359/ios-dev-talk – pixelbitlabs Jan 03 '12 at 20:50
  • do you have an email address I can contact you on? I have some work I'd like you to do (you'd probably view it as relatively easy), in exchange for some cash :) – pixelbitlabs Jan 08 '12 at 17:49