I've enabled push notifications for my phonegap app inside the AppDelegate.m file. The line of code I'm using is from a video tutorial, since I don't really know OBJ-C, and it is giving me a Format String Issue. Here's the code along with the error.
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
{
NSString *deviceTokenString = [NSString stringWithFormat:#"%#", deviceToken];
NSLog(deviceTokenString);}
Format is not string literal (potentially dangerous)
Knowing Javascript, I kind of understand what they mean by string literally but I'm not sure how to resolve it. Any ideas?
If the error is appearing on your NSLog line, then I think it refers to the fact that the format string - the first argument to NSLog, which tells it the string you want to output (potentially with substitution tokens) - is a variable, rather than a string literal. You might instead try:
NSLog(#"%#", deviceToken);
or if you use deviceTokenString elsewhere and want to keep that variable, you can do:
NSLog(#"%#", deviceTokenString);
All you really need is this if you are wanting to see that the device token was registered:
NSLog(#"My token is: %#", deviceToken);
This will give you the device token information. Get rid of the string literal because you are converting a string to a string. No real need to do that.
Related
Xcode debug log can print the method name using %B and the hit count using %H. How can I print the interface / object name using similar notation ? Preferably without the use of NSLog / debugger command.
The closest you can get is
NSLog("My file is %#", [NSString stringWithUTF8String:__FILE__]);
__PRETTY_FUNCTION__ contains the class name and method, which may suit you better than FILE.
I just found that #[self class]# log message will do the trick. I am not sure its validity on every occasion. I am using #[self class]# %B %H for my purpose. I also found this question and answer but it was not just working on Xcode 6.2.
Every Objective-C object can be printed with %#. NSLog(#"%#", someObject) will print object's description. You can override description method for your classes to provide more detailed info. Look at this note too: Improved logging in Objective-C
In the breakpoint's debugger command action, you can put:
expr printf ("[%s %s]\n", (char *)object_getClassName(self), _cmd)
This will output, e.g. [SomeClass someMethod:]
You can print anything using NSLog();
Say, for example, you have an
NSString *myString = #"qdfsqsdfqsdf";
This is an important string you would like to log, you'll just type :
NSLog(#"My important string : %#", myString);
If that is what you're asking, I'm surprised you dont' know that if you already know about %B & %H. Am I answering your question or do you need a bit more?
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 %).
I have a NSMutableDictionary holding EXIF metadata from a picture.
An example:
const CFStringRef kCGImagePropertyExifExposureTime;
Instead of accessing every key individually, I just want write the whole dictionary content into a label.
When I want to write this data into the console I would just use:
NSLog(#"EXIF Dic Properties: %#",EXIFDictionary );
That works fine, but if I use:
NSString *EXIFString = [NSString stringWithFormat:(#"EXIF Properties: %#", EXIFDictionary)];
I get warnings that the result is not a string literally and if I try to use that string to set my label.text, the program crashes.
Any idea where my error is?
[NSString stringWithFormat:(#"EXIF Properties: %#", EXIFDictionary)] is not, as you may think, a method with two arguments. It's a method with one argument. That one argument is (#"EXIF Properties: %#", EXIFDictionary), which uses the comma operator and ends up returning EXIFDictionary. So in essence you have
[NSString stringWithFormat:EXIFDictionary]
which is obviously wrong. This is also why you're getting a warning. That warning tells you that the format argument is not a string literal, because using variables as format strings is a common source of bugs. But more importantly here, that argument isn't even a string at all, and so it crashes.
Remove the parentheses and everything will be fine. That will look like
[NSString stringWithFormat:#"EXIF Properties: %#", EXIFDictionary];
I get warnings that the result is not a string literally
Nah. You get a warning saying that the format string of stringWithFormat: is not a string literal. That's because you don't know how the comma operator (and a variadic function) works (that's why one should master the C language before trying to make an iOS app). Basically what you have here:
[NSString stringWithFormat:(#"EXIF Properties: %#", EXIFDictionary)]
is, due the behavior of the comma operator, is equivalent to
[NSString stringWithFormat:EXIFDictionary]
which is obviously wrong. Omit the parentheses, and it will be fine:
[NSString stringWithFormat:#"EXIF Properties: %#", EXIFDictionary]
You don't want those parentheses:
NSString *EXIFString = [NSString stringWithFormat:#"EXIF Properties: %#", EXIFDictionary];
I'm trying to implement a job search App. The results are shown to the user in a UITableView.
When a user clicks on a cell, it should open the original Job announcement.
To do this, i implemented the following method:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
NSString *url = [[NSString alloc]init];
url=[[[xmlParser jobs] objectAtIndex:indexPath.row] urlAddress]; //UrlAddress is an instance variable of type NSString
NSURL *urlJobDetail = [NSURL URLWithString:(url)];
[[UIApplication sharedApplication] openURL: urlJobDetail];
}
The interesting part is: if i type an NSString like #"http://www.google.com" or any other link, it works. But when i try to open a "the urlJobDetail", it just doesn't work... Nothing happens at all...
And i searched it in stackoverflow.com and found this:
url = [url stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
Than the url works but this method changes the original url address and adds lots of % signs like:
"http://www.google.com%20 %20 %20"
So i get an page not found error.
I don't understand why this function doesn't accept a regular NSString variable as?
I checked it with NSLog and the url seems to be perfectly in order.
Any help would be much, very much appreciated !
Thanks in advance
Because it's URL specification related restrictions
Spaces and control characters in URLs must be escaped for transmission in HTTP, as must other disallowed characters... It is necessary to encode any characters disallowed in a URL, including spaces and other binary data not in the allowed character set, using the standard convention of the "%" character followed by two hexadecimal digits.
i'm trying to send the iOS device name along with the deviceToken to my server in didRegisterForRemoteNotificationsWithDeviceToken.. my device has an apostrophe in it and the request string shows as ....&name=John+Doe’s+iPhone ...
I've tried replacing "'" with "" and "’" with "" .. but neither fix it.. i assume i need to convert the encoding?
NSString *string = [[UIDevice currentDevice] name];
output-> John Doe’s iPhone
EDIT
output is from
NSLog(string);
You should never use NSLog with a user-provided string like that. NSLog expects a format string for its first argument. If it's something containing a format specifier, it will probably crash your application, and may expose a security hole if provided by an untrusted source.
Your encoding issue appears to be a bug in LLDB. Switching to GDB fixes the problem.
Wait, you're doing NSLog(string)? You need NSLog(#"%#", string)