iPhone 上的 Base64 编码,在模拟器中工作,在设备上中断

发布于 2024-08-01 23:47:19 字数 1983 浏览 9 评论 0原文

我正在使用简单的 SMTP 客户端代码,首先发布在 google 代码上:

http://code.google .com/p/skpsmtpmessage/

该存储库似乎已经无人维护,因为项目所有者现在受雇于苹果,也许有更好的事情要做。 该代码有一个分支,最近在 github 上维护,您可以在以下位置找到它:

http://github.com/kailoa/iphone-smtp/tree/master

我无法让附件在 iPhone 设备上工作,奇怪的是它们在模拟器中工作。 我的github评论可以在这里找到:

http://github.com/kailoa/ iphone-smtp/commit/50cbd49f351c2f0bb3a5ad6aea7736ac82d40af2#comment_27560

将所有这些作为上下文...我的问题是:是否有人能够成功地将文件(最好是音频文件,但现在任何文件都可以)附加到电子邮件,通过 iPhone 上的 SMTP 客户端发送? 我真的希望能够将文件附加到使用直接 SMTP 客户端发送的电子邮件中,但到目前为止,我还没有找到任何可以正确运行的代码,并且我还无法成功编写自己的代码来完成说壮举。

是否有任何原因导致文件在 iPhone 上的编码与模拟器中的编码不同? 因为当我查看在两个上下文中创建的原始 base64 字符串时,它们是不同的。 我只是不明白为什么会这样。

我的代码的相关部分是这样的:

NSString* emailBody = @"This is a test email body";

NSDictionary *plainPart = [NSDictionary dictionaryWithObjectsAndKeys:@"text/plain",kSKPSMTPPartContentTypeKey, emailBody,kSKPSMTPPartMessageKey,@"8bit",kSKPSMTPPartContentTransferEncodingKey,nil];

NSString *imgPath = [[NSBundle mainBundle] pathForResource:@"screenshot" ofType:@"png"];

NSData *imgData = [NSData dataWithContentsOfFile:imgPath];

NSDictionary *imgPart = [NSDictionary dictionaryWithObjectsAndKeys:@"image/png;\r\n\tx-unix-mode=0644;\r\n\tname=\"screenshot.png\"", kSKPSMTPPartContentTypeKey, @"attachment;\r\n\tfilename=\"screenshot.png\"", kSKPSMTPPartContentDispositionKey, [imgData encodeBase64ForData],kSKPSMTPPartMessageKey, @"base64",kSKPSMTPPartContentTransferEncodingKey,nil];

testMsg.parts = [NSArray arrayWithObjects:plainPart,imgPart,nil];

如果需要更多代码或参考来解决这个问题,我很乐意继续添加。 但所有代码都可以在 github 上找到,因此任何对此感兴趣的人都可以很容易地看到所有内容。

谢谢 瑞安

I am using the simple SMTP client code, first posted on google code at:

http://code.google.com/p/skpsmtpmessage/

That repo seems to have gone unmaintained, as the project owner is now employed at apple, and maybe has better things to do. There is a fork of the code, that is more recently maintained at github, which you can find at:

http://github.com/kailoa/iphone-smtp/tree/master

I am having trouble getting attachments to work on the iPhone device, thought oddly they work in the simulator. My github comment can be found here:

http://github.com/kailoa/iphone-smtp/commit/50cbd49f351c2f0bb3a5ad6aea7736ac82d40af2#comment_27560

With all that as context...my question is: has anyone been able to successfully attach a file (preferably an audio file, but any file will do for now) to an email, sent with a SMTP client on the iPhone? I really want to be able to attach files to emails that I send with a direct SMTP client, but so far I have yet to find any code that correctly functions and works, and I have not been able to successfully write my own code to accomplish said feat.

Is there any reason why files would encode differently on the iPhone than in the simulator? Because when I look at the raw base64 string that is created in both contexts, they are different. I'm just lost as to why that would be the case.

The relevant portion of my code is this:

NSString* emailBody = @"This is a test email body";

NSDictionary *plainPart = [NSDictionary dictionaryWithObjectsAndKeys:@"text/plain",kSKPSMTPPartContentTypeKey, emailBody,kSKPSMTPPartMessageKey,@"8bit",kSKPSMTPPartContentTransferEncodingKey,nil];

NSString *imgPath = [[NSBundle mainBundle] pathForResource:@"screenshot" ofType:@"png"];

NSData *imgData = [NSData dataWithContentsOfFile:imgPath];

NSDictionary *imgPart = [NSDictionary dictionaryWithObjectsAndKeys:@"image/png;\r\n\tx-unix-mode=0644;\r\n\tname=\"screenshot.png\"", kSKPSMTPPartContentTypeKey, @"attachment;\r\n\tfilename=\"screenshot.png\"", kSKPSMTPPartContentDispositionKey, [imgData encodeBase64ForData],kSKPSMTPPartMessageKey, @"base64",kSKPSMTPPartContentTransferEncodingKey,nil];

testMsg.parts = [NSArray arrayWithObjects:plainPart,imgPart,nil];

If more code or references are necessary to figure this out, I'm happy to keep adding. But all the code is readily available on github, so anyone who is interested in this can see everything very easily.

Thanks
Ryan

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

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

发布评论

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

评论(2

请别遗忘我 2024-08-08 23:47:19

可能您需要使用 [imgDataencodeWrappedBase64ForData] 而不是 [imgDataencodeBase64ForData],这就是这个人的问题 这里正在拥有。

it might be that you need to use [imgData encodeWrappedBase64ForData] instead of [imgData encodeBase64ForData], that's the problem the guy here was having.

忆离笙 2024-08-08 23:47:19

您是否检查过字节序

代码运行在两种不同的架构上; 一般来说,当您切换机器时,一些事情可能会发生变化:

  • 某些类型的大小,例如 CGFloat 或指针
  • 单词内的默认编码字节顺序(字节序)
  • 区域设置敏感的数据
  • 存储的首选项data
  • 代码中其他定义的常量。

可能还有更多。

如何更详细地调试这个? 两个想法:

  1. 这真的是一个错误吗? 如果它按预期在设备上运行,是否值得您花时间在模拟器上“修复”?
  2. 如果是,那么您可以找到以不同方式编码的第一个字节(diff 可以提供帮助),并在您有权访问的设置字节的最低级别代码处设置断点。 我假设您可以访问此功能,因为我在 SDK 中不知道它。 一旦在两台机器上都有堆栈跟踪,您就可以向后工作,检查重要帧中的关键变量值,并找出差异的根源。

Have you checked for endianness?

The code is running on two different architectures; in general, when you switch machines, a few things could be changing:

  • the size of certain types, like CGFloats or pointers
  • the default encoding byte order within words (endianness)
  • locale-sensitive data
  • stored-preference data
  • other defined constants within the code.

and probably a few more.

How to debug this in more detail? Two ideas:

  1. Is it really a bug? If it works on the device as intended, is it worth your time to "fix" on the simulator?
  2. If yes, then you could find the first byte that's being encoded differently (diff can help), and set a breakpoint at the lowest-level code you have access to which sets bytes. I'm assuming you have some access to this functionality since I'm unaware of it in the SDK. Once you have that stacktrace on both machines, you can work backward, checking key variable values in the significant frames, and figure out where the difference originated.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文