Search code examples
flutterflutterwebviewpluginurl-launcher

I want to open Amazon associate links in the app with url_launcher


I am currently using url_launcher. "mode: LaunchMode.externalApplication" and if I tap the link I want to show the Amazon app.

But I can't do that.

Probably because I am using Amazon Associates links.

I could not solve the problem.

code

   Future _launchUniversalLinkIos(int index) async {
    final url = reversedList[index].acf!.link.toString();
    try {
      await launchUrl(
        Uri.parse(url),
      );
    } catch (e) {
      debugPrint('Laucnh url Error: $e');
    }
  }
                      PageView(
                        controller: _pageController,
                        onPageChanged: (page) {
                          setState(() {
                            selectedPage = page;
                          });
                        },
                        children: pages.map((image) {
                          return GestureDetector(
                            onTap: () =>
                                // final index = pages.indexOf(image);
                                _launchUniversalLinkIos(
                              pages.indexOf(image),
                            ),
                            child: AspectRatio(
                              aspectRatio: 4 / 3,
                              child: Image.network(
                                image,
                                fit: BoxFit.cover,
                              ),
                            ),
                          );
                        }).toList(),
                      ),

print result flutter: https://amzn.to/xxxxxxxx

info.plist

<key>LSApplicationQueriesSchemes</key>
        <array>
            <string>itms-beta</string>
            <string>itms</string>
        </array>

I did try amzn and com.amazon.mobile.shopping but I can not open amazon app


Solution

  • Try below code your problem has been solved just remove if else block from function:

    Widget:

       GestureDetector(
              
              child: Image.network(
                'https://upload.wikimedia.org/wikipedia/commons/d/de/Amazon_icon.png',
                width: 70,
                height: 70,
              ),
              onTap: () => amazon(),
            ),
    

    Function:

      amazon() async {
        const url = 'https://amzn.to/xxxxxxxx'; // or add your URL here
        await launchUrl(Uri.parse(url));
      }
    

    Add below code into your Info.plist file [myproject/ios/Runner/Info.plist]

    <key>LSApplicationQueriesSchemes</key>
    <array>
        <string>http</string>
        <string>https</string>
    </array>
    

    Result-> image