2011-12-02 77 views

回答

15

不,這是不可能的,whatsapp沒有任何你可以使用的公共API。

請注意,當沒有Wha​​tsApp的API時,此答案對於2011是正確的。

現在有可與WhatsApp的交互的API:http://www.whatsapp.com/faq/en/iphone/23559013

Objective-C的調用打開其中一個網址如下:

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

現在任何API該附加whatsappLSApplicationQueriesSchemes,請幫幫我吧,我已經花費更多時間爲 – SampathKumar

+1

不,仍然沒有與iOS中的WhatsApp互動的API。 – rckoenes

+0

你可以使用url方案與whatsapp應用程序交談!請參閱@Wagner銷售答案。 – JHNeves

21

現在是可能的。還沒有嘗試過。

最新release notes for whatsapp表明您可以通過共享擴展:

的WhatsApp接受下列類型的內容:

  • 文本(UTI:public.plain文本)
  • 照片(UTI :public.image)
  • 視頻(UTI:public.movi​​e)
  • 音頻筆記和音樂文件(UTI:public.audio)
  • PDF文檔(UTI:com.adobe.pdf)
  • 聯繫卡(UTI:public.vcard)
  • 網頁的URL(UTI:public.url)
+2

它不可能只是打開應用程序,但不共享圖像等:) :) – Rushabh

+0

上面的鏈接是好的。這現在是可能的。這應該成爲最好的答案。 –

+1

上面的鏈接不允許在不顯示菜單的情況下將媒體共享到WhatsApp。在使用WhatsApp之前,您必須始終顯示文檔控制器的菜單,這在屁股上很痛苦。 – user3344977

112

現在可以以這種方式:

發送文本 - 對象 -

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 
} 

發送文本 - 斯威夫特

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]; 
} 

發送圖像 - 斯威夫特

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 
     } 
    } 
} 

由於iOS版9的新的安全功能,則需要加上 的.plist文件這行:

<key>LSApplicationQueriesSchemes</key> 
<array> 
    <string>whatsapp</string> 
</array> 

更多形成關於網址sheme:https://developer.apple.com/videos/play/wwdc2015-703/

我沒有找到一個單一的解決方案。 關於http://www.whatsapp.com/faq/en/iphone/23559013的更多信息

我做了一個小項目來幫助一些人。 https://github.com/salesawagner/SharingWhatsApp

+0

是否有可能與android應用程序集成whatsapp – BeingMIAkashs

+0

可以通過whatsapp將文本發送到特定的號碼?意味着不需要從什麼應用程序聯繫人列表中選擇聯繫人? –

+0

您需要將「ID地址簿」與聯繫人關聯,只需添加參數「abid = 」即可。我不知道直接發送到電話號碼,但我正在尋找解決方案。 鏈接有一個更好的解釋。 http://www.whatsapp.com/faq/en/iphone/23559013 –

3

的WhatsApp提供了兩種方式爲您的iPhone應用程序與WhatsApp的互動:

  • 通過自定義URL方案
  • 通過iOS的文件交流的API

欲瞭解更多信息,請訪問this link

謝謝。

1

是的,它是可能的:

NSMutableArray *arr = [[NSMutableArray alloc]init]; 
    NSURL *URL = [NSURL fileURLWithPath:path]; 
    NSString *textToShare = [NSString stringWithFormat:@"%@ \n",_model.title]; 
    NSString *SchoolName= [[AppUtility sharedUtilityInstance]getAppConfigInfoByKey:@"SchoolName" SecondKeyorNil:Nil]; 
    [arr addObject:textToShare]; 
    [arr addObject:URL]; 
    [arr addObject:_model.body]; 
    [arr addObject:SchoolName]; 
    TTOpenInAppActivity *openInAppActivity = [[TTOpenInAppActivity alloc] initWithView:_parentController.view andRect:((UIButton *)sender).frame]; 

    UIActivityViewController *activityViewController = [[UIActivityViewController alloc] initWithActivityItems:arr applicationActivities:@[openInAppActivity]]; 

    // Store reference to superview (UIActionSheet) to allow dismissal 
    openInAppActivity.superViewController = activityViewController; 
    // Show UIActivityViewController 
    [_parentController presentViewController:activityViewController animated:YES completion:NULL]; 
6

這是共享鏈接正確的代碼,以什麼應用程序的用戶。

NSString * url = [NSString stringWithFormat:@"http://video...bla..bla.."]; 
url = (NSString*)CFBridgingRelease(CFURLCreateStringByAddingPercentEscapes(NULL,(CFStringRef) url, NULL,CFSTR("!*'();:@&=+$,/?%#[]"),kCFStringEncodingUTF8)); 

    NSString * urlWhats = [NSString stringWithFormat:@"whatsapp://send?text=%@",url]; 
NSURL * whatsappURL = [NSURL URLWithString:urlWhats]; 
if ([[UIApplication sharedApplication] canOpenURL: whatsappURL]) { 
    [[UIApplication sharedApplication] openURL: whatsappURL]; 
} else { 
    // can not share with whats app 
} 
+0

它的工作! –

2

斯威夫特3版瓦格納銷售的回答:

let urlWhats = "whatsapp://app" 
if let urlString = urlWhats.addingPercentEncoding(withAllowedCharacters: CharacterSet.urlQueryAllowed) { 
    if let whatsappURL = URL(string: urlString) { 
    if UIApplication.shared.canOpenURL(whatsappURL) { 

     if let image = UIImage(named: "image") { 
     if let imageData = UIImageJPEGRepresentation(image, 1.0) { 
      let tempFile = NSURL(fileURLWithPath: NSHomeDirectory()).appendingPathComponent("Documents/whatsAppTmp.wai") 
      do { 
      try imageData.write(to: tempFile!, options: .atomic) 

      self.documentIC = UIDocumentInteractionController(url: tempFile!) 
      self.documentIC.uti = "net.whatsapp.image" 
      self.documentIC.presentOpenInMenu(from: CGRect.zero, in: self.view, animated: true) 
      } 
      catch { 
      print(error) 
      } 
     } 
     } 

    } else { 
     // Cannot open whatsapp 
    } 
    } 
} 
0
NSString *shareText = @"http:www.google.com"; 
NSArray *objectsToShare = @[shareText]; 

UIActivityViewController *activityVC = [[UIActivityViewController alloc] initWithActivityItems:objectsToShare applicationActivities:nil]; 

if (isIphone) 
{ 
    [self presentViewController:activityVC animated:YES completion:nil]; 
} 
else { 
    UIPopoverController *popup = [[UIPopoverController alloc]   initWithContentViewController:activityVC]; 
    [popup presentPopoverFromRect:CGRectMake(self.view.frame.size.width/2, self.view.frame.size.height/4, 0, 0)inView:self.view permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES]; 
} 
+2

你需要添加一些解釋。你爲什麼認爲這個代碼有效?與以前的答案有什麼不同? –

2

簡單的代碼和示例代碼;-)

注: - 您只可以共享文本或圖像,兩者共享在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]; 
1

斯威夫特3版本發送文字:

func shareByWhatsapp(msg:String){ 
     let urlWhats = "whatsapp://send?text=\(msg)" 
     if let urlString = urlWhats.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed) { 
      if let whatsappURL = NSURL(string: urlString) { 
       if UIApplication.shared.canOpenURL(whatsappURL as URL) { 
        UIApplication.shared.openURL(whatsappURL as URL) 
       } else { 

        let alert = UIAlertController(title: NSLocalizedString("Whatsapp not found", comment: "Error message"), 
                message: NSLocalizedString("Could not found a installed app 'Whatsapp' to proceed with sharing.", comment: "Error description"), 
                preferredStyle: UIAlertControllerStyle.alert) 

        alert.addAction(UIAlertAction(title: NSLocalizedString("Ok", comment: "Alert button"), style: UIAlertActionStyle.default, handler:{ (UIAlertAction)in 
        })) 

        self.present(alert, animated: true, completion:nil) 
        // Cannot open whatsapp 
       } 
      } 
     } 
} 

另外,你需要在你的Info.plist

相關問題