iOS App - 在用户选择的浏览器中打开网站
iOS App - Open website in browser choosed by user
我需要开发一个应用程序,将用户重定向到我们的在线商店。
首先我通过 webview 尝试了它,但是 webview 引入了一些错误,例如我无法打开 PDF 文件。
我在 android 中使用 Intent
解决了它:
package de.example.onlineshop;
import android.annotation.SuppressLint;
import android.content.Intent;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class FullscreenActivity extends AppCompatActivity
{
public void openWebPage(String url)
{
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
if (intent.resolveActivity(getPackageManager()) != null) {
startActivity(intent);
}
}
@SuppressLint("ClickableViewAccessibility")
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
openWebPage("https://www.example.com");
finish();
}
}
这很完美。询问用户他想在哪个浏览器中打开我们的网站,然后网站在该应用程序中打开。
它是否也适用于 iOS,最好是 swift?
您可以通过添加以下代码在默认浏览器中打开url,
func openWebPage(urlStr: String)
{
guard let url = URL(string: urlStr) else {
return //be safe
}
if #available(iOS 10.0, *) {
UIApplication.shared.open(url, options: [:], completionHandler: nil)
} else {
UIApplication.shared.openURL(url)
}
}
默认情况下,它在 Safari 中打开,但如果您还有其他浏览器,用户将要求选择浏览器 -
guard let url = URL(string: "some url") else {
return
}
if UIApplication.shared.canOpenURL(url) {
UIApplication.shared.open(url, options: [:], completionHandler: nil)
}
我需要开发一个应用程序,将用户重定向到我们的在线商店。
首先我通过 webview 尝试了它,但是 webview 引入了一些错误,例如我无法打开 PDF 文件。
我在 android 中使用 Intent
解决了它:
package de.example.onlineshop;
import android.annotation.SuppressLint;
import android.content.Intent;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class FullscreenActivity extends AppCompatActivity
{
public void openWebPage(String url)
{
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
if (intent.resolveActivity(getPackageManager()) != null) {
startActivity(intent);
}
}
@SuppressLint("ClickableViewAccessibility")
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
openWebPage("https://www.example.com");
finish();
}
}
这很完美。询问用户他想在哪个浏览器中打开我们的网站,然后网站在该应用程序中打开。
它是否也适用于 iOS,最好是 swift?
您可以通过添加以下代码在默认浏览器中打开url,
func openWebPage(urlStr: String)
{
guard let url = URL(string: urlStr) else {
return //be safe
}
if #available(iOS 10.0, *) {
UIApplication.shared.open(url, options: [:], completionHandler: nil)
} else {
UIApplication.shared.openURL(url)
}
}
默认情况下,它在 Safari 中打开,但如果您还有其他浏览器,用户将要求选择浏览器 -
guard let url = URL(string: "some url") else {
return
}
if UIApplication.shared.canOpenURL(url) {
UIApplication.shared.open(url, options: [:], completionHandler: nil)
}