2016-04-05 4 views

ответ

2

Нет, это невозможно, у whatsapp нет публичного API, который вы можете использовать.

Обратите внимание, что этот ответ верен для 2011 года, когда API для WhatsApp отсутствовал.

Теперь есть апи для взаимодействия с WhatsApp: http://www.whatsapp.com/faq/en/iphone/23559013

, но если и хотят, чем попробовать это теперь можно таким образом:

Отправить текст - Obj-C

NSString * msg = @"YOUR MSG"; 
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 { 
    // Cannot open whatsapp 
} 

Отправить Text - Swift

let msg = "YOUR MSG" 
let urlWhats = "whatsapp://send?text=\(msg)" 
if let urlString = urlWhats.stringByAddingPercentEncodingWithAllowedCharacters(NSCharacterSet.URLQueryAllowedCharacterSet()) { 
    if let whatsappURL = NSURL(string: urlString) { 
     if UIApplication.sharedApplication().canOpenURL(whatsappURL) { 
      UIApplication.sharedApplication().openURL(whatsappURL) 
     } else { 
      // Cannot open whatsapp 
     } 
    } 
} 

изображение - Obj-C

- в файле .h

<UIDocumentInteractionControllerDelegate> 

@property (retain) UIDocumentInteractionController * documentInteractionController; 

- в .m файл

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

    UIImage  * iconImage = [UIImage imageNamed:@"YOUR 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]; 
} 
Send Image - Swift 

let urlWhats = "whatsapp://app" 
if let urlString = urlWhats.stringByAddingPercentEncodingWithAllowedCharacters(NSCharacterSet.URLQueryAllowedCharacterSet()) { 
    if let whatsappURL = NSURL(string: urlString) { 

     if UIApplication.sharedApplication().canOpenURL(whatsappURL) { 

      if let image = UIImage(named: "image") { 
       if let imageData = UIImageJPEGRepresentation(image, 1.0) { 
        let tempFile = NSURL(fileURLWithPath: NSHomeDirectory()).URLByAppendingPathComponent("Documents/whatsAppTmp.wai") 
        do { 
         try imageData.writeToURL(tempFile, options: .DataWritingAtomic) 
         self.documentInteractionController = UIDocumentInteractionController(URL: tempFile) 
         self.documentInteractionController.UTI = "net.whatsapp.image" 
         self.documentInteractionController.presentOpenInMenuFromRect(CGRectZero, inView: self.view, animated: true) 
        } catch { 
         print(error) 
        } 
       } 
      } 

     } else { 
      // Cannot open whatsapp 
     } 
    } 
} 
Because a new security feature of iOS 9, you need add this lines on .plist file: 

<key>LSApplicationQueriesSchemes</key> 
<array> 


    <string>whatsapp</string> 
</array> 
More information about url sheme: https://developer.apple.com/videos/play/wwdc2015-703/ 
I did not find a single solution for both. More information on http://www.whatsapp.com/faq/en/iphone/23559013 

I made a small project to help some. https://github.com/salesawagner/SharingWhatsApp 
+0

мы можем разделить изображение и текст оба вместе в WhatsApp? –

+0

Большое вам спасибо. – Karthickkck

Смежные вопросы