views:

153

answers:

1

Hey!

I'm working on an app that would display a remote html and use local images, so what I'm trying to do is download the HTML of the website and display it as a "local" page, that would have access to images in my bundle.

For some reason I can't get initWithContentsOfURL to work. I checked all manuals and examples I could find and it seems that I'm doing it correctly, but the thing just won't work, returns null all the time. The same page loaded with NSURLRequest requestWithURL works fine. Appreciate your help!

Here is the code:

- (void)awakeFromNib 
{
    appURL = @"http://dragontest.fantasy-fan.org";
    notConnectedHTML = @"Could not connect.";

    NSString *seedString = [[NSString alloc] initWithContentsOfURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@/seed.php", appURL]]];

    NSString *HTMLdata = @"";

    if (seedString = @"(null)") {
        NSLog(@"Can't connect on awakeFromNib.");
        HTMLdata = notConnectedHTML;
    }else {
        HTMLdata = [NSString stringWithFormat:@"<body style='padding:0px;margin:0px;'>%@%@</body>", seedString, @"<br><img src='images/Default.png'>"];
    }   
    [homeView loadHTMLString:HTMLdata baseURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] resourcePath]]];
}
+4  A: 

Firstly, why aren't appURL and notConnectedHTML declared as NSString *? Are they declared this way elsewhere?

Secondly, you might be better off using NSURL's -urlWithString:relativeToURL: to create the actual request URL.

Thirdly (and this is your actual problem here I suspect), to compare two C primitives, you use ==. = is the assignment operator (it makes the thing on the left equal to the thing on the right). To compare two Objective-C objects, use a comparison method, like -isEqual: or -isEqualToString: (which is specifically for NSStrings).

So instead of:

if (seedString = @"(null)")

You should use

if ([seedString isEqualToString:@"(null)"])

However I suspect the reason you're trying to compare to "(null)" is because that's what NSLog spits out when an object is equal to nil. When an object is nil, the object reference itself is equal to the nil constant, so you should use this to see if an object is nil:

if (seedString == nil)

Just for good measure, some people like to use this syntax which does exactly the same thing:

if (!seedString)
Nick Forge
wow, I could have looked for 100 years and I wouldn't have looked there for a solution. thanks Nick! :) such a rookie mistake I can't believe it hehe I don't understand though what you mean by using `urlWithString:relativeToURL:` , how is that better/different from what I'm using?and yes, those two vars in the beginning are global.
AragornSG
NSString does not qualify as a primitive, I'm afraid, but you are right that when comparing two strings == is not the best way. NSString does provide its own method for that:if ([seedString isEqualToString:@"(null)"])
Elise van Looij
No problems!If you store `appURL` as an `NSURL` instead of an `NSString`, you could do `[NSURL urlWithString:@"/seed.php" relativeToURL:appURL]`. Usually in Cocoa/CocoaTouch programming, you store URLs as `NSURL` objects rather than `NSString`s. The way I have just written is more readable, but will do exactly the same as what you have already.
Nick Forge
@Elise Just to clarify - `-[NSString isEqual:]` and `-[NSString isEqualToString:]` both do the same thing when comparing two `NSString` s, but have subtle differences. You're right that I should have used `-isEqualToString:`, since as the docs say, it is faster than `-isEqual:`. The other significant difference is that `-isEqualToString:` treats accents, umlauts et. al. differently: see the `NSString` docs [here](http://developer.apple.com/mac/library/documentation/cocoa/reference/foundation/Classes/NSString_Class/Reference/NSString.html#//apple_ref/occ/instm/NSString/isEqualToString:).
Nick Forge