iOS - Pinterest Sharing Description Text not working if I send "&" in description text - ios

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.

Related

split string in ios string from json data?

I have one string. Now I wanted to split this string. For static separation I know the code but I don’t code for dynamic value.
my string is
NSString *str = #"https://graph.facebook.com/v2.5/181054825200000/feed?fields=created_time,message,picture,full_picture,comments.limit%280%29.summary%28true%29,likes.limit%280%29.summary%28true%29&limit=5&format=json&access_token=CAALjFrE5mNYBAOg1EDiUrsE2kr1kIRrLIv7g4OweSMvHso2exB5Dttshn7dgOlW24ZCXSnDZAWiV6xMUKXedTXUhiHpdmZBPCGzD1orFlrLRP2gaBZCbZBZBnjUHewF9hZBmJKxtiwVzpw9gnnQXk5Hfx0ZBM2ksAUzkSWR5feaNMbf3UUmUpJlxeh0gKdDrzWBvIJRPy0xGqL0ZAMFsRhyCZCTX42l1sZAceZB0VCeDZB95mrAZDZD&until=1456345291&__paging_token=enc_AdCKD3tSYMoZB3MCKaJkYnbVmBgUyY2tBceGDD2G1hqxRDiQKZCsSbmvWZASLvlCMf0BVzq2uZAScSWp7ZAavZB2d72BIHJISefk09noRuv9gA5b5hFwZDZD";
but i don’t how to show any value dynamically .(for e.g. until (in string))
please help me for this issue.
Thank You.
If you are parsing a URL you should really use NSURLComponents. It makes breaking a URL into the different parts much easier, and the code is tested and verified by Apple.
For separate string by a separator you can use this.
NSString *url = #"<url>";
NSArray *array = [url componentsSeparatedByString:#"<seperator string>"];
NSLog(#"%#", array);
But for URL parsing ,As per Duncan's answer, yes it is good to parse a URL using NSURLComponents. By using this class you can get any desired part of an URL.

Connect this NSSting with a String Variable

All I want is to change every time the NSString townLocation.
Because I take data from an API and I don't want to create different API for different location. Also I know that the "+" that I put on the link is not correct and there is not such think in Objective C but I want to make you understand what I want.
NSString*townLocation;
NSData* data = [NSData dataWithContentsOfURL:[NSURL URLWithString:#"http://api.openweathermap.org/data/2.5/find?q="+townLocation+"&units=metric"]];
How I must do it ? Im sure you understand that I'm new at Objective C
Thank you
You only need to look into the most basic NSString documentation to find a method that will do that, stringWithFormat:.
NSString *urlString = [NSString stringWithFormat:#"http://api.openweathermap.org/data/2.5/find?q=%#&units=metric", townLocation];
NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:urlString]];
If you're new at Objective-C, a good place to find information like this is to simply search the internet or the iOS Developer Library for the class in question (in this case, NSString) to find a myriad of resources at your disposal. Another doc to check would be Formatting String Objects, which is linked in the stringWithFormat: section of the iOS Developer Library, to find more info about formatting strings.

Reference website live updating status from App

I have a website: http://www.grandforks.af.mil/library/weathercenter/index.asp
There is a logo on the middle of the page for the current road conditions on base.
The file names for each condition is different by 1 number.
Green is 19
Yellow is 20
Red is 21
I do not have control over this website.
My question is how can I have the current road conditions in an app that I am building display as a reflection of the website? I don't want to have any user input I want the app to be able to pull off of the site. I have tried using UIWebView but I cannot get it to focus on that area of the page. If I could do that it would probably be the better solution.
Ugly solution:
you can download the html of the website, parse it searching the string "http://www.grandforks.af.mil/shared/media/document/AFD-121002-" and get the actual URL (which is something like http://www.grandforks.af.mil/shared/media/document/AFD-121002-019.jpg).
After that, you can load that URL in your UIImageView or (better) discriminate and show your image that is included in the app.
The ugliness depends by the fact that if they change the image's URL, your app stops working.
I would suggest that when the user opens the app, it goes to check your page and eventually download the string your are looking for. Doing so, if the website changes the url, you can manually modify the source without update the app.
I don't know if using others information is legal.
EDIT:
here is an example that I built that demonstrate how you can substring a string:
NSString *myString = #"11111www.the-site.com/img42.jpg222222222222222";
NSString *searchString = #"www.the-site.com/img";
NSRange range = [myString rangeOfString:searchString];
NSLog(#"searched string strart at character %lu, and it's long %lu charactrers", (unsigned long)range.location, (unsigned long)range.length);
NSInteger index = range.location + range.length;
NSString *mySubString = [myString substringFromIndex:index];
NSLog(#"string from the index: %#", mySubString);
//I know that the substring I'm looking for is long exactly 2 characters
NSString *the_answer = [mySubString substringToIndex:2];
NSLog(#"this should print 42: %#", the_answer);
I added some prints, so you can see step by step what I'm doing.
Obviously you have to change "searchString" with "http://www.grandforks.af.mil/shared/media/document/AFD-121002-" and myString is the html of the page.
I hope it helps.

iOS Apple Map native links - URL Scheme examples

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.

Is it possible to use the Google Translate api with iOS?

How do I use the google translate API with iOS 6.0? I have tried googling the documentation and tried to research it, but I can't find anything that is updated since google made the api to be no longer free.
The only thing I have been able to find was this, but it didn't really help much because I need to use the api key that I have, but I have no idea how to.
http://www.raywenderlich.com/1448/how-to-translate-text-with-google-translate-and-json-on-the-iphone
This seems up to date: https://developers.google.com/translate/v2/getting_started
(completely meta: Oops! Your answer couldn't be submitted because:
body must be at least 30 characters; you entered 23)
Yes, it is. Once you get your Google key, just plug it into FGTranslator. Note, generate a "server" key, since the Google Translate API does not currently support iOS keys.
It's just a one-line call to the translate function after that.
FGTranslator *translator = [[FGTranslator alloc] initWithGoogleAPIKey:#"your_google_key"];
[translator translateText:#"Bonjour!"
completion:^(NSError *error, NSString *translated, NSString *sourceLanguage)
{
NSLog(#"translated from %#: %#", sourceLanguage, translated);
}];

Resources