Parsing NSURL in Objective-C - objective-c

I have an NSTextView control that could potentially have links in it. How do I get the full url of the link?
Here is what I have so far
-(BOOL)textView:(NSTextView *)aTextView clickedOnLink:(id)aLink atIndex:(NSUInteger)charIndex
{
NSURL *htmlURL = [NSURL fileURLWithPathComponents:[aLink pathComponents]];
}
This gives me a URL that begins with file://localhost ... How do I get rid of that portion of the URL?

NSURL* url = [NSURL URLWithString:#"http://localhost/myweb/index.html"];
NSString* reducedUrl = [NSString stringWithFormat:
#"%#://%#",
url.scheme,
[url.pathComponents objectAtIndex:1]];

Related

Objective-C - Formatting GET request

What are the rules for passing the string parameter such as %JOHN% in a GET request url
my request url is supposed to look like: https://somesite.com/search/name?name=%SEARCH_KEYWORD%
Try#1: I did this
NSURL* url = [NSURL URLWithString:[NSString stringWithFormat:#"https://somesite.com/search/name?name=%%%#%%",SEARCH_KEYWORD]];
O/P: nil
Try#2:
NSURL* url = [NSURL URLWithString:[NSString stringWithFormat:#"https://somesite.com/search/name?name=%JOE%"]];
**O/P:
https://somesite.com/search/name?name=JOE
Any suggestions?
You can use NSURLComponents to build URLs:
Objective-C:
NSURLComponents *components = [NSURLComponents componentsWithString:#"https://google.com"];
components.query = #"s=%search keywords%"
NSURL *url = components.URL;
Swift (careful with ! in production, I used to test in Playgrounds):
let components = NSURLComponents(string: "https://google.com")!
components = "s=%search keywords%"
let url = components!
print(url) // "https://google.com?s=%25search%20keywords%25"
Also if you need more complex queries NSURLComponent have a queryItems property.
You are making a mistake, just use a single %#.
Example:
NSString *string = #"JOE";
NSURL *url = [[NSURL alloc] initWithString:[NSString stringWithFormat:#"https://somesite.com/search/name?name=%#",string]];

i want to take only simple url without http://

i want to add "http://" inside code so that user find it easy to browse by only typing a simple url
i have tried
- (IBAction)uurl:(id)sender {
NSURL *myurl = [NSURL URLWithString : #"http://"];
}
but its not working
//This is what the user types, depending on the type of input you use
NSString *urlString = #"www.bla.com";
NSString *httpString = #"http://";
urlString = [httpString stringByAppendingString:urlString];
//OR urlString = [NSString stringWithFormat:#"http://%#",urlString];
NSURL *url = [NSURL URLWithString:urlString];
Right this is simple enough to do but we need to check as well if the user inputs the http:// or https:// themselves. So see below with explanation in comments
- (IBAction)uurl:(id)sender
{
// Our to NSStrings that we want to be checking for.
static NSString *httpsStr = #"https://";
static NSString *httpStr = #"http://";
NSString *urlStr = #"www.google.com"; // User input however you take this
// Our check to make sure that the urlStr passed in does already have http or https
if(![urlStr hasPrefix:httpsStr] && ![urlStr hasPrefix:httpStr]) {
// If it doesn't have anything our default HTTP Protocol will be http
urlStr = [NSString stringWithFormat:#"http://%#", urlStr];
}
// If the user has entered the http or https themselves ignore reseting to http
// Set our urlStr as the URL we want to use.
NSURL *url = [NSURL URLWithString:urlStr];
// At this point you would do whatever it is you want to do with the url
}
Use -initWithScheme:host:path:
// your user input string
NSString *userString = #"www.apple.com";
NSURL *yourURL;
if (![userString hasPrefix:#"http://"])
yourURL = [[NSURL alloc] initWithScheme:#"http" host:userString path:#"/"];
else
yourURL = [[NSURL alloc] initWithString:userString];

UIWebView not opening a link

for(int i=0; i<arrString.count;i++){
if([[arrString objectAtIndex:i] rangeOfString:#"www."].location != NSNotFound)
link = [arrString objectAtIndex:i];
}
NSString *url = (#"THIS IS WHERE HTTP:// would Be!%#", link);
NSLog(#"THIS IS WHERE HTTP:// would Be!%#", url);
NSURL *nsurl=[NSURL URLWithString:url];
NSURLRequest *nsrequest=[NSURLRequest requestWithURL:nsurl];
[_webView1 loadRequest:nsrequest];
When I use NSLog:
NSLog(#"THIS IS WHERE HTTP:// would Be!, url);
It logs the correct URL that begins with http://. However, when I set the string URL equal to http://"link", it returns nothing. Any help is much appreciated.
(Stack Overflow is not letting me put the real HTTP:// followed by an %#.)
It looks like you may need to use stringWithFormat:
NSString *domainName = #"aol.com";
NSString *url = [NSString stringWithFormat:#"http://www.%#", domainName];

Drag&Drop NSURL without "file://"

I'm implementing my drag&drop method. I need that when user drags something on my app window I can get that file URL. NSURL needs to be converted to char. Thats OK. But how to remove file:// from url? My current code:
pboard = [sender draggingPasteboard];
NSString *url = [[NSURL URLFromPasteboard:pboard] absoluteString];
input_imageN = strdup([url UTF8String]);
its OK, but it gives url with file:// prefix. I tried using
NSURL *fileUrl = [[NSURL URLFromPasteboard:pboard] isFileURL];
NSString *url = [fileUrl absoluteString];
NSLog(#"url: %#", [NSURL URLFromPasteboard:pboard]);
input_imageN = strdup([url UTF8String]);
but it says that
Cannot initialize a variable of type 'NSURL *' with an rvalue of type 'BOOL' (aka 'signed char')
at
NSURL *fileUrl = [[NSURL URLFromPasteboard:pboard] isFileURL];
To go from a file URL to the path as a C string in the appropriate representation for the filesystem, you'd do:
NSURL *fileURL = [NSURL URLFromPasteboard: pboard];
NSString *filePath = [fileURL path];
char *filesystemRepresentation = [filePath filesystemRepresentation];
This avoids assumptions that stripping off the scheme leaves you with just the path, or that the filesystem is definitely happy accepting UTF8-encoded paths.
url = [url stringByReplacingOccurencesOfString:#"file://" withString:#""];
Hope this helps. Cheers!
#user23743's answer is correct. Since iOS 7 though NSURL has its own filestSystemRepresentation method.
In Swift:
if let fileURL = NSURL(fromPasteboard: pboard) {
let representation = fileURL.fileSystemRepresentation
}
if let fileURL = NSURL(from: pboard)?.filePathURL {
}
has been most effective for me.

Problem in converting urlString into NSURL in iphone sdk

I am having string at first the method calls with timestamp value nil and I am getting converted the string into url .next time when I click load more results button again the method calls with time stamp value assigned to it.but the url string is not converting into NSURL iam getting the null value into it.
-(NSMutableArray*)getTextMessagesArray:(NSString *)endTimestamp
{
printf("\n endtimestamp value...%s",[endTimestamp UTF8String]);
NSString *urlString = #"http://123.237.186.221:8080/upload/textRequest.jsp";
urlString = [urlString stringByAppendingString:#"?beginTimestamp="];
urlString = [urlString stringByAppendingString:#"&endTimestamp="];
if([endTimestamp length]>0)
{
urlString = [urlString stringByAppendingString:endTimestamp];
}
printf("\n &*(*(((urlString...%s",[urlString UTF8String]);
NSURL* aUrl = [NSURL URLWithString:urlString];
NSLog(#"url in appdelegaare in text...%#",aUrl);
[textParser parseXMLFileAtURL:aUrl];
textMessagesList = [textParser getTextMessagesList];
printf("\n textMessagesList Count in appDelegate....%d",[textMessagesList count]);
return textMessagesList;
}
The result I am getting in console is:
&*(*(((urlString...http://123.237.186.221:8080/upload/textRequest.jsp?endTimestamp=2010-10-08 16:20:47.0
url in appdelegaare in text...(null)
Guy's can any one suggest me why this happening
Anyone's help will be much appreciated.
Thanks to all,
Monish.
Your problem is that valid URLs cannot contains spaces. You want to do something along the following lines:
NSString *escapedUrlString = [urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
// escapedUrlString should be "http://123.237.186.221:8080/upload/textRequest.jsp?endTimestamp=2010-10-08%2016:20:47.0"
NSURL *aUrl = [NSURL URLWithString:escapedUrlString];
This might be what you wanted.