How to get bundle id in flutter - ios

I used the below method to get the app name and packageName but I need Bundle id for iPhone users.
I want to share an app link.
I did it in android but on iPhone, I need bundle id.
Future<Null> _initPackageInfo() async {
final PackageInfo info = await PackageInfo.fromPlatform();
setState(() {
_packageInfo = info;
packageName = info.packageName;
appName = info.appName;
buildNumber = info.buildNumber;
});
}

To find the project name manually, you can look in AndroidManifest.xml or in Info.plist.
Android
In Android the package name is in the AndroidManifest:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
...
package="com.example.appname">
iOS
In iOS the package name is the bundle identifier in Info.plist:
<key>CFBundleIdentifier</key>
<string>$(PRODUCT_BUNDLE_IDENTIFIER)</string>
which is found in Runner.xcodeproj/project.pbxproj:
PRODUCT_BUNDLE_IDENTIFIER = com.example.appname;
See also
How to change package name in flutter?

In iOS portion of a Flutter project Product Bundle Identifier is in project.pbxproj file in path:
[your-flutter-project-dir]\ios\Runner.xcodeproj\project.pbxproj
and that is specified as following:
PRODUCT_BUNDLE_IDENTIFIER = com.app.flutter.example;
Note in that this value is same as Android Package Name in Flutter projects.

If you just need to get the IOS bundle ID manually, here is how
In Android Studio select the root folder (ex. flutte_name)
In the taskbar go to Tools>>Flutter>>Open IOS Modules in Xcode
In Xcode open Runner and under Identity/Bundle Identifier there is your ID

You probably want to update it to a custom name rather than com.example.appName anyways so you can check out this package called change_app_name on pub.dev here https://pub.dev/packages/change_app_package_name
Super simple I just did it myself. Add the package to your pubspec file and than in a terminal in the root folder type in "flutter pub run change_app_package_name:main com.company.app" change the last part to whatever you want and it will update your whole project with the new name you chose

what i see you have already get the package name by info.packageName; and appName info.appName;
try request to http://itunes.apple.com/search?media=software&country={countryID}&term={appName}
you can test by curl instead of browser ( this will return file.txt )
curl --location -g --request GET 'http://itunes.apple.com/search?media=software&country=id&term=appName' \
--header 'Accept: application/json' | jq
in other solution some people can get it from
// if doesnt work try with these lookup
itunes.apple.com/lookup?bundleId=com.apple.Pages
| jq // these for make response beutifull
response json
....
....
bundleId: '212312xxxxxx'
trackViewUrl: 'https://apps.apple.com/id/app/appName/id212312xxxxxx'
....
....
i use these concept to self update from backend ( SplashScreen / check app version ) will popUP alert forced user to go to the market place.

Use get_version package . It's the easiest way
Installing :
dependencies:
get_version: any
Usage:
String projectAppID;
// Platform messages may fail, so we use a try/catch PlatformException.
try {
projectAppID = await GetVersion.appID;
} on PlatformException {
projectAppID = 'Failed to get app ID.';
}
You can use it as String inside anything you want like Text widget etc ...
Another extract of get_version in a small application :
import 'package:get_version/get_version.dart';
class _MyAppState extends State<MyApp> {
String _projectAppID = '';
#override
initState() {
super.initState();
initPlatformState();
}
// Platform messages are asynchronous, so we initialize in an async method.
initPlatformState() async {
String projectAppID;
try {
projectAppID = await GetVersion.appID;
} catch (e) {
projectAppID = 'Failed to get app ID.';
}
setState(() {
_projectAppID = projectAppID;
});
}
#override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
body: ListTile(
leading: new Icon(Icons.info),
title: const Text('App ID'),
subtitle: new Text(_projectAppID),
),
),
);
}
}
Output :

Related

Flutter Sign in with apple failure

i implement sign in with apple on my apps, for some reason my server return error cause username length from sign in with apple didn't meet my condition and i didn't cache the information of the user. After that i update the server to remove username length requirement, but unfortunately user name and email is null? The solution i know is to stop using apple id from setting page on Iphone, is there any other solution from code? here is my code :
AppleSignInButton(
style: ButtonStyle.whiteOutline,
cornerRadius: 8,
type: ButtonType.signIn,
onPressed: () async {
var isConnected = await checkConnection();
if (!isConnected) {
showErrorDialog(message: ErrorMessage.NO_NETWORK);
} else {
final AuthorizationResult result = await AppleSignIn.performRequests([
AppleIdRequest(requestedScopes: [Scope.email, Scope.fullName])
]);
if (result != null) {
switch (result.status) {
case AuthorizationStatus.authorized:
_loginWithApple(result.credential);
break;
case AuthorizationStatus.cancelled:
break;
case AuthorizationStatus.error:
showErrorDialog(message: result.error.localizedFailureReason);
break;
}
} else {
showErrorDialog(message: result.status.toString(), onTap: (){});
}
}
},)
Apparently Apple provides the fullName and email fields for the first sign in only. These fields will be null for subsequent sign ins.
However, you may opt to get the fullName through appleIDCredential.fullName in didCompleteWithAuthorization delegate method, and update the user's profile yourself.
Source: https://github.com/firebase/firebase-ios-sdk/issues/4393#issuecomment-559012512
Add the dependency to your pubspec.yaml file .
You can get the latest version from https://pub.dev/packages/apple_sign_in
dependencies:
apple_sign_in: ^0.1.0
You can install packages from the command line:
$ flutter pub get
Alternatively, your editor might support flutter pub get like Android Studio and Visual Studio code.
import the “apple_sign_in.dart” to use it’s functionality .
import 'package:apple_sign_in/apple_sign_in.dart';
In initState( ) of Scaffold initialise the Functionality .
if(Platform.isIOS){
//check for ios if developing for both android & ios
AppleSignIn.onCredentialRevoked.listen((_) {
print("Credentials revoked");
});
}
Place the Apple Sign in button Specially provided for this operation .
AppleSignInButton(
style: ButtonStyle.black,
type: ButtonType.continueButton,
onPressed: appleLogIn,
);
Define the appleLogIn( ) method for the sign in functionality
if(await AppleSignIn.isAvailable()) {
//Check if Apple SignIn isn available for the device or not
}else{
print('Apple SignIn is not available for your device');
}
If Available then We can request for login
if(await AppleSignIn.isAvailable()) {
final authorizationResult result = await
AppleSignIn.performRequests([
AppleIdRequest(requestedScopes: [Scope.email, Scope.fullName])
]);
}
Handle the result according to your requirement
if(await AppleSignIn.isAvailable()) {
final AuthorizationResult result = await AppleSignIn.performRequests([
AppleIdRequest(requestedScopes: [Scope.email, Scope.fullName])
]);
switch (result.status) {
case AuthorizationStatus.authorized:
print(result.user);//All the required credentials
case AuthorizationStatus.error:
print("Sign in failed: ${result.error.localizedDescription}");
break;
case AuthorizationStatus.cancelled:
print('User cancelled');
break;
}
}
You can send the credentials to your backend on whatever you want to do .

Login with facebook error on iOS in flutter project

I wrote a flutter login with facebook app.
It works correctly on Android.
On iOS simulator and on real iPhone device it does not work. Both having iOS 13.2.
pub spec.yaml file
firebase_auth: ^0.6.6
flutter_facebook_login: ^1.1.1
flutter_auth_buttons: ^0.3.1
main.dart file
import 'package:flutter/material.dart';
import 'package:firebase_auth/firebase_auth.dart';
import 'package:flutter_facebook_login/flutter_facebook_login.dart';
import 'package:flutter_auth_buttons/flutter_auth_buttons.dart';
void main() => runApp(MyApp());
class MyApp extends StatefulWidget {
#override
State<StatefulWidget> createState() {
return _MyAppState();
}
}
class _MyAppState extends State<MyApp> {
FirebaseAuth _auth = FirebaseAuth.instance;
bool isLogged = false;
FirebaseUser myUser;
Future<FirebaseUser> _loginWithFacebook() async {
var facebookLogin = new FacebookLogin();
var result = await facebookLogin.logInWithReadPermissions(['email']);
debugPrint(result.status.toString());
if (result.status == FacebookLoginStatus.loggedIn) {
FirebaseUser user =
await _auth.signInWithFacebook(accessToken: result.accessToken.token);
return user;
}
return null;
}
void _login() {
_loginWithFacebook().then((response) {
if (response != null) {
myUser = response;
setState(() {
isLogged = true;
});
}
});
}
void _logout() async {
await _auth.signOut().then((response) {
setState(() {
isLogged = false;
});
});
}
#override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Login App',
home: Scaffold(
appBar: AppBar(
title: Text(isLogged ? 'Profile Page' : 'Login App'),
actions: <Widget>[
IconButton(
onPressed: _logout,
icon: Icon(Icons.power_settings_new),
)
],
),
body: Center(
child: isLogged
? Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text('Name: ' + myUser.displayName),
Image.network(myUser.photoUrl),
],
)
: FacebookSignInButton(
onPressed: _login,
),
),
),
);
}
}
I added the plist file in the Xcode project correctly. And configured the iOS version of the Facebook developer correctly based on the following steps:
https://developers.facebook.com/docs/facebook-login/ios
I changed the project workspace settings in Xcode between Legacy build and new build system. I still get error.
I tried to add the following to the pod file, and still get error
pod 'FBSDKCoreKit', '~> 4.44'
And even all proposed solutions in here
How to fix build error with FBSDKLoginKit in Xcode
The error that I get is
No known class method for selector 'dictionary:setObject:forKey:'
I also tried to change the Firebase version and the Facebook Flutter login version in pub spec file (between 1.1.1 and 1.2.0 because I do not want to use AndroidX) but still getting the error.
Note that I updated Xcode to latest version, and error was not fixed.
I changed the iOS deployment version in Xcode for all pods to version 8. And error not fixed!
UPDATE (JULY 2020)
You can use a new package that works for both Android and iOS:
flutter_login_facebook
If you want to use the flutter_facebook_login, below is a solution:
UPDATE (APRIL 2020)
Add flutter_facbook_login version 3.0.0 from pub.dev to your flutter project
Create your app on Facebook developers and configure your flutter app accordingly
Change your iOS target version to 9 in Xcode
Open in terminal the Podfile.lock which is located in the iOS folder of your flutter project and change all facebook api versions to 5.8.0. If the Podfile.lock does not exist, run your app once (so the file is created) and after it fires an error edit Podfile.lock then run it again. If your app still fails, run pod install manually form terminal.
There are two problems:
the compatibility between Facebook Login API and Firebase.
iOS update to version 13.x.
In pubspec.yaml file updated the firebase version to latest version.
Also, I am using version 1.2.0 for facebook login but still 1.1.1 works.
firebase_auth: ^0.14.0+5
flutter_facebook_login: ^1.2.0
flutter_auth_buttons: ^0.3.1
I opened Runner.xcworkspace in Xcode (located in iOS folder inside flutter project), then File->WorkSpace settings, and selected New Build System option.
For all pods, change iOS deployment version to 8.0 (or above).
Now the application builds successfully and runs on iOS. But I get CancelledByUser each time I click on the Login with Facebook button.
This is a bug when having iOS update 13.x running the facebook login api.
I found a turnaround that made the login works.
final facebookLogin = new FacebookLogin();
facebookLogin.loginBehavior = FacebookLoginBehavior.webViewOnly;
You can directly use my function as it works on both android and iOS and also registers the user in firebase console.
I have simply opening the login page in the WebView and when the user register himself, I am storing the credential in the firebase and then printing his name .
final FacebookLogin fblogin = FacebookLogin();
FirebaseAuth auth = FirebaseAuth.instance;
Future loginwihFacebook() async {
fblogin.loginBehavior = FacebookLoginBehavior.webViewOnly;
final result = await fblogin.logInWithReadPermissions(["email"]);
switch (result.status) {
case FacebookLoginStatus.loggedIn:
final token = result.accessToken.token;
await auth.signInWithCredential(FacebookAuthProvider.credential(token));
var url =
'https://graph.facebook.com/v2.12/me?fields=name,first_name,picture,last_name,email&access_token=$token';
var graphapiresponse = await http.get(url);
var profileData = jsonDecode(graphapiresponse.body);
var name = profileData["name"];
print(name);
break;
case FacebookLoginStatus.cancelledByUser:
print("cancelled by user");
break;
case FacebookLoginStatus.error:
print("error");
break;
}
}

How do I open an external url in flutter web in new tab or in same tab

I have a simple web app I have created with flutter web. I would like to know how I can open new an external url either in a new tab or in the same tab in my flutter web app. say I want to open the url https://stackoverflow.com/questions/ask
I think you want this — dart:js enables interoperability between Dart and JS —:
import 'dart:js' as js;
// ...
FlatButton(
child: Text('Button'),
onPressed: () {
js.context.callMethod('open', ['https://stackoverflow.com/questions/ask']);
},
)
One simple way is to just create a button and use dart:html's window.open() method:
import 'dart:html' as html;
// ...
html.window.open('https://stackoverflow.com/questions/ask', 'new tab');
The name parameter — which I left as 'new tab' — refers to the new tab's window name, about which you can learn more from MDN's documentation.
https://github.com/flutter/plugins/tree/master/packages/url_launcher/url_launcher_web
url_launcher has been the solution for android and ios, recently it added support for web.
You can use the url_launcher plugin
Then in your code
import 'package:flutter/material.dart';
import 'package:url_launcher/url_launcher_string.dart';
void main() {
runApp(Scaffold(
body: Center(
child: RaisedButton(
onPressed: _launchURL,
child: Text('Show Flutter homepage'),
),
),
));
}
_launchURL() async {
const url = 'https://flutter.io';
if (await canLaunchUrlString(url)) {
await launchUrlString(url);
} else {
throw 'Could not launch $url';
}
}
Example taken from the package site
As of url_launcher: ^6.1.0 The plugin introduces support for webOnlyWindowName property and few Other new apis like launchUrl, For web support now you longer have to depend on dart:html You can simply declare the below function
Future<void> launchLink(String url, {bool isNewTab = true}) async {
await launchUrl(
Uri.parse(url),
webOnlyWindowName: isNewTab ? '_blank' : '_self',
);
}
and use it like this
onTap:(){
launchLink('https://stackoverflow.com/questions/ask', isNewTab: true)
}
Answered here https://stackoverflow.com/a/56656885/361832
Flutter Web does not support plugins (yet), so you have to use replacements from dart:html
https://api.dartlang.org/stable/2.4.0/dart-html/Window/open.html window.open(url, 'tab');
or
https://api.dartlang.org/stable/2.4.0/dart-html/Window/location.html window.location.assign(url);
Extending the answer of #xuyanjun which works fine when to want to open an external link from flutter web to a new tab. But if you want to open an external link to the website in the same tab in which the flutter web app is currently running.
then you can do it like this.
import 'dart:js' as js;
// ...
FlatButton(
child: Text('Button'),
onPressed: () {
js.context.callMethod('open', ['https://blup.in/','_self']); //<= find explanation below
},
)
Explanation :- dart:js package from flutter provide the functionality to call web-specific functions like open function from flutter and all the strings in the list are parameter which are passed to the function refer this.
So if you want to open new tab then not need to pass seconds parameter but if you wan to open in same tab then pass _self as second parameter.
You can use the below code to launch the URL in the same tab.
window.top.location.href = '<your url>'
Must import
import 'dart:html';
package url_launcher now has web support.
just import url_launcher_web, and url_launcher to your pubspec.yaml
import 'package:url_launcher/url_launcher.dart';
const String _url = 'https://flutter.dev';
void _launchURL() {
launch(_url);
}
To launch URL you need to import url_launcher with latest version and you are good to launch URL through following code:
//Launch Github
_launchGithub() async {
const url = 'https://github.com/Pranav2918'; //Paste your URL string here
if (await canLaunchUrlString(url)) {
await launchUrlString(url);
} else {
throw 'Could not launch $url';
}
}
For non string URLs:
Another approach :
final Uri _url = Uri.parse('https://flutter.dev');
Future<void> _launchUrl() async {
if (!await launchUrl(_url)) {
throw 'Could not launch $_url';
}
}
Happy Fluttering !!

How to open Apple Maps using Flutter/Dart?

Our Flutter App shows a number of locations using Google Maps, if available, or else using the local browser.
Although we had already previously uploaded a binary code for iOS which was accepted by Apple and successfully published in the App Store, now that we have added some more locations and thus attempted to publish a new version, Apple has rejected our binary, stating that it is mandatory to use "Apple Maps" instead of anything that starts with a "G", like Google...
The rejection message reads as follows:
Your app's location feature is not integrated with the built-in mapping functionality, which limits users to a third-party maps app.
Next Steps
To resolve this issue, please revise your app to give users the option to launch the native Apple Maps app.
I have found that there exists some documentation about a Javascript library named MapKit JS, which serves precisely the purpose of interacting with Apple Maps: https://developer.apple.com/maps/mapkitjs/
<script src="https://cdn.apple-mapkit.com/mk/5.x.x/mapkit.js"></script>
<script>
mapkit.init({
authorizationCallback: function(done) {
var xhr = new XMLHttpRequest();
xhr.open("GET", "/services/jwt");
xhr.addEventListener("load", function() {
done(this.responseText);
});
xhr.send();
}
});
var Cupertino = new mapkit.CoordinateRegion(
new mapkit.Coordinate(37.3316850890998, -122.030067374026),
new mapkit.CoordinateSpan(0.167647972, 0.354985255)
);
var map = new mapkit.Map("map");
map.region = Cupertino;
</script>
Nevertheless, I could really use some help no how to connect with this MapKit JS using DART, instead of JAVA, for our Flutter application.
Thank you immensely for your kind help!
Daniel
Firstly, install the url_launcher plugin
Secondly, add the below code in Info.plist:
<key>LSApplicationQueriesSchemes</key>
<array>
<string>googlechromes</string>
<string>comgooglemaps</string>
</array>
Thirdly:
var urlAppleMaps = 'https://maps.apple.com/?q=$lat,$lng';
if (await canLaunch(urlAppleMaps)) {
await launch(urlAppleMaps);
} else {
throw 'Could not launch $url';
}
We can use it like this:
_launchMap(BuildContext context, lat, lng) async {
var url = '';
var urlAppleMaps = '';
if (Platform.isAndroid) {
url = "https://www.google.com/maps/search/?api=1&query=${lat},${lng}";
} else {
urlAppleMaps = 'https://maps.apple.com/?q=$lat,$lng';
url = "comgooglemaps://?saddr=&daddr=$lat,$lng&directionsmode=driving";
if (await canLaunch(url)) {
await launch(url);
} else {
throw 'Could not launch $url';
}
}
if (await canLaunch(url)) {
await launch(url);
} else if (await canLaunch(urlAppleMaps)) {
await launch(urlAppleMaps);
} else {
throw 'Could not launch $url';
}
}
You could try to use a Map Launcher plugin to launch apple/google maps depending on a platform
import 'dart:io';
import 'package:map_launcher/map_launcher.dart';
if (Platform.isIOS) {
await MapLauncher.launchMap(
mapType: MapType.apple,
coords: Coords(31.233568, 121.505504),
title: "Shanghai Tower",
description: "Asia's tallest building",
);
} else {
await MapLauncher.launchMap(
mapType: MapType.google,
coords: Coords(31.233568, 121.505504),
title: "Shanghai Tower",
description: "Asia's tallest building",
);
}
Just an idea but maybe you can try to use the url_launch plugin to launch a url following the schemata given in the apple maps url schemata given here: https://developer.apple.com/library/archive/featuredarticles/iPhoneURLScheme_Reference/MapLinks/MapLinks.html#//apple_ref/doc/uid/TP40007899-CH5-SW1
A simple solution I have found is to replace the google maps URL with a maps.apple.com equivalent, using the same url_launcher function.
For example, the original function for Google Maps:
void _mapaBethania() async {
const url = "https://www.google.com/maps/place/Panamanian+Institute+for+Special+Training/#9.0067418,-79.5300556,17z/data=!3m1!4b1!4m5!3m4!1s0x8faca84549395297:0x9c54b1fdb96ac590!8m2!3d9.0067365!4d-79.5278669";
if (await canLaunch(url)) {
await launch(url);
} else {
throw 'Lo sentimos, no es posible abrir: $url';
}
}
Has become:
void _mapaBethania() async {
const url = "https://maps.apple.com/?q=IPHE&ll=9.0067418,-79.5300556&z=16";
if (await canLaunch(url)) {
await launch(url);
} else {
throw 'Lo sentimos, no es posible abrir: $url';
}
}
Nevertheless, although this change does fulfill Apple's enforced obligation to use their own software instead of the competition's, the user experience with Apple Maps is very poor, because once the Map App is shown, it becomes a bit confusing and difficult to return back to the original App.
Therefore, I am planning to write code that enables both options, Apple Maps in order to comply with Apple enforcement, and also Google Maps in order to provide a better user experience, despite Apple.
Anyway, the latter is just a personal opinion; the true fact is that replacing the URL for a maps.apple.com equivalent using the same launch_url function, does seem acceptable to comply with Apple requirements.

How to play a custom sound in Flutter?

I was able to play a simple sound with this line of code:
SystemSound.play(SystemSoundType.click);
How can I play a customized sound?
Let's say a short mp3
Simple solution for playing a file already defined in assets is using AudioCache.
Library: https://pub.dartlang.org/packages/audioplayers.
More about AudioCache
After adding library to pubspec.yaml, import required class:
import 'package:audioplayers/audio_cache.dart';
add an asset in the same file and place the file with sound to assets folder (if you don't have this folder, create it)
assets:
- assets/sound_alarm.mp3
then add this code:
static AudioCache player = new AudioCache();
const alarmAudioPath = "sound_alarm.mp3";
player.play(alarmAudioPath);
An example here
Thanks for checking out Flutter!
Flutter SDK today (as of May 5, 2017) doesn't have built-in support to play and control arbitrary audio. However, we designed our plugin system to support it.
This plugin adds audio support to Flutter: https://pub.dartlang.org/packages/audioplayer
From the plugin's README:
Future play() async {
final result = await audioPlayer.play(kUrl);
if (result == 1) setState(() => playerState = PlayerState.playing);
}
// add a isLocal parameter to play a local file
Future playLocal() async {
final result = await audioPlayer.play(kUrl);
if (result == 1) setState(() => playerState = PlayerState.playing);
}
Future pause() async {
final result = await audioPlayer.pause();
if (result == 1) setState(() => playerState = PlayerState.paused);
}
Future stop() async {
final result = await audioPlayer.stop();
if (result == 1) {
setState(() {
playerState = PlayerState.stopped;
position = new Duration();
});
}
}
The audioplayers works (from https://medium.com/#bennett4/adding-custom-sound-effects-to-a-flutter-mobile-app-41594f1f3305):
(1) Add the library to your pubspec.yaml: audioplayers: ^0.15.1
(2) In pubspec.yaml under flutter add the reference to your assets file:
flutter
assets:
- assets/yes.mp3
MAKE SURE it is under the assets folder. It does not work when it is in a subfolder. For example, something like: - assets/sounds/yes.mp3 will not work. Just put your audio file in the assets folder, not in its subfolder
(3) import the library in your app as: import package:audioplayers/audioplayers.dart;
(4) then define this function:
Future<AudioPlayer> playLocalAsset() async {
AudioCache cache = new AudioCache();
//At the next line, DO NOT pass the entire reference such as assets/yes.mp3. This will not work.
//Just pass the file name only.
return await cache.play("yes.mp3");
}
(5) call the function whenever you need to play a sound: await playLocalAsset();
Null-safe code:
Add dependency to your pubspec.yaml file,
dependencies:
audioplayers: ^0.19.0
Add audio file path to your pubspec.yaml file.
flutter:
assets:
- assets/audio/my_audio.mp3
Run flutter pub get
Full code:
class HomePage extends StatelessWidget {
final AudioCache _audioCache = AudioCache(
prefix: 'audio/',
fixedPlayer: AudioPlayer()..setReleaseMode(ReleaseMode.STOP),
);
#override
Widget build(BuildContext context) {
return Scaffold(
body: ElevatedButton(
onPressed: () => _audioCache.play('my_audio.mp3'),
child: Text('Play Audio'),
),
);
}
}
[Answer updated: this approach doesn't work, see comments]
You can use the video_player plugin maintained by the Flutter team. It can reproduce many kinds of media across platforms, including sound files. More specifically, you may want to use the the VideoPlayerController class.
eg. _controller = VideoPlayerController.network('https://www.example.com/soundsFile.wav');
_controller.play();

Resources