%2C URL causing iOS app crash - ios

I have an iOS application which downloads a JSON feed from this URL:
https://www.googleapis.com/youtube/v3/activities?part=snippet%2CcontentDetails&home=true&maxResults=50&access_token=%#
I am storing the URL in a NSString for later use. I am also adding a NSString to the end of the URL which contains an access token which I am using for OAuth Authentication (hence the %# at the very end of the URL).
Here is how I am storing the URL:
NSString *pre_yt_user_url = [NSString stringWithFormat:#"https://www.googleapis.com/youtube/v3/activities?part=snippet%2CcontentDetails&home=true&maxResults=50&access_token=%#", token_youtube];
As you can see part of the URL has a %2C
This is causing a warning and making my iOS app to crash!!
Here are the warning I get:
Format specifies type 'unsigned-short' but the argument has type NSString
and:
More % conversions than data arguments
What am I doing wrong here? Can't I store a URL in a string??
Thanks, Dan.

When using stringWithFormat the % character is the start of a data argument unless it's escaped. So you need to escape it because you don't want to use it as a supplied parameter. You need to use %%2C (because the first % escapes the second %).

Related

Using '#' end of openURL

let x = dpadtxt.text! + "#"
let url = URL(string: ("tel://23456712561,2#,\(x)"))!
UIApplication.shared.openURL(url)
when I add # at the end of the string it gives me this error:
fatal error: unexpectedly found nil while unwrapping an Optional value
Take a look at Apple URL Scheme reference documentation where you can read about how to work with telephone number encoding.
In that document you can read...
To prevent users from maliciously redirecting phone calls or changing
the behavior of a phone or account, the Phone app supports most, but
not all, of the special characters in the tel scheme. Specifically, if
a URL contains the * or # characters, the Phone app does not attempt
to dial the corresponding phone number. If your app receives URL
strings from the user or an unknown source, you should also make sure
that any special characters that might not be appropriate in a URL are
escaped properly. For native apps, use the
stringByAddingPercentEscapesUsingEncoding: method of NSString to
escape characters, which returns a properly escaped version of your
original string.
You have to encode you number String with something like this
let phone: NSString = "tel://23456712561,2#,#"
if let phone = phone.addingPercentEncoding(withAllowedCharacters: .urlHostAllowed)
{
print(phone)
}
Please, check if my NSCharacterSet is correct, cause I can't test it on a device right now.

How to Hardcode a NSURL path?

So, this seems like it should be super easy, but i'm stumped...
I have an iOS application that I am developing where I am downloading video content from a server, storing it in a custom folder in the Documents, and upon a certain trigger, trying to play the video. I am experiencing some loading errors in my MovieViewController, so am trying to debug the NSURL that is constructed from the file path in a separate standalone application. It is worth noting that the same content works fine when added to the Bundle, but there's quite a lot, so can't afford to do that.
Anyways...
I have set breakpoints in my app and copied out the path of the NSURL that gets loaded into my movie player, and am attempting to hardcode it into an NSURL in my test app. But I can't seem to just assign it with a preexisting value... Eg.
NSURL *url = "file:///var/mobile/Containers/Data/Application/21078F3B-12C5-4D42-8B8B-3C85CB7A0A91/Documents/SecondStory/BloodAlley/MEDIA/copperthief.mp4"
(which is what I copied out of the Variables View of the Debug area).
It gives me the error:
*"Implicit conversion of a non-Objective-C pointer type 'char *' to 'NSURL ' is disallowed with ARC"
... which I cannot seem to track down on SO ....
Of course, I have the NSString representation of the file path before it gets converted to an NSURL, so can just try to reconstruct it, but this got me curious why I couldn't just assign a value...
As the error states, "file:///var/.../copperthief.mp4" is a char * not an NSURL.
To convert it to an NSURL, first you have to get the char *'s NSString representation by adding the # prefix:
#"file:///var/.../copperthief.mp4"
then convert the NSString to an NSURL using URLWithString::
[NSURL URLWithString:#"file:///var/.../copperthief.mp4"];
The first thing is that "..." is not an NSString. It's a C string. An NSString would be #"...", wouldn't it? And even then, if you wanted an NSURL, you'd have to convert from NSString to NSURL, wouldn't you? I mean, you can't assign a string of any kind to an NSSURL variable and expect it to work, can you? This is a computer language; you can't say just any old thing that comes into your head - you have to obey the rules.

ios issue with stringByAddingPercentEscapesUsingEncoding

In my app I need to send some parameters to the url, when I am trying with the stringByAddingPercentEscapesUsingEncoding it is not converting correctly. If I am not using this encoding I am getting null(Exception) from the nsurl.Here is me code.
http://www.mycompurl.co?message=xyz&id=____ here I am sending the id 1 or 2 or any number.
when I convert this string to url by using stringByAddingPercentEscapesUsingEncoding I got
"http://www.mycompurl.co?message=xyz&id=**%E2%80%8B**1" (when I send 1 as parameter). Then I got the 0 data from the Url.
str = [NSString stringWithFormat:#"%#?message=xyz&id=​%#",Application_URL,bootupdateNew];
str = [str stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding];
url=[NSURL URLWithString:str];
NSError* error = nil;
data1 = [NSData dataWithContentsOfURL:url options:NSDataReadingUncached error:&error];
Thank you In advance
Basics
A URL is composed of several components.
Each component has its own rule how the component's source string must be encoded, so that this component becomes valid within the URL string.
Applying stringByAddingPercentEscapesUsingEncoding: will never always produce a correct URL if the string consists of more than one component (and if we assume, we have an unbounded set of source strings - so that the encoded string actually differs from the source string).
It even won't work always with a string which represents any single component.
In other words, for what's worth, stringByAddingPercentEscapesUsingEncoding: should not be used to try to make a URL out of several components. Even getting the URL query component correctly encoded is at least error prone, and when utilizing stringByAddingPercentEscapesUsingEncoding: it still remains wonky. (You may find correct implementations on SO, though - and I posted one myself).
But now, just forget about it:
It took awhile for Apple to recognize this failure, and invented NSURLComponents. It's available since iOS 7. Take a look! ;)

Get request length limit in iOS

I am using Omniture SiteCatalyst in my iPhone app.It uses get request to hit the servers internally via its sdk.However i am facing an issue where some of the request are not reaching the Omniture servers.The get request which is being sent is of variable length depending on the type of request(around 900 + characters).
My question is whether there any limit for the get request length in an iOS app? and if yes
how it would behave in case the request crosses the limit?
Theoretically if URL conforms to RFC 2396 it is fine. According to documentation
The NSURL class fails to create a new NSURL object if the path being
passed is not well-formed; the path must comply with RFC 2396.
Examples of cases that will not succeed are strings containing space
characters and high-bit characters. Should creating an NSURL object
fail, the creation methods return nil, which you must be prepared to
handle. If you are creating NSURL objects using file system paths, you
should use fileURLWithPath: or initFileURLWithPath:, which handle the
subtle differences between URL paths and file system paths. If you
wish to be tolerant of malformed path strings, you’ll need to use
functions provided by the Core Foundation framework to clean up the
strings.
But some time there is issue with specail character e.g. space, accents and others. You must [NSURL URLWithString:[urlString stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding]];
It is also possible server could not handle very long urls, if there are any limitation on server, server will simple truncate the rest of url string, if this is an issue then server will return 414 error url too long.

Getting NSData from a url works in the simulator but not on a device

I implement a method that send notes to the server:
-(IBAction)inserttotextfied:(id)sender{
NSString *strurl=[NSString stringWithFormat:#"http://localhost/get-data/insert.php?Name=%#&message=%#",txtf.text,txt2.text];
NSData *dataurl=[NSData dataWithContentsOfURL:[NSURL URLWithString:strurl]];
NSString *stresult=[[[NSString alloc]initWithData:dataurl encoding:NSUTF8StringEncoding]autorelease];
NSLog(#"%a",stresult);
}
The problem is when I test it via simulator the is being sent, but when I test it in the device the data did not being saved
You probably don't want to send something to localhost on your device, or do you use a different url on the device build?
Simulator is faster in response. So the url return data and print properly. But on device the response time is higher then simulator. Your NSLog(#"%a",stresult); statement is executing before it get any data from the response. I will suggest to give some delay or use delegate so that you can use data after getting the response.
Format Specifier %a is 64-bit floating-point number (double), printed in scientific notation with a leading 0x and one hexadecimal digit before the decimal point using a lowercase p to introduce the exponent.
if that is not your intent, try this:
NSLog(#"%#", stresult);

Resources