-
Notifications
You must be signed in to change notification settings - Fork 9.8k
Add canLaunch method to url_launcher plugin #8
Changes from all commits
d7b5dbb
7c3ed27
2b3b91e
18c3b53
6c6e71d
96c1963
44786cc
6c1e633
4354c1a
16634fc
b7ec3d6
f97821c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,6 @@ | ||
## [0.3.0] - 2017-04-27 | ||
|
||
* Add `canLaunch` method. | ||
|
||
## [0.2.0] - 2017-04-24 | ||
|
||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,28 +3,56 @@ | |
@implementation UrlLauncherPlugin { | ||
} | ||
|
||
- (instancetype)initWithController:(FlutterViewController *)controller { | ||
- (instancetype)initWithController:(FlutterViewController*)controller { | ||
self = [super init]; | ||
if (self) { | ||
FlutterMethodChannel* channel = [FlutterMethodChannel | ||
methodChannelWithName:@"plugins.flutter.io/URLLauncher" | ||
methodChannelWithName:@"plugins.flutter.io/url_launcher" | ||
binaryMessenger:controller]; | ||
[channel setMethodCallHandler:^(FlutterMethodCall *call, | ||
FlutterResult result) { | ||
if ([@"UrlLauncher.launch" isEqualToString:call.method]) { | ||
[self launchURL:call.arguments]; | ||
result(nil); | ||
NSString* url = call.arguments; | ||
if ([@"canLaunch" isEqualToString:call.method]) { | ||
result(@([self canLaunchURL:url])); | ||
} else if ([@"launch" isEqualToString:call.method]) { | ||
[self launchURL:url result:result]; | ||
} else { | ||
result(FlutterMethodNotImplemented); | ||
} | ||
}]; | ||
} | ||
return self; | ||
} | ||
|
||
- (NSDictionary*)launchURL:(NSString*)urlString { | ||
- (BOOL)canLaunchURL:(NSString*)urlString { | ||
NSURL* url = [NSURL URLWithString:urlString]; | ||
UIApplication* application = [UIApplication sharedApplication]; | ||
bool success = [application canOpenURL:url] && [application openURL:url]; | ||
return @{@"success": @(success) }; | ||
return [application canOpenURL:url]; | ||
} | ||
|
||
- (void)launchURL:(NSString*)urlString result:(FlutterResult)result { | ||
NSURL* url = [NSURL URLWithString:urlString]; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Similarly here. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. According to https://developer.apple.com/reference/foundation/nsurl/1572047-urlwithstring url is nil. |
||
UIApplication* application = [UIApplication sharedApplication]; | ||
if ([application respondsToSelector:@selector(openURL:options:completionHandler:)]) { | ||
// iOS 10 and above | ||
[application openURL:url options:@{} completionHandler: ^(BOOL success) { | ||
[self sendResult:success result:result url:url]; | ||
}]; | ||
} else { | ||
[self sendResult:[application openURL:url] result:result url:url]; | ||
} | ||
} | ||
|
||
- (void)sendResult:(BOOL)success result:(FlutterResult)result url:(NSURL*)url { | ||
if (success) { | ||
result(nil); | ||
} else { | ||
result([FlutterError errorWithCode:@"Error" | ||
message:[NSString stringWithFormat:@"Error while launching %@", url] | ||
details:nil]); | ||
|
||
} | ||
|
||
} | ||
|
||
@end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,25 @@ | ||
// Copyright 2017 The Chromium Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
import 'dart:async'; | ||
|
||
import 'package:flutter/services.dart'; | ||
|
||
const _channel = const MethodChannel('plugins.flutter.io/url_launcher'); | ||
|
||
/// Parse the specified URL string and delegate handling of the same to the | ||
/// underlying platform. | ||
Future<Null> launch(String urlString) { | ||
return const MethodChannel('plugins.flutter.io/URLLauncher').invokeMethod( | ||
'UrlLauncher.launch', | ||
return _channel.invokeMethod( | ||
'launch', | ||
urlString, | ||
); | ||
} | ||
|
||
Future<bool> canLaunch(String urlString) { | ||
return _channel.invokeMethod( | ||
'canLaunch', | ||
urlString, | ||
); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What happens, if the
urlString
is not a URL (syntax error)?