2

I have integrated whastapp in my iOS app. When I tested it in my iOS 10 device. It crashes with an issue.

Snapshotting a view that has not been rendered results in an empty snapshot. Ensure your view has been rendered at least once before snapshotting or snapshot after screen updates.

NSURL *whatsappURL = [NSURL URLWithString:[NSString stringWithFormat: @"whatsapp://send?abid=%@&text=WelcomeToChatBought",[abidArray objectAtIndex:buttonclicked.tag-1000]]];
        if ([[UIApplication sharedApplication] canOpenURL: whatsappURL])
        {
            [[UIApplication sharedApplication] openURL: whatsappURL];
        }

What might be the issue. Any help would be appreciated.

Ronak Chaniyara
  • 5,335
  • 3
  • 24
  • 51

1 Answers1

5

You need to set LSApplicationQueriesSchemes in plist if not set:

Like,

<key>LSApplicationQueriesSchemes</key>
<array>
 <string>urlscheme1</string>
 <string>urlscheme2</string>

</array> 

Also, note that openURL(_:) is deprecated in iOS 10.

The new UIApplication method openURL:options:completionHandler:, which is executed asynchronously and calls the specified completion handler on the main queue (this method replaces openURL:).

New method in iOS 10:

- (void)openURL:(NSURL*)url options:(NSDictionary<NSString *, id> *)options
  completionHandler:(void (^ __nullable)(BOOL success))completion

Parameters:

  • The URL to open

  • An options dictionary (see below for valid entries). Use an empty dictionary for the same behaviour as openURL:.

  • completion handler called on the main queue with the success. Nullable if you are not interested in the status.

Like,

UIApplication *application = [UIApplication sharedApplication];
[application openURL:URL options:@{} completionHandler:nil];

Example:

NSString *scheme=[NSString stringWithFormat: @"whatsapp://send?abid=%@&text=WelcomeToChatBought",[abidArray objectAtIndex:buttonclicked.tag-1000]]];

  UIApplication *application = [UIApplication sharedApplication];
  NSURL *URL = [NSURL URLWithString:scheme];

  if ([application respondsToSelector:@selector(openURL:options:completionHandler:)]) {
    [application openURL:URL options:@{}
       completionHandler:^(BOOL success) {
      NSLog(@"Open %@: %d",scheme,success);
    }];
  } else {
    BOOL success = [application openURL:URL];
    NSLog(@"Open %@: %d",scheme,success);
  }

Read more here:

http://useyourloaf.com/blog/openurl-deprecated-in-ios10/

Edit:(Code based on iOS Version)

NSURL *URL = [NSURL URLWithString:strUrl];

if([[UIDevice currentDevice].systemVersion floatValue] >= 10.0){

  if ([application respondsToSelector:@selector(openURL:options:completionHandler:)]) {
    [application openURL:URL options:@{}
       completionHandler:^(BOOL success) {
      NSLog(@"Open %@: %d",scheme,success);
    }];
  } else {
    BOOL success = [application openURL:URL];
    NSLog(@"Open %@: %d",scheme,success);
  }


}
else{

  bool can = [[UIApplication sharedApplication] canOpenURL:URL];

  if(can){

     [[UIApplication sharedApplication] openURL:URL];

  }

}
Ronak Chaniyara
  • 5,335
  • 3
  • 24
  • 51
  • Deprecated of not, `openURL:` works as expected. That has nothing do with the problem here. The other part of your answer, about `LSApplicationQueriesSchemes`, is correct. – Léo Natan Oct 14 '16 at 13:51
  • `LSApplicationQueriesSchemes` is seems solution here but may be for others it may help@LeoNatan – Ronak Chaniyara Oct 14 '16 at 14:06
  • I edited the answer. However, you should add that the new method does not exist in ios9. So if the author wants to target both, he cannot use the new one. – Léo Natan Oct 14 '16 at 14:16