How can I search a NSString for a url in Xcode iOS?
For instance, If scan it for
HTTP://
and detect where the end of that url is and return the url string.
NSSearchFieldCell Class Reference
is only available on Mac development.
Check out this link I think it should be work NSDataDetector
Related
I am building an app using Xamarin's Unified API and am creating a NSUrl with a string. However the app crashes as soon as I try to create the NSUrl.
string url = "comgooglemaps-x-callback://?daddr=" + destination;
NSUrl googleMapsURL = new NSUrl (url);
The value of url is comgooglemaps-x-callback://?daddr=600+Drake+Apartments\n600+Drake+St,+Vancouver+BC+V6B+5W7,+Canada
The message in the console is:
Could not initialize an instance of the type 'Foundation.NSUrl': the
native 'initWithString:' method returned nil. It is possible to ignore
this condition by setting
MonoTouch.ObjCRuntime.Class.ThrowOnInitFailure to false.
How do I fix this ?
This will work (tested with your above code).
using System.Web.Services;
...
string destination = System.Web.HttpUtility.UrlEncode(
"600 Drake Apartments\n600 Drake St, Vancouver BC V6B 5W7, Canada");
Now turns into
"comgooglemaps-x-callback://?daddr=600+Drake+Apartments%0a600+Drake+St%2c+Vancouver+BC+V6B+5W7%2c+Can…"
Add Reference System.Web.Services to the project. My guess is that your URL encoding, wherever it's coming from isn't compatible with what the Apple APIs expect.
I had implemented pinterest sharing code in my app as well. Its working fine. But problem arrives at one scenario see follow
Correct working:
[pinterest createPinWithImageURL:[NSURL URLWithString:imageUrl]
sourceURL:[NSURL URLWithString:[NSString stringWithFormat:#"%#",shareUrl]]
description:#"My Description"];
Then it will share Pinterest Description same My Description as per my expectation.
But when I send Description Test like :
[pinterest createPinWithImageURL:[NSURL URLWithString:imageUrl]
sourceURL:[NSURL URLWithString:[NSString stringWithFormat:#"%#",shareUrl]]
description:#"My Details & Description"];
Then it will share Pinterest Description like My Details.
My expected text here is My Details & Description this is trunks my string after & symbol.
What actually wrong happening with me please look at here.
AFAIK, Pinterest's Pin It SDK isn't open source, so it's difficult to find out what's really going on.
I would guess, however, that this method is creating a GET request under-the-hood that's incorrectly URL encoding the description parameter (perhaps they're using stringByAddingPercentEscapesUsingEncoding or some other naive method).
I'd recommend contacting the Pinterest SDK developers/maintainers to look into this.
As a quick fix, you might try URL encoding the & yourself. For example, you might try replacing & with %26, e.g.
NSString *description = // ...whatever it should be set to...
description = [description stringByReplacingOccurrencesOfString:#"&" withString:#"%26"];
However, this might actually lead to other problems as the Pinterest SDK is likely doing some sort of URL encoding and would likely encode the % symbol.
Another naive approach may simply be to replace & with the word and, such as
NSString *description = // ...whatever it should be set to...
description = [description stringByReplacingOccurrencesOfString:#"&" withString:#"and"];
Again, it's hacky, but it's a workaround for a bug that's likely in the underlying SDK.
I'm trying to add a base url for my networking code, the problem is that this URL gets broken when passing to the URLWithString:relativeToURL: method. This URL has the port i'm using, however, after calling the described, the URL is wrong, not including my current port number. I think this is a problem with percent escapers, but i've tried some methods to solve this problem without success.
Here is my code:
// absolute string returns a url a with broken path
[[NSURL URLWithString:#"api/whatever" relativeToURL:[NSURL URLWithString:#"193.178.0.99:9000"]] absoluteString]
// printed absolute path 193.173.0.99:///api/whatever
Other tried approaches:
NSString *baseURLString = (NSString *)CFURLCreateStringByAddingPercentEscapes(NULL,(CFStringRef)#"193.178.0.99:9000",NULL,(CFStringRef)#":",kCFStringEncodingUTF8);
[[NSURL URLWithString:#"api/whatever" relativeToURL:[NSURL URLWithString:baseURLString]]
// Printed path : 193.173.0.99%3A8000/api/whatever, this path is still not working, although i have the percent escape set.
NSString *baseURLString = [#"193.173.0.99:8000" stringByAddingPercentEscapesUsingEncoding : NSUTF8StringEncoding];
// ... The same final code from above.
// Printed -> the very same first result.
EDIT : From the comment above the URLWithString:relativeToURL: : "These methods expect their string arguments to contain any percent escape codes that are necessary."
Does anybody have a solution to this problem ?
Thanks.
Actually the solution is pretty simple... just add the scheme.
Something like this:
NSURL *baseURL = [NSURL URLWithString:#"http://193.178.0.99:9000"];
NSString *absoluteString = [[NSURL URLWithString:#"api/whatever" relativeToURL:baseURL] absoluteString];
// Prints => http://193.178.0.99:9000/api/whatever
That behavior is actually quite understandable (from an RFC point of view): the relativeToURL part is expected to be a full-fledged URL root, including the URL scheme.
So here in your example, as you didn't provide an http:// scheme or similar, 193.178.0.99 is considered to be the scheme — like it would be http or https or ftp or tel or mailto — and the 9000 port considered to be the host part of your URL (but as 9000 is probably not a valid host according to the RFC, it's probably why you have the warning by the way)
In a way, 193.178.0.99:9000 is interpreted in a similar manner a phone-number URL tel:1-541-754-3010 or a mail URL mailto:john.doe#nowhere.com would; the : separating the URL scheme from the host, not separating the host from the port.
To solve this, simply include the URL scheme (like http or https or whatever the protocol you intend to use) in the relativeToURL parameter:
[[NSURL URLWithString:#"api/whatever"
relativeToURL:[NSURL URLWithString:#"http://193.178.0.99:9000"]]
absoluteString]; // ^^^^^~~ this is the important part
Note: as an alternate solution to build your URL, you could use the iOS7's NSURLComponents class to manipulate NSURL parts separately, that's another way to break down and build up URLs
I had written a code snippet which writes the contents of a textfield to a file. Unfortunately my code, depending on the OS, writes the file path differently
In Yosemite, The path is
file:///var/folders/qg/....../myfile.txt
While in mountain lion the path is
file://localhost/var/folders/yx....../myfile.txt
I have an API which takes the file path as /var/folder/xx/...../myfile.txt
I was wondering if there is a way to make a substring from /var/.. till the end of the path.
You could do something along the following:
// Lets say pathString is an NSString of the path
NSRange varRange = [pathString rangeOfString:#"/var/"];
NSString *correctPath = [pathString substringFromIndex:varRange.location];
In the above example, you use NSString's instance method rangeOfString: to receive the range of the wanted substring, which in this case is /var/, and store it into a range variable.
Then you create a new NSString variable, using the original pathString, with the use of the instance variable substringFromIndex, which returns a new substring, which start at the index you choose, and ends at the end of the string (which you provide the range location we've received, to identify where /var/ begins).
Good luck mate.
If file path is store in NSString *yourPathString, then
NSString *resultString= [yourPathString substringFromIndex:[yourPathString rangeOfString:#"/var"].location]];
NSLog(#"Final result : %#", resultString);
Try this code, hope it helps.
Depending on how you're calling your resource, how it needs to be saved, and what that API is looking for, you'll want to use NSURL methods, not strings. Specifically, it sounds like you'll be most interested in using absolutePath, relativePath, resourceIdentifierand pathComponents
I would recommend reading the URL Loading System guide by Apple or NSHipster's article for a more human readable answer.
In one of my projects I'm capturing the Lat/Long coords in a core data store, and later on providing this information in an email with Attached PDF.. On the receiving end, I'd like the convenience of another iPhone user to be able to click on the link, and go to that location in Apple Maps
I find the native apple map comes up, but it's simply an open map view w/no pin:
NSString *appleLink2 = [NSString stringWithFormat:#"http://maps.apple.com/maps
ll=%#,%#",self.lat,self.lon];
I'm creating the clickable in a common fashion with a CGRect, and NSURL
It appears on the surface that I should probably get the street address instead of the Lat/Long coords also, and use the address for better map resolution... Thoughts?
Also, I'm looking for examples of Apples URL Schemes to query strings mentioned in the Docs, but can't find any real examples... ll = , son =, t = , z =. I understand saddr and daddr, and am surprised that I can't find any reference or real examples anywhere.
If you want the pin to be placed on the map in Apple's Maps app, use a 'q' instead of 'll' in the URL:
NSString *appleLink2 = [NSString stringWithFormat: #"http://maps.apple.com/maps?q=%#,%#", self.lat, self.lon];
The following apple link has all details for the questions
http://developer.apple.com/library/ios/#featuredarticles/iPhoneURLScheme_Reference/Articles/MapLinks.html
USE NSString for URL
NSString *appleLink2 = [NSString stringWithFormat:#"http://maps.apple.com/maps
ll=%#,%#",self.lat,self.lon];
NSURL *myUrl = [NSURL URLWithString:[appleLink2 stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
Then use myUrl is as URL.