I´m trying to show a pdf in a webview, but the webview is always white / blank.
if i log the path from the local file, it can be logged, so the file is there..
Here is my code:
NSString *path = [[NSBundle mainBundle] pathForResource:#"fileName" ofType:#"pdf"];
NSURL *targetURL = [NSURL fileURLWithPath:path];
NSURLRequest *request = [NSURLRequest requestWithURL:targetURL];
[webView loadRequest:request];
(webView is an iboutlet)
Maik
Please make sure the User Interaction Enabled & Multiple Touch are enabled.
Your targetURL is maybe nil, please debug this.
If the targetUrl is nil, this is maybe the 'path' has space.
So you should encode the path:
//Encode Url
+(NSString *) urlEncodeString:(NSString *) str
{
return [str stringByReplacingOccurrencesOfString:#" " withString:#"%20"];
}
Controlling the whole characters rather than not just the space is more appropriate.Try this one for encoding the path of a file.
-(NSString *)urlenc:(NSString *)val{
CFStringRef safeString =
CFURLCreateStringByAddingPercentEscapes(NULL,
(CFStringRef)val,
NULL,
CFSTR("/%&=?$#+-~#<>|*,.()[]{}^!şığüöçĞÜŞİÖÇ"),
kCFStringEncodingUTF8);
return [NSString stringWithFormat:#"%#", safeString];
}
Related
How to find out if a NSString is an website URL in Objective-c.It will be better if you provide source code. If not, tell me which class is useful is good enough.
Finally i found a way to judge,may not the best way,but useful :
NSURL *url = [NSURL URLWithString:urlStr];
NSURL *lazyUrlStr = [NSURL URLWithString:[NSString stringWithFormat:#"http://%#",urlStr]];
if([[UIApplication sharedApplication] canOpenURL:url]){
NSLog(#"it's a url");
}else if([[UIApplication sharedApplication] canOpenURL:lazyUrlStr]){
NSLog(#"it's a url");
}else{NSLog(#"it isn't a url");
}
FYI: urlStr is the NSString to be judged
You can use URLWithString method of NSURL (if you are asking for NSString, not UIString):
NSURL *url = [NSURL URLWithString:yourUrlString];
if (url && url.scheme && url.host)
{
//the url is ok
NSLog(#"URL:%#", yourUrlString);
}
Hope this will help:)
I have generated a PDF file within my application which is saved to the Documents directory. I am trying to fetch the pdf to display within a UIWebView but i have an unused NSURL variable. Could this be the reason why the file is not loading properly? Here is my code which is attached to an IBAction button
The only way to get rid of that warning is to NSLog that NSURL. But I want to display that saved file.
This is the code I am using.
-(IBAction)pdf:(id)sender
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [documentsDirectory stringByAppendingPathExtension:#"client.pdf"];
NSURL *url = [NSURL fileURLWithPath:filePath];
NSString *urlString = [url absoluteString];
NSString *encodedString=[urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL *webURL = [NSURL URLWithString:encodedString];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[self.myWebView loadRequest:request];
NSLog(#"%#", webURL);
}
I figured it out. I needed to change my filePath NSString from stringByAppendingPathExtension to stringByAppendingPathcomponent. I can now view my PDF file in my UIWebView.
How do you use a URL and be able to change part of the URL? I'm new at Objective C.
Errors at url in all spots!
restaurants is my UIWebView, zip is my UITextField, restlabel is my UILabel.
-(void)load {
url = [[NSString alloc] initWithFormat:#"http://www.example.com/search?query=%#", restlabel.text];
}
-(IBAction)gourl {
restlabel.text = [NSString stringWithFormat:#"%#", [zip text]];
[restaurants loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:url]]];
}
You have to read the text from the text field and build the URL in your gourl method, not in the load method.
-(IBAction)goToURL {
url = [[NSString alloc] initWithFormat:#"http://www.example.com/search?query=%#", restlabel.text];
[restaurants loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:url]]];
}
You can't define a string with a placeholder that will be dynamically changed (I think that was what you were trying to do). You build a string with a "snapshot" of the variables you are referencing.
Found my own answer.
-(IBAction)gorest {
restlabel.text = [NSString stringWithFormat:#"%#", [zip text]];
NSString *url;
url = [[NSString alloc] initWithFormat:#"http://maps.google.com/maps?q=Restaurants+in+%#", restlabel.text];
[restaurants loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:url]]];
next.hidden = NO;
}
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 am trying to open a url
(http://c22.smaato.net/oapi/lp.jsp;jsessionid=E253E547A55290CA553F493659433DBF.c22)
on a button through the following code
NSString *strs=[[NSString alloc]initWithFormat:#"%#",[linkArry objectAtIndex:0]];
NSURL *urls = [NSURL URLWithString:strs];
[[UIApplication sharedApplication] openURL:urls];
[linkArry objectAtIndex:0] is the link mentioned above.
but it is not responding?? if i type something like "http://www.google.com" it works..
is there any other method to open these urls??
Check the line
NSURL *urls = [NSURL URLWithString:strs];
NSlog("urls : %#", urls);
and print the urls in console , if you find urls is nil,
then escape the strs with NSUTF8StringEncoding.
strs = [strs stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL *urls = [NSURL URLWithString:strs];
Make sure that url is of NSURL type and not NSString