New Line in WhatsApp vs Mail

我的未来我决定 提交于 2019-12-23 12:32:35

问题


When sharing text from an app on IOS 9.2 you can choose from various messagin options. The problem is that most options like mail or sms expect \n to be the line break while WhatsApp expects <BR> to be the line break.

I'm told that there is no way to know in the app what the user will choose so I'm sending \n<BR>. While is works well for WhatsApp that ignores the \n it does not work well for Mail that shows the <BR>.

Also tried %0A%0D but WhatsAPP ignores.


回答1:


EDIT

As of 2017-07-19, WhatsApp for iOS no longer interprets <br> as newline, and instead switched to \n.

This isn't a backwards compatible change, so if you use <br> you will literally get Some<br>Text. The code below should no longer be used; the good news is that you don't need to do nothing: WhatsApp will handle \n like it always should have.


DEPRECATED

I used my own UIActivityItemProvider which, depending on the chosen activity, uses \n or <br>:

@interface ShareManager : UIActivityItemProvider <UIActivityItemSource>
@end

@implementation ShareManager

- (id)activityViewController:(UIActivityViewController *)activityViewController itemForActivityType:(NSString *)activityType
    id result = [self getShareText];

    if ([activityType containsIgnoringCase:@"WhatsApp"]) // You can also match against the exact id "net.whatsapp.WhatsApp.ShareExtension"
    {
        result = [result stringByReplacingOccurrencesOfString:@"\n" withString:@"<br>"];
    }

    return result;
}

@end

Usage:

UIActivityViewController *activity = [[UIActivityViewController alloc] initWithActivityItems:@[[[ShareManager alloc] init]]];
[self presentViewController:activity animated:YES completion:nil];


来源:https://stackoverflow.com/questions/34695055/new-line-in-whatsapp-vs-mail

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!