Skip to content
This repository has been archived by the owner on Feb 22, 2023. It is now read-only.

Add canLaunch method to url_launcher plugin #8

Merged
merged 12 commits into from
Apr 27, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions packages/url-launcher/CHANGELOG.md
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

Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
// 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.

package io.flutter.plugins.url_launcher;

import android.content.ComponentName;
import android.content.Intent;
import android.net.Uri;

@@ -13,34 +18,45 @@
* UrlLauncherPlugin
*/
public class UrlLauncherPlugin implements MethodCallHandler {
private FlutterActivity activity;

public static UrlLauncherPlugin register(FlutterActivity activity) {
return new UrlLauncherPlugin(activity);
}

private UrlLauncherPlugin(FlutterActivity activity) {
this.activity = activity;
new MethodChannel(
activity.getFlutterView(), "plugins.flutter.io/URLLauncher").setMethodCallHandler(this);
}

@Override
public void onMethodCall(MethodCall call, Result result) {
if (call.method.equals("UrlLauncher.launch")) {
launchURL((String) call.arguments);
result.success(null);
} else {
result.notImplemented();
private FlutterActivity activity;

public static UrlLauncherPlugin register(FlutterActivity activity) {
return new UrlLauncherPlugin(activity);
}

private UrlLauncherPlugin(FlutterActivity activity) {
this.activity = activity;
new MethodChannel(
activity.getFlutterView(), "plugins.flutter.io/url_launcher").setMethodCallHandler(this);
}

@Override
public void onMethodCall(MethodCall call, Result result) {
String url = call.arguments();
if (call.method.equals("canLaunch")) {
canLaunch(url, result);
} else if (call.method.equals("launch")) {
launchURL(url, result);
} else {
result.notImplemented();
}
}

private void launchURL(String url, Result result) {
Intent launchIntent = new Intent(Intent.ACTION_VIEW);
launchIntent.setData(Uri.parse(url));
activity.startActivity(launchIntent);
result.success(null);
}
}
private void launchURL(String url) {
try {
Intent launchIntent = new Intent(Intent.ACTION_VIEW);
launchIntent.setData(Uri.parse(url));
activity.startActivity(launchIntent);
} catch (java.lang.Exception exception) {
// Ignore parsing or ActivityNotFound errors

private void canLaunch(String url, Result result) {
Intent launchIntent = new Intent(Intent.ACTION_VIEW);
launchIntent.setData(Uri.parse(url));
ComponentName componentName = launchIntent.resolveActivity(activity.getPackageManager());

boolean canLaunch = componentName != null &&
!"{com.android.fallback/com.android.fallback.Fallback}".
equals(componentName.toShortString());
result.success(canLaunch);
}
}
}
102 changes: 51 additions & 51 deletions packages/url-launcher/example/ios/Pods/Pods.xcodeproj/project.pbxproj

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.

56 changes: 44 additions & 12 deletions packages/url-launcher/example/lib/main.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
// 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/material.dart';
import 'package:url_launcher/url_launcher.dart';

@@ -9,7 +15,7 @@ class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return new MaterialApp(
title: 'URL Laucher',
title: 'URL Launcher',
theme: new ThemeData(
primarySwatch: Colors.blue,
),
@@ -27,8 +33,28 @@ class MyHomePage extends StatefulWidget {
}

class _MyHomePageState extends State<MyHomePage> {
Future<Null> _launched;

void _launchUrl() {
launch('https://flutter.io');
setState(() {
_launched = _launch('https://flutter.io');
});
}

Future<Null> _launch(String url) async {
if (await canLaunch(url)) {
await launch(url);
} else {
throw "Could not launch $url";
}
}

Widget _launchStatus(BuildContext context, AsyncSnapshot<Null> snapshot) {
if (snapshot.hasError) {
return new Text('Error: ${snapshot.error}');
} else {
return const Text('');
}
}

@override
@@ -38,17 +64,23 @@ class _MyHomePageState extends State<MyHomePage> {
title: new Text(widget.title),
),
body: new Center(
child: new Row(
mainAxisSize: MainAxisSize.min,
children: [
new Padding(
padding: new EdgeInsets.all(16.0),
child: new Text("https://flutter.io"),
),
new RaisedButton(
onPressed: _launchUrl,
child: new Text("Go"),
child: new Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
new Row(
mainAxisSize: MainAxisSize.min,
children: [
new Padding(
padding: new EdgeInsets.all(16.0),
child: new Text("https://flutter.io"),
),
new RaisedButton(
onPressed: _launchUrl,
child: new Text("Go"),
),
],
),
new FutureBuilder<Null>(future: _launched, builder: _launchStatus),
],
),
),
44 changes: 36 additions & 8 deletions packages/url-launcher/ios/Classes/UrlLauncherPlugin.m
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];
Copy link
Contributor

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)?

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];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similarly here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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
17 changes: 15 additions & 2 deletions packages/url-launcher/lib/url_launcher.dart
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,
);
}
2 changes: 1 addition & 1 deletion packages/url-launcher/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: url_launcher

version: 0.2.0
version: 0.3.0
description: Flutter plugin for launching a URL
author: Flutter Team <flutter-dev@googlegroups.com>
homepage: https://github.com/flutter/plugins