Home > Software engineering >  Redirect to appstore in flutter
Redirect to appstore in flutter

Time:07-05

I try to launch a link in my apps, so when user click the button it will redirect to appstore. Here is part of the code:

 InkWell(
                                onTap: () =>launchapp(),
                                child: Text(
                                        "Update",
                                        style: TextStyle(
                                            fontSize: 17),
                                      )
                              )

launchapp() async {
    try {
      if (await canLaunch('https://apps.apple.com/app/MyAppName/idXXXXX')) {
        await launch(url);
      } else {
        throw 'Could not launch $url';
      }
    } catch (e) {
      print(e);
    }
  }

I have tried it in my simulator, but always getting

safari cannot open the page because the address is invalid

is there a way to solve this?

CodePudding user response:

You may change DNS server and select manually option. Go to Apple menu then go to System Preferences > Network. some of google Public DNS server example:: 8.8.8.8 8.8.4.4 I had same issue solved this way.

CodePudding user response:

Add below dependency in AndroidManifest.xml just above of application

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.QUERY_ALL_PACKAGES" />

<queries>
    <intent>
        <action android:name="android.intent.action.VIEW" />
        <category android:name="android.intent.category.BROWSABLE" />
        <data android:scheme="https" />
    </intent>
    <!-- If your app opens https URLs -->
    <intent>
        <action android:name="android.intent.action.VIEW" />
        <data android:scheme="http" />
    </intent>
    <!-- If your app opens https URLs -->
    <intent>
        <action android:name="android.intent.action.VIEW" />
        <data android:scheme="https" />
    </intent>
</queries>

MobileUrl

import 'package:flutter/material.dart';
 import 'package:url_launcher/url_launcher.dart';
    
    class MobileUrl extends StatelessWidget {
      const MobileUrl({Key? key}) : super(key: key);
    
      @override
      Widget build(BuildContext context) {
    
      void  launchapp() async {
          final Uri url = 
         Uri.parse('https://apps.apple.com/app/MyAppName/idXXXXX');
          try {
            if (await canLaunch(url.toString())) {
              await launch(url.toString());
            } else {
              throw 'Could not launch $url';
            }
          } catch (e) {
            print(e);
          }
        }
    
        return Scaffold(
          body: SafeArea(
            child: Center(
              child: Column(
                mainAxisAlignment: MainAxisAlignment.center,
                children: [
                
   
                  ElevatedButton(onPressed: (){
                    launchapp();
                  }, child: Text('Launch app Button'))
    
                ],
              ),
            ),
          ),
        );
      }
    }

CodePudding user response:

launchapp() async {
    try {
      if (await canLaunch("https://apps.apple.com/app/id$appId")) {
        await launch(url);
      } else {
        throw 'Could not launch $url';
      }
    } catch (e) {
      print(e);
    }
  }

$appId is your appId of App Store.

  • Related