tags:

views:

330

answers:

1
-(void)loadWebAdress:(NSString*)textAdress {
    [UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
    adressurl=[NSString stringWithFormat:@"http://%@", textAdress];
    NSURL *url=[NSURL URLWithString:adressurl];
    NSURLRequest *requestObj=[NSURLRequest requestWithURL:url]; 
    [webview loadRequest:requestObj];
}

although url takes it's value from adressurl, adressurl is all the time out of scope when checked in debugger. what is going on? i would like to use it in some other places too. not only in this method. because is out of scope, the app crashes. But, I repeat, it is the one who gives its value to url.

+1  A: 

It depends on where the adressurl variable is declared. Since it is generated from the method parameter, it seems odd that you'd want to use it elsewhere in the code. If you have it as a static variable, it could be getting stomped by other code. (For example, if you set it to one value in this method, and another elsewhere, it's not unusual to get crashes, especially if you don't coordinate or synchronize between the two. One reason to avoid globals/statics.) You're free to use the same local variable name in different methods if you like.

Here's what I'd suggest doing instead: (Note: I've fixed some typos.)

- (void) loadWebAddress:(NSString*)textAddress {
    [UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
    NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"http://%@", textAddress]];
    [webview loadRequest:[NSURLRequest requestWithURL:url]];
}

This is shorter and avoids unnecessary variables. Since the "http://" prefix is fairly common, it doesn't seem like reusing that will provide that much benefit. Is there something else I'm missing?


Edit: To clarify a typo in my comment, you can get the URL as a string from the UIWebView as follows:

[[[webview request] URL] absoluteString]

This uses the following methods chained together:

Quinn Taylor
adressurl is declared as an ivar in this viewcontroller. the reason I put the url into adressurl is because my app has to bookmark when user wants to. so everytime i load an url i put it also in that variable so it can be bookmarked
Since you're using UIWebView, why not just query the `request` property, and get the URL from that if/when you need to bookmark? (e.g. `[[[webview request] URL] absoluteURL]`)
Quinn Taylor
thanks. the result of what you wrote is NSString?
it's important to me that i exctract in NSString the url because further this is put in a certain structure that goes in a database.