Whatsapp共享文本和图像在ios中不起作用

时间:2014-11-24 12:39:57

标签: ios objective-c whatsapp

如果向WhatsApp发送文字:

NSString *msg = @"Some Text";
NSString *urlWhats = [NSString stringWithFormat:@"whatsapp://send?text=%@",msg];
NSURL *whatsappURL = [NSURL URLWithString:[urlWhats stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
if ([[UIApplication sharedApplication] canOpenURL: whatsappURL]) {
  [[UIApplication sharedApplication] openURL: whatsappURL];
} else {
  UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@"WhatsApp not installed." message:@"Your device has no WhatsApp installed." delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
  [alert show];
}

如果将图像发送到WhatsApp:

if ([[UIApplication sharedApplication] canOpenURL: [NSURL URLWithString:@"whatsapp://app"]]){

   UIImage* iconImage = [self captureScreen];// this will return a image
   NSString* savePath  = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/whatsAppTmp.wai"];

   [UIImageJPEGRepresentation(iconImage, 1.0) writeToFile:savePath atomically:YES];

   _documentInteractionController = [UIDocumentInteractionController interactionControllerWithURL:[NSURL fileURLWithPath:savePath]];
   _documentInteractionController.UTI = @"net.whatsapp.image";
   _documentInteractionController.delegate = self;

   [_documentInteractionController presentOpenInMenuFromRect:CGRectMake(0, 0, 0, 0) inView:self.view animated: YES];
} else {
   UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@"WhatsApp not installed." message:@"Your device has no WhatsApp installed." delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
   [alert show];
}

打开Whatsapp没有任何文本共享文件,图像也没有被共享。请帮我。

2 个答案:

答案 0 :(得分:2)

你添加了" LSApplicationQueriesSchemes"你是谁?

  1. 转到您的项目/支持文件/ YourProjectName-Info.plist XCode项目资源管理器。
  2. 添加新的键值对。关键: LSApplicationQueriesSchemes类型:Array添加新的字符串项 " WhatsApp的"
  3. 请尝试这个并告诉我它是否仍然无效。

答案 1 :(得分:0)

注意: - 您只能共享文本或图片,两者在whatsApp中共享无法在whatsApp端工作

 /*
    //Share text
    NSString *textToShare = @"Enter your text to be shared";
    NSArray *objectsToShare = @[textToShare];

    UIActivityViewController *activityVC = [[UIActivityViewController alloc] initWithActivityItems:objectsToShare applicationActivities:nil];
    [self presentViewController:activityVC animated:YES completion:nil];
     */

    //Share Image
    UIImage * image = [UIImage imageNamed:@"images"];
    NSArray *objectsToShare = @[image];

    UIActivityViewController *activityVC = [[UIActivityViewController alloc] initWithActivityItems:objectsToShare applicationActivities:nil];
    [self presentViewController:activityVC animated:YES completion:nil];