iPhone Development: Post text or image to Whats-app in IOS


Whats-app, a common social platform gaining popularity very rapidly and now its time to let the app user’s share contents from native apps to their whats-app friends. In our IOS app development we can share content with whats-app friends using two techniques provided by whats-app.

1. Through cstom URL scheme.
2. Through UIDocumentInteractionController.

If you want to allow your app user to send text to a specific whats-app friend you can use custom URL scheme method.The custom URL provided by whats-app to open their app is whatsapp:// , you can send two parameter’s in this abid and send 

abid = address book id of contact with which you want to start chat (if abid is not passed, then whats – app is opened with whats-app contact list)
send = initial text you want to send to specific contact

Below is the code example that shows how we can use it in our app

NSURL *whatsappURL = [NSURL URLWithString:@"whatsapp://send?text=Hello World!"];
if ([[UIApplication sharedApplication] canOpenURL: whatsappURL]) {
[[UIApplication sharedApplication] openURL: whatsappURL];
}

Above code, first we create an url with whats-app url scheme and text we want to show on message box. Next we check if user has whats-app installed on his/her device. If its installed we will open whats-app url using openURL method of UIApplication.

Secondly, we can use UIDocumentationController to send image, audio, video to whats-app. Below is the code we use for sending image to whats-app




UIImage *whtsappImage = tempImageView.image; 


NSString* imagePath = [NSString stringWithFormat:@"%@/image.wai",[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject]];


 [[NSFileManager defaultManager] removeItemAtPath:imagePath error:nil];


 [UIImagePNGRepresentation(whtsappImage) writeToFile:imagePath atomically:YES];


 NSLog(@"image size: %@", NSStringFromCGSize(whtsappImage.size));


 UIDocumentInteractionController *_docController = [UIDocumentInteractionController interactionControllerWithURL:[NSURL fileURLWithPath:imagePath]];


 [_docController presentOpenInMenuFromRect:self.view.frame inView:self.view animated:YES]; 



In above code, we saved our image as .wai extension to our document directory and then add url or file path of saved image to UIDocumentController instance and present our UIDocumentController instance.

NOTE:- For more information check this thread https://www.whatsapp.com/faq/iphone/23559013