2016-01-07 60 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中。這在模擬器中不起作用 –

相關問題