Recently, I received a new requirement, that is, I need to jump from the company project to the conversation interface of the designated contact in the third-party communication App. When you jump to a third-party APP, you can use your mobile phone number to find the corresponding contact and then automatically reach the chat interface of the contact for communication. As for the third-party App, the company gave two choices, one is wechat and the other is WhatsApp. Say wechat SDK often contact, WhatsApp is not how to do, so I decided to start from wechat. According to the development document of wechat, only the sharing function meets this requirement, but the document only provides the following three sharing scenarios:
Share to Conversation: WXSceneSession Share to Moments: WXSceneTimeline Share to Favorites: WXSceneFavorite
It is not supported to share to the designated contact. I used the first scenario to share to the conversation to do the test, and I had to manually select the contact to jump to the conversation interface, so this does not meet the requirements, so wechat Pass.
Take a look at WhatsApp. The official documentation of WhatsApp is very simple, and there is no need to integrate SDK, which is very convenient. Fortunately, it provides a Custom URL Scheme for jumping to a conversation with a specific contact.
Official: WhatsApp’s click-to-chat feature lets users talk to people without having to store their phone numbers in their phone contacts. As long as you know their phone number, you can set up a link that allows you to talk to them. Clicking on the link opens the conversation window directly. WhatsApp’s mobile app and web versions support click-to-chat. To create a link, use wa.me/, where fill in the contact’s full international format phone number and omit any zeros, parentheses, or dashes. For more complete information on international format phone numbers, read this article. Please note that the phone number must be tied to a valid WhatsApp account. Correct: wa.me/15551234567 Incorrect: wa.me/+001-(555)1… >
1. In short, we only need to create a link with wa.me/ + user’s mobile phone number to jump to meet our requirements. Without further ado, go directly to the code:
NSString *whatsappPhoneMumber = @"133XXXXXXXX”;
NSString *url = [NSString stringWithFormat:@"https://wa.me/86%@",whatsappPhoneMumber]; NSURL *whatsappURL = [NSURL URLWithString: @"whatsapp://"]; NSURL *ChatsURL = [NSURL URLWithString: url]; If ([[UIApplication sharedApplication] canOpenURL: whatsappURL]) { [[UIApplication sharedApplication] openURL: ChatsURL]; } else { // Cannot open whatsapp NSLog(@"Can't open WhatsApp"); }Copy the code
Note: before running the code we need to add the list of allowed access to WhatsApp in the info.plist file, otherwise we cannot jump.
Or open info.plist as Source Code and add the following Code:
<key>LSApplicationQueriesSchemes</key>
<array>
<string>whatsapp</string>
</array>
Copy the code
Code execution effect:
2. If you want to attach the above text after the jump, it is also very easy to splice the following text
Format: wa.me/ Mobile number /? Text =…
NSString *urlencodedtext = @"Pre-fill content"
NSString *whatsappphonenumber = @"133XXXXXXXX";
NSString *url = [NSString stringWithFormat:@"https://wa.me/86%@/? text=%@",whatsappphonenumber,urlencodedtext];
Copy the code
Reference links:
www.jianshu.com/p/d66a8aef8…
Faq.whatsapp.com/zh_cn/iphon…