Declared Function
- (IBAction) changeProductWeb:(NSString *)str;
- (IBAction) changeProductWeb:(NSString *)str{
NSString *urlAddress = str;
NSURLRequest *request =[NSURLRequest requestWithURL:urlAddress];
[webView loadRequest:request];
}
Set string using Array
[cell changeProductWeb:[webTitle objectAtIndex:indexPath.row]];
The Array
webTitle = [[NSArray alloc] initWithObjects:
#"bar.html",
#"bar.html",
#"bar.html",
#"bar.html",
nil];
When I launches it chrashes, if I set the string staticaly in the:
- (IBAction) changeProductWeb:(NSString *)str{
It works fine
[NSURLRequest requestWithURL:] wants an NSURL, not an NSString. Try something like this instead:
NSURLRequest *request =[NSURLRequest requestWithURL:[NSURL URLWithString:urlAddress]];
I am not sure how it works fine setting it statically because you are passing the incorrect type to NSURLRequest. requestWithURL: requires an NSURL not an NSString.
NSURL *urlAddress = [NSURL URLWithString:str];
NSURLRequest *request =[NSURLRequest requestWithURL:urlAddress];
Related
My code must update web view and download web site. But it doesn't because URLWithString returns null. How can I solve this problem? stringByAddingPercentEscapesUsingEncoding is already deprecated.
-(void) setWebViewMain:(NSString *) link {
// link = #"http://www.rbc.ru/society/05/09/2017/59ae2dfe9a794753f05e3e06"; if this string uncomment code is working
NSURL *url = [NSURL URLWithString: link];
NSLog(#"%#",url); // here url is (null)
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[self.webView loadRequest:requestObj];
[self.webView reload];
}
try as
-(void) setWebViewMain:(NSString *) link {
// link = #"http://www.rbc.ru/society/05/09/2017/59ae2dfe9a794753f05e3e06"; if this string uncomment code is working
NSURL *url = [NSURL URLWithString: [NSString stringWithFormat:#"%#",link]];
NSLog(#"%#",url); // here url is (null)
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[self.webView loadRequest:requestObj];
// [self.webView reload];
}
Just remove the last line from your method and it will work:
-(void) setWebViewMain:(NSString *) link {
// link = #"http://www.rbc.ru/society/05/09/2017/59ae2dfe9a794753f05e3e06"; if this string uncomment code is working
NSURL *url = [NSURL URLWithString: link];
NSLog(#"%#",url); // here url is (null)
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[self.webView loadRequest:requestObj];
//[self.webView reload];
}
You can call it like this:
[self setWebViewMain:#"http://www.rbc.ru/society/05/09/2017/59ae2dfe9a794753f05e3e06"];
I'm having the same problem as:
NSURL with special characters
But tried their solution. Can't get my NSURLRequest to work with åöä characters. If the variable "string" contains åöä the request return null. Also tried with NSISOLatin1StringEncoding.
NSString *encodedString = [string stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSString *urlString = [NSString stringWithFormat: #"http://suggestqueries.google.com/complete/search?output=firefox&q=%#", encodedString];
NSURL *url = [NSURL URLWithString: urlString];
NSURLRequest *request = [[NSURLRequest alloc] initWithURL:url];
This works:
http://suggestqueries.google.com/complete/search?output=firefox&q=%C3%A5%C3%B6%C3%A4 (åöä)
Any ideas?
EDIT:
Using the debugger the NSURL looks correct:
string __NSCFString * #"åäö" 0x0a77cd60
encodedString __NSCFString * #"%C3%A5%C3%A4%C3%B6" 0x0a77fc40
url NSURL * #"http://suggestqueries.google.com/complete/search?output=firefox&q=%C3%A5%C3%A4%C3%B6" 0x0a79d1f0
Solved: The problem was not the NSURL it was how the return NSDATA was interpreted.
Instead of using
NSURL *url = [NSURL URLWithString: urlString];
NSURLRequest *request = [[NSURLRequest alloc] initWithURL:url];
try using
NSString *encoded = [urlString stringByAddingPercentEncodingWithAllowedCharacters: [NSCharacterSet URLQueryAllowedCharacterSet]];
NSURLRequest* request = [NSURLRequest requestWithURL: [NSURL URLWithString: encoded]];
Hello every one this is my simple code.. what i am missing ? it is two days i am trying to sort out the problem.. any idea?
- (void)viewDidLoad
{
[super viewDidLoad];
NSString *path = [[NSBundle mainBundle] pathForResource:#"dog" ofType:#"pdf"];
NSURL *url = [NSURL fileURLWithPath:path];
NSURL *request = [NSURLRequest requestWithURL:url];
[webView loadRequest:request]; <<<<<< HERE WHERE THE ERROR COME OUT <<<<<<
[webView setScalesPageToFit:YES];
}
loadRequest argument is of type NSURLRequest and not NSURL as in your case.
And are you sure this line is compiling ?? :
NSURL *request = [NSURLRequest requestWithURL:url];
As far as I know the method returns object of type NSURLRequest and not NSURL
I don't understand why if I load the content of a UIWebView in XCode this way:
NSString *string = #"http://www.dummyurl.org/dummy.pdf";
NSURL *url = [NSURL URLWithString:string];
[my_view loadRequest:[NSURLRequest requestWithURL:url]];
everything works fine, but if a build the original string from a php script I wrote:
NSString *strURL = [NSSTring stringWithFormat:#"www.myserver.php"];
NSString *string = [[NSString alloc] initWithContentsOfURL:[NSURL URLWithString:strURL]];
NSURL *url = [NSURL URLWithString:string];
[my_view loadRequest:[NSURLRequest requestWithURL:url]];
nothing works. I checked my script and it returns EXACTLY the original string (http://www.dummyurl.org/dummy.pdf) that works fine with the first method.
I build from a PHP script the content of a UITextView too, but that works fine.
I don't understand why this method works with the UITextView but not with the UIWebView to load the .pdf.
It may be useful
NSCharacterSet *characterSet = [NSCharacterSet whitespaceAndNewlineCharacterSet];
NSString *tempString = [string stringByTrimmingCharactersInSet:characterSet];
NSURL *url = [NSURL URLWithString:string];
[my_view loadRequest:[NSURLRequest requestWithURL:url]];
Make sure you linked the webview (IBOutlet) and the delegate.
With this lines it should load a url:
webView.delegate = self;
NSURLRequest *urlRequest;
NSString *urlToOpen = [NSString stringWithFormat:#"http://www.stackoverflow.com"];
urlRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:urlToOpen]];
[self.webView loadRequest:urlRequest];
Hope this helps...
I had this and it was a cache problem:
[webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:url] cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:60.0]];
I would like to pass a NSArray containing NSstrings to NSURLRequest, is it possible ? I'm not quite sure how to approach this, I'm getting it to work fine with just one URL but I can't seem to be able to pass an Array or urls . Any ideas ?
This is the code I'm using which is obviously wrong calling url1, I would like to call an array instead :
UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, 768, 1024)];
NSString *url1 = #"http://www.apple.com/";
NSString *url2 = #"http://www.google.com/";
urls = [[NSArray alloc] initWithObjects:url1,url2,nil];
NSURL *url = [NSURL URLWithString:url1];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[webView loadRequest:requestObj];
Thanks !
So you want to load multiple webpages, right?
urls = [[NSArray alloc] initwithObjects:url1, url2, nil];
for (NSURL *url in urls) {
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:url]];
[webView loadRequest:request];
}
However, I'm not sure why you would want to do this? This may send a request to url1, but will instantly cancel that request and start loading url2.