设备名称中的波形符导致 NSOutputStream 套接字出现问题

发布于 2024-08-26 12:08:55 字数 1079 浏览 5 评论 0原文

在我们的应用程序的 iPhone 和桌面版本之间的网络中,iPhone 发送设备名称以在桌面上使用。问题是一些 Beta 测试人员的设备名称中包含波浪号 (`)。由于某种原因,当它出现在设备名称中时,它会阻止套接字发送实际的字符串数据。

我尝试在发送设备名称之前简单地清理一下设备名称,但设备名称中的波形符(在 iTunes 中输入)在运行时不会被识别为波形符。这是不起作用的代码:

NSString *safedevicename = [[UIDevice currentDevice] name];
safedevicename = [safedevicename stringByReplacingOccurrencesOfString:@"`" withString:@"'"];

它没有发现波浪号的出现,并且不替换任何内容。我还使用 rangeOfString 搜索波浪号,但它什么也没返回。我 100% 确定该字符(至少在 iTunes 中输入时如此)是波浪号。

此外,当将字符串的描述打印到控制台时,该字符被编码为 \u00b4,当将鼠标悬停在变量上时,它会显示为句点 .

有人知道我如何抓住这个角色并将其从那里取出吗?另外,目标 C 中是否有一种方法可以更轻松地清理字符串以确保通过套接字发送是安全的?

编辑: 还有一些可能有用的东西,将 NSString 写入 NSOutputString 我使用以下代码行:

len = [oStream write:[[writeString dataUsingEncoding:NSASCIIStringEncoding] bytes] maxLength:[writeString lengthOfBytesUsingEncoding:NSASCIIStringEncoding]];

编辑#2: 这行代码可以替换波浪号,但我确信还有其他字符我应该担心:

safedevicename = [safedevicename stringByReplacingOccurrencesOfString:@"\\u00b4" withString:@"'"];

In the networking between the iPhone and desktop versions of our application, the iPhone sends over the device name for use on the desktop. The problem is that some of the beta testers have tildes (`) in their device names. For some reason when this is in the device name it prevent the socket from sending the actual string data.

I've tried simply cleaning up the device name before sending it, but the tilde in the device name (as entered in iTunes) is not recognized at runtime as a tilde. Here's the code that doesn't work:

NSString *safedevicename = [[UIDevice currentDevice] name];
safedevicename = [safedevicename stringByReplacingOccurrencesOfString:@"`" withString:@"'"];

It finds no occurrences of a tilde, and replaces nothing. I've also used rangeOfString to search for tildes and it returns nothing. I'm 100% sure the character, at least as it's entered in iTunes, is a tilde.

Also, when printing a description of the string to the console, the character is encoded as \u00b4, and when hovering over the variable it appears as a period ..

Anyone know how I can grab this character and get it out of there? Also, isn't there a way in objective C to more easily clean up the string to make sure it's safe to send over a socket?

EDIT:
Also something that might be useful, to write the NSString to the NSOutputString I use the following line of code:

len = [oStream write:[[writeString dataUsingEncoding:NSASCIIStringEncoding] bytes] maxLength:[writeString lengthOfBytesUsingEncoding:NSASCIIStringEncoding]];

EDIT #2:
This line of code works to replace the Tilde, but I'm sure there are other characters I should be worrying about:

safedevicename = [safedevicename stringByReplacingOccurrencesOfString:@"\\u00b4" withString:@"'"];

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

洒一地阳光 2024-09-02 12:08:55

Jason 的评论是正确的答案:我需要将编码从 NSASCIIStringEncoding 更改为 NSUTF8StringEncoding

Jason's comment was the correct answer: I needed to change the encoding from NSASCIIStringEncoding to NSUTF8StringEncoding.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文