Reputation: 2639
-(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.
Upvotes: 0
Views: 1096
Reputation: 44769
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:
Upvotes: 1