New line in WhatsApp vs Mail

When sharing text from an application on iOS 9.2, you can choose from various messaging options. The problem is that most parameters, such as mail or SMS, expect it to \nbe a line break, and WhatsApp expects it to <BR>be a line break.

I was told that there is no way in the application to find out what the user will choose, so I submit \n<BR>. While WhatsApp, which ignores \n, works well, it does not work well for Mail, which shows <BR>.

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

+4
source share
1 answer

EDIT

Starting from 2017-07-19, WhatsApp for iOS no longer interprets <br>as a new line and instead switches to \n.

This is not a backward compatible change, so if you use <br>, you will literally get it Some<br>Text. The following code should no longer be used; the good news is that you don’t have to do anything: WhatsApp will handle \nit as it should always be.


RECOMMENDED

I used my own UIActivityItemProvider, which, depending on the action selected, uses \neither <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

Using:

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

Source: https://habr.com/ru/post/1623563/


All Articles