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
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:)
NSString *urlAddress = [NSString stringWithFormat:#"http://www.domain.com?input=%#",[alertView textFieldAtIndex:0].text];
NSURL *myUrl = [NSURL URLWithString:[urlAddress stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
NSString *openURL = [NSString stringWithContentsOfURL:myUrl encoding:NSUTF8StringEncoding error:NULL];
NSLog(#"%#",openURL);
openUrl is always returning (null), probably because of the encoding, but I don't know how to fix it.
Double check these three things:
Check that the URL which you are pointing to is a valid URL. The null value could be a result of an invalid URL.
Catch the NSError which the stringWithContentsOfURL throws. That will give you some insight on what is wrong.
Like you suspect, try changing the string encoding.
NSError *error;
NSString *urlString = [NSString stringWithFormat:#"http://www.domain.com?input=%#", stringVariable];
NSURL *urlAdress = [NSURL URLWithString:urlString];
NSString *urlContent = [[NSString alloc] initWithContentsOfURL:urlAdress encoding:NSUTF8StringEncoding error:&error];
NSLog(#"%#", urlContent);
This works fine, the problem was an failing internet connection.
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];
}
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 have an NSURL, a file path, and I want to add an NSString to the end of it (the file name) how can I do this? But after this is don't I want the entire thing to be an NSURL.
Thanks.
I think it's good solution:
NSURL *bUrl = [aUrl URLByAppendingPathComponent:#"newString"];
In Swift you could do the following,
var bURL = aURL.URLByAppendingPathComponent( "newString" )
You can also state whether the URL is a directory,
var bURL = aURL.URLByAppendingPathComponent( "newString", isDirectory: true )
I think it's as simple as:
NSString *s = [aUrl.path stringByAppendingString:#"newString"];
If you have a file NSURL to a directory and you want to end up with a NSString containing the NSURL's path with a file name appended to it, use this:
NSURL *url = [NSURL fileURLWithPath:#"/System" isDirectory:YES];
NSString *filename = #"foo";
NSString *result = [url.path stringByAppendingPathComponent:filename];
You can also use URLByAppendingPathComponent but that adds an extra step which creates an extra NSURL object that isn't needed.
NSURL *url = [NSURL fileURLWithPath:#"/System" isDirectory:YES];
NSString *filename = #"foo";
NSURL *newURL = [url URLByAppendingPathComponent:filename];
NSString *result = newURL.path;