2016-01-07 27 views
0

我使用此代码在iOS应用程序的WhatsApp上共享图像,但控制器总是处于else的状态。请给我一个解决方案。如何从我的iOS应用程序发送图像或文本到whatsapp?

if ([[UIApplication sharedApplication] canOpenURL: [NSURL URLWithString:@"whatsapp://app"]]) 
{ 
    UIImage *iconImage = [UIImage imageNamed:dict23[@"profileimg"]]; 
    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]; 
} 

回答

4

使用此代码对什么应用程序共享只有文字

NSString * msg = @"Hi! I am using app! download it at https://itunes.apple.com/us/app/google-search/id284815942?mt=8"; 

    msg = [msg stringByReplacingOccurrencesOfString:@" " withString:@"%20"]; 
    msg = [msg stringByReplacingOccurrencesOfString:@":" withString:@"%3A"]; 
    msg = [msg stringByReplacingOccurrencesOfString:@"/" withString:@"%2F"]; 
    msg = [msg stringByReplacingOccurrencesOfString:@"?" withString:@"%3F"]; 
    msg = [msg stringByReplacingOccurrencesOfString:@"," withString:@"%2C"]; 
    msg = [msg stringByReplacingOccurrencesOfString:@"=" withString:@"%3D"]; 
    msg = [msg stringByReplacingOccurrencesOfString:@"&" withString:@"%26"]; 

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

请参阅此链接的分享图像 WhatsApp image sharing iOS

+0

我使用此代码,但始终给“您的设备没有安装WhatsApp。”消息... –

+0

然后你在模拟器中运行它,你必须在装有whats app的设备上运行它。 –

0

检查此链接,并按照它提到的步骤:https://ioscoderhub.wordpress.com/2014/05/01/how-can-i-share-image-from-iphone-application-to-whatsapp-line-wechat-programmatically/

下面是从上面的链接的简短代码...

in ViewControl ler.h文件:

@interface ViewController : UIViewController 
{ 
} 

@property(nonatomic,retain) UIDocumentInteractionController *documentationInteractionController; 

在ViewController.m文件:

- (IBAction)bocClick:(UIButton *)sender { 

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,  NSUserDomainMask, YES); 
    NSString *documentsDirectory = [paths objectAtIndex:0]; 

    NSString *getImagePath = [documentsDirectory stringByAppendingPathComponent:@"savedImage.png"]; //here i am fetched image path from document directory and convert it in to URL and use bellow 

    NSURL *imageFileURL =[NSURL fileURLWithPath:getImagePath]; 
    NSLog(@"imag %@",imageFileURL); 

    self.documentationInteractionController.delegate = self; 
    self.documentationInteractionController.UTI = @"net.whatsapp.image"; 
    self.documentationInteractionController = [self setupControllerWithURL:imageFileURL usingDelegate:self]; 
    [self.documentationInteractionController presentOpenInMenuFromRect:CGRectZero inView:self.view animated:YES]; 
} 

- (UIDocumentInteractionController *) setupControllerWithURL: (NSURL*) fileURL 
               usingDelegate: (id) interactionDelegate { 

    self.documentationInteractionController = [UIDocumentInteractionController interactionControllerWithURL: fileURL]; 

    self.documentationInteractionController.delegate = interactionDelegate; 

    return self.documentationInteractionController; 
} 
0

下面给出你可以用WhatsApp的分享文字:

NSString *msgStr = [NSString stringWithFormat:@"whatsapp://send?text=%@ ,msgString]; 
     NSURL * whatsappURL = [NSURL URLWithString:[msgStr 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]; 
     } 
+0

使用此代码我发现输出是“WhatsApp未安装”,请告诉我什么做错了 –

+0

WhatsApp应安装在iPHONE中。这在模拟器中不起作用 –

+0

是的,我在ios设备上测试了这个代码,并且这个设备安装了whatsApp。 –

0
NSString * urlWhats = [NSString stringWithFormat:@"whatsapp://send?text=%@",@"YOUR STRING"]; 
    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 WhatsApp installed." delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil]; 
     [alert show]; 
    } 

对于详细知识PLZ参考以下链接

https://www.whatsapp.com/faq/en/iphone/23559013

+0

控制器总是进入其他部分 –

+0

WhatsApp应该安装在iPHONE中。这在模拟器中不起作用 –

相关问题