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)
Related
I have what should be a dead-simple piece of code that is failing for me in strange ways.
void MediaShare(char* text, char* furl) {
NSString *status = [NSString stringWithUTF8String: text];
NSString *media = [NSString stringWithUTF8String: furl];
[[SocialShare sharedInstance] mediaShare:status media:media];
text is just a line of text for Twitter sharing, and furl is just a file location string. I am getting a crash down past this function that comes down to bad data getting passed. Putting a breakpoint at the head of this function yields the following-
Image of Xcode variable monitor
The two values look fine, although not sure if the * values that only contain the first char are a problem.
Anyway, jumping to the end, and status and media appear to be converted to hex values.
Converted to hex values?
Any ideas? To give the full story, this is a Unity plug-in. I am simply passing in two strings from Unity C# to this function.
Thanks so much.
The code looks fine so far, if the input values are well formed C char* strings that can be interpreted as an UTF-8 encoded string.
status and media being hex values at the end of the function hint that they are. Both are pointers to Objective-C objects, so this is expected. Print them to the debug console or use po status at the debug console to check their contents (it will print the result of the -description method of status which is the string content in that case).
The subsequent crash might be caused elsewhere in the code.
What's the crash's log output?
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'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.
I've got a very strange issue. I started an iOS App about three years ago (iOS-SDK 3.0), and since then went through the SDKs 4.0 and 5.0. Since 5.0 (or maybe 5.1) I suddenly started having problems with German special chars (ä ö ü ß).
Now I can't even initialize an NSString with special chars, this line:
NSString *str = #"abcäxyz";
gives the following warning:
Input conversion stopped due to an input byte that does not belong to the input codeset UTF-8
And this one:
NSLog(#"%#", strTemp);
gives:
abc
So it's stopping at the first special char. In other projects everything is fine. I can work with special chars without any problems.
Is it a configuration problem?
EDIT: Obviously it is a problem with the file encoding.
file -I myFile
is giving:
text/x-c++; charset=unknown-8bit
Trying to convert it with iconv gives me:
conversion from unknown-8bit unsupported
What happens when you use the UTF-8 codes to initialize the string? Like so:
NSString *s = [NSString stringWithFormat:#"%C", 0xc39f]; // should be ß
As far as I know you should also be able to do this, but haven't tested it:
NSString *s = [NSString stringWithUTF8String:"0xc39f"];
Try those and see what happens. There's a number of sites around that keep UTF-8 code tables for special characters, e.g. this one.
As long as your file is encoded UTF-8, #"abcäxyz" should be fine, but the explicit form of embedding a literal unicode characters is \u????.
- (void)testGermanChar
{
NSString *expected = #"abc\u00E4xyz";
NSString *actual = #"abcäxyz";
STAssertEqualObjects(expected, actual, #"the two strings should be equivalent");
}
SOLVED: Changed the file encoding in Xcode:
Click on the file you want to change the encoding of, then open the right panel (whats the name of this pane actually? any idea?) to edit the properties. There you see "Text Encoding" under "Text Settings". That is all.
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);