stringwithcontentsofurl file deprecated in ios 2.0 warning IOS7 - ios

Hi in my application I'm showing the route map using google map in that its showing warning like .
stringwithcontentsofurl file deprecated in ios 2.0
The code which I'm using.
-(NSArray*) calculateRoutesFrom:(CLLocationCoordinate2D) f to: (CLLocationCoordinate2D) t {
NSString* saddr = [NSString stringWithFormat:#"%f,%f", f.latitude, f.longitude];
NSString* daddr = [NSString stringWithFormat:#"%f,%f", t.latitude, t.longitude];
NSString* apiUrlStr = [NSString stringWithFormat:#"http://maps.google.com/maps?output=dragdir&saddr=%#&daddr=%#", saddr, daddr];
NSURL* apiUrl = [NSURL URLWithString:apiUrlStr];
NSLog(#"api url: %#", apiUrl);
NSString *apiResponse = [NSString stringWithContentsOfURL:apiUrl];
NSString* encodedPoints = [apiResponse stringByMatching:#"points:\\\"([^\\\"]*)\\\"" capture:1L];
return [self decodePolyLine:[encodedPoints mutableCopy]];
}
In the above code in a particular line.
NSString *apiResponse = [NSString stringWithContentsOfURL:apiUrl];
This above line its showing warning like that please tell me how to resolve this issue.
Thanks.

As it was depreciated.You can use it as below
NSString *apiResponse = [NSString stringWithContentsOfURL: apiUrl encoding:NSUTF8StringEncoding error:nil];

The method you are using is deprecated. Use [NSString stringWithContentsOfURL: encoding: error:].

Related

iOS: App Share URL using objective C

I have created the code to share my app on whatsapp. The problem is when the share link is sent from ios to an android phone, the url link and text all are displayed in plain text format in android phone's whatsapp chat box, without highlighted link and url.
Can this be fixed? How?
NSString *urlString = [NSString stringWithFormat:#"%#", APP_SHARE_URL];
NSString *initialText1 = [NSString stringWithFormat:#"Hey I am using App: %#\n%#",urlString, profileModel.name];
NSCharacterSet *set = [NSCharacterSet URLHostAllowedCharacterSet];
NSString *whatsappString = [NSString stringWithFormat:#"%#", [[NSString stringWithFormat:#"%#", initialText1] stringByAddingPercentEncodingWithAllowedCharacters:set]];
NSURL *whatsappURL = [NSURL URLWithString:[NSString stringWithFormat:#"whatsapp://send?text=%#", whatsappString]];
if ([[UIApplication sharedApplication] canOpenURL:whatsappURL]) {
[[UIApplication sharedApplication] openURL:whatsappURL];
}
else {
[self showMessage:#"Unable to open WhatsApp"];
}
I am using this code for sharing on whatsapp and it works absolutely fine. Plain text issue occurs in Android OS 5.0 and earlier.
NSString *textToShare = [self getEncodedString:text];
NSString *urlStr = #"whatsapp://send?text=";
urlStr = [NSString stringWithFormat:#"%#%#",urlStr,textToShare];
NSURL *url = [NSURL URLWithString:urlStr];
[[UIApplication sharedApplication] openURL:url]
- (NSString*)getEncodedString:(NSString*)string {
NSString * encodedString = (NSString *)CFBridgingRelease(CFURLCreateStringByAddingPercentEscapes(
NULL,
(CFStringRef)string,
NULL,
(CFStringRef)#"!*'\"();:#&=+$,/?%#[]% ",
kCFStringEncodingUTF8 ));
return encodedString;
}

how to replace space with dash in objective-c?

Hey folkks I'm working with an api that search about movies now the problem is when i type single word in UISearchBar it works but when I type space for another word it dose'nt work.Near query=%# when i type single word it works but when I type another word with space it won't
NSString *movieName=searchBar.text;
NSString *movieString = [NSString stringWithFormat:#"https://api.themoviedb.org/3/search/movie?query=%#&api_key=c4bd81709e87b1209433c49",movieName];
NSURL *url=[NSURL URLWithString:movieString];
You don't need to replace Space with Dash you new to URL Encode your string
Below is an example of encoding your string to URL Encode
NSString *movieName=searchBar.text;
movieName = [movieName stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLUserAllowedCharacterSet]];
NSString *movieString = [NSString stringWithFormat:#"https://api.themoviedb.org/3/search/movie?query=%#&api_key=c4bd81709e87b1209433c49",movieName];
NSURL *url=[NSURL URLWithString:movieString];
Use
formattedString = [originalString stringByReplacingOccurrencesOfString:#" " withString:#"-"];
Please refer this code.
NSString *movieName=searchBar.text;
NSString* replacedMovieName = [movieName stringByReplacingOccurrencesOfString:#" " withString:#"_"];
Hope this helps.
No need replace space with dash, need to add Percent Escapes in string before creating NSURL.
Use NSString class method:
-(NSString *)stringByAddingPercentEscapesUsingEncoding:(NSStringEncoding)encoding
Like,
NSString *movieName=searchBar.text;
//This is what you need
movieName=[movieName stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSString *movieString = [NSString stringWithFormat:#"https://api.themoviedb.org/3/search/movie?query=%#&api_key=c4bd81709e87b1209433c49",movieName];
NSURL *url=[NSURL URLWithString:movieString];
OR
Alternately you can do following(Not best approach):
NSString *movieName=searchBar.text;
//This is what you need
movieName = [movieName stringByReplacingOccurrencesOfString:#" " withString:#"%20"];
NSString *movieString = [NSString stringWithFormat:#"https://api.themoviedb.org/3/search/movie?query=%#&api_key=c4bd81709e87b1209433c49",movieName];
NSURL *url=[NSURL URLWithString:movieString];
NSString *movieName=searchBar.text;
movieName = [movieName stringByReplacingOccurrencesOfString:#" " withString:#"-"];//use vice-versa
NSString *movieString = [NSString stringWithFormat:#"https://api.themoviedb.org/3/search/movie?query=%#&api_key=c4bd81709e87b1209433c49",movieName];
NSURL *url=[NSURL URLWithString:movieString];

Show Location string using latitude and longitude

I am using this method to show the string of location using current location latitude and longitude but it is showing differently
NSString *urlString = [NSString stringWithFormat:#"http://maps.googleapis.com/maps/api/geocode/json?latlng=%f,%f&sensor=false",location.coordinate.latitude, location.coordinate.longitude];
NSError* error;
NSString *locationString = [NSString stringWithContentsOfURL:[NSURL URLWithString:urlString] encoding:NSASCIIStringEncoding error:&error];
NSData *data = [locationString dataUsingEncoding:NSUTF8StringEncoding];
id json = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
NSDictionary *dic = [[json objectForKey:#"results"] objectAtIndex:0];
NSArray* arr = [dic objectForKey:#"address_components"];
//Iterate each result of address components - find locality and country
NSString *cityName;
NSString *countryName;
for (NSDictionary* d in arr)
{
NSArray* typesArr = [d objectForKey:#"types"];
NSString* firstType = [typesArr objectAtIndex:0];
if([firstType isEqualToString:#"locality"])
cityName = [d objectForKey:#"long_name"];
if([firstType isEqualToString:#"country"])
countryName = [d objectForKey:#"long_name"];
}
NSString* locationFinal = [NSString stringWithFormat:#"%#,%#",cityName,countryName];
NSLog(#"Final Location %# ",locationFinal);
but final location is showing this type :-
Final Location नठदिलà¥à¤²à¥,India
Why it is showing this type? Can anyone know about this.
Please supply the language with the API params. If language is not supplied, the geocoder attempts to use the preferred language as specified in the Accept-Language header, or the native language of the domain from which the request is sent.
So please replace the code as with the language parameter as like this.
NSString *urlString = [NSString stringWithFormat:#"http://maps.googleapis.com/maps/api/geocode/json?latlng=%f,%f&sensor=false&language=en",location.coordinate.latitude, location.coordinate.longitude];
and try again.
I believe that is an uninitialzed variable which is pointing into random memory.
Try:
NSString *cityName = nil;
NSString *countryName = nil;
Short-circuit your for loop:
for (NSDictionary* d in arr)
{
// Add this after the existing code:
if (cityName && countryName)
break;
}
and check for errors before presenting the results:
if (cityName && countryName) {
NSString* locationFinal = [NSString stringWithFormat:#"%#,%#",cityName,countryName];
NSLog(#"Final Location %# ",locationFinal);
} else {
NSLog(#"Failed to find location");
}
Finally your JSON-processing code does no error-checking at all. That's a mistake.

NSTask not working for file path with special characters

I am using NStask to get the folder size.Its working fine for normal file paths like home/ABC/Users/testUser/Documents. But it is not showing any output for file path like home/ABC/Users/testUser/Ω≈ç∂√√√∂ƒ∂.However I am getting proper output for the same file path on terminal. Any suggestion?
NSString* userName = #"test123_test123";
NSString* password = #"test";
NSString* serverIP = #"200.144.172.210";
NSString* sizeFolderPath = [[NSBundle mainBundle] pathForResource:#"demoUtilities" ofType:nil];
NSString* xml= #"--test";
NSString* passwordArg = [NSString stringWithFormat:#"--pwd-=%#",password];
NSString *filePath = #"UsersMac/Users/test/Ω≈ç∂√√√∂ƒ∂";
NSString* address = [NSString stringWithFormat:#"%##%#::home/%#", userName, serverIP,filePath];
NSArray* arguments = [NSMutableArray arrayWithObjects:xml,passwordArg,address,nil];
TestCommandExcecuter *cmExec = [[TestCommandExcecuter alloc]init];
[cmExec setCommandString:sizeFolderPath];
[cmExec setCommandArguments:arguments];
I am using this code:
Depricated from 2.0
NSDictionary *fileDictionary = [[NSFileManager defaultManager]
fileAttributesAtPath:filePath traverseLink:YES];
long long size = [fileDictionary fileSize];
NSLog(#"size :: %lld",size);
Latest :
NSDictionary *fileDictionary = [[NSFileManager defaultManager]
attributesOfItemAtPath:filePath error:nil];
long long size = [fileDictionary fileSize];
NSLog(#"size :: %lld",size);
Here is the reference link :
https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSFileManager_Class/Reference/Reference.html#//apple_ref/occ/instm/NSFileManager/attributesOfItemAtPath%3aerror%3a

Incompatible pointer types NSString and NSArray why?

-(void)displayData:(NSString *)text{
NSLog(#"DATA SEND");
NSString *string1 = [NSString stringWithFormat:text];
NSString *separate = [string1 componentsSeparatedByString:#"B"];
separate is the one that gives issue? How do I properly do this? I'm trying to perform a string split.
componentsSeparatedByString: method returns NSArray not NSString, try that:
NSArray *seperate = [string1 componentsSeparatedByString:#"B"];
what is that?
NSString *string1 = [NSString stringWithFormat:text];
Right way
NSString *string1 = [NSString stringWithFormat:#"%#",text];
or
NSString *string1 = [NSString stringWithString:text];
and then Grag answer.

Resources