如何从ios应用程序向whatsapp发送图像或文本?

如何从ios应用程序向whatsapp发送图像或文本?,ios,objective-c,iphone,ios7,Ios,Objective C,Iphone,Ios7,我使用此代码从iOS应用程序在Whatsapp上共享图像,但控制器始终处于else状态。请给我一个解决办法 if ([[UIApplication sharedApplication] canOpenURL: [NSURL URLWithString:@"whatsapp://app"]]) { UIImage *iconImage = [UIImage imageNamed:dict23[@"profileimg"]]; NSString *savePath = [NSHom

我使用此代码从iOS应用程序在Whatsapp上共享图像,但控制器始终处于
else
状态。请给我一个解决办法

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];
}
使用此代码仅在whats应用程序上共享文本

请参阅此链接以共享图像

使用此代码仅在whats应用程序上共享文本

请参阅此链接以共享图像


检查此链接并遵循其中提到的步骤:

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

在ViewController.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;
}

检查此链接并遵循其中提到的步骤:

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

在ViewController.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;
}

您可以与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];
        }

您可以与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];
        }
有关plz的详细信息,请参阅下面的链接

有关plz的详细信息,请参阅下面的链接



我使用此代码,但总是给出“您的设备未安装WhatsApp”消息…然后您在模拟器中运行它,您必须在安装了whats应用程序的设备中运行它。如果要使用-[UIApplication canOpenURL:]查询用户iPhone上是否存在WhatsApp,请确保在应用程序的Info.plist的LSApplicationQueriesSchemes键下包含WhatsApp URL方案。我使用此代码,但始终给出“您的设备未安装WhatsApp”消息…然后您在模拟器中运行它,您必须在安装了whats应用程序的设备中运行它。如果您想使用-[UIApplication canOpenURL:]使用此代码查询用户iPhone上WhatsApp的存在,请确保在应用程序的Info.plist的LSApplicationQueriesSchemes键下包含WhatsApp URL方案,我发现输出是正确的“WhatsApp未安装“请告诉我什么做错了iPHONE中应该安装WhatsApp。这在模拟器中不起作用,先生,我在ios设备上测试了这段代码。这台设备安装了whatsApp。如果您想使用-[UIApplication canOpenURL:]查询用户iPhone上是否存在whatsApp,请确保在应用程序的Info.plist下的LSApplicationQueriesSchemes键下包含whatsApp URL方案。使用这段代码,我发现输出是“WhatsApp未安装“请告诉我什么做错了iPHONE中应该安装WhatsApp。这在模拟器中不起作用,先生,我在ios设备上测试了此代码。此设备安装whatsApp。如果您想使用-[UIApplication canOpenURL:]查询用户iPhone上是否存在whatsApp,请确保在应用程序的Info.plist的LSApplicationQueriesSchemes键下包含whatsApp URL方案.contoller始终进入其他部分iPHONE中应安装哪些应用程序。这在模拟器中不起作用。如果要使用-[UIApplication canOpenURL:]查询用户iPhone上是否存在WhatsApp,请确保在应用程序的Info.plist中LSApplicationQueriesSchemes键下包含WhatsApp URL方案。contoller始终进入其他部分WhatsApp应安装在iPhone中。这在Simulator中不起作用。如果要使用-[UIApplication canOpenURL:]查询用户iPhone上是否存在WhatsApp,请确保在应用程序的Info.plist中LSApplicationQueriesSchemes键下包含WhatsApp URL方案。