Ragini Jaisankar
Ragini Jaisankar

Reputation: 21

Whatsapp integration and openURL issue in iOS 10

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.

Upvotes: 2

Views: 2277

Answers (1)

Ronak Chaniyara
Ronak Chaniyara

Reputation: 5435

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

  }

}

Upvotes: 5

Related Questions