如何启动iOSApp

问题描述 投票:0回答:11

在设置页面上,我想包括三个链接到

    我的应用程序支持网站
  • Youtube应用教程
  • 我的主要网站(即:与戴尔·迪特里奇(Dale Dietrich)标签的“创建”。)
  • 我已经搜索了这个网站和网络和我的文档,我没有发现显而易见的。
注:

我不想在我的应用程序中打开网页。我只想将链接发送到Safari,该链接在那里打开。我已经看到许多应用程序在其设置页面上执行相同的操作,因此必须有可能。

我所做的就是我所做的:

ios url mobile-safari
11个回答
399
投票

- (IBAction)openDaleDietrichDotCom:(id)sender;

  1. I在设置页面上添加了一个Uibutton,其中包含我要链接到的文本。

    i适当地将按钮连接到文件所有者中的ibaction。
    
  2. 然后实现以下内容:

  3. 对象c
  4. - (IBAction)openDaleDietrichDotCom:(id)sender { [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"http://www.daledietrich.com"]]; }

Swift

(ibaction在ViewController中,而不是标题文件)

if let link = URL(string: "https://yoursite.com") {
  UIApplication.shared.open(link)
}

注意,我们不需要逃脱字符串和/或地址,例如:

let myNormalString = "https://example.com"; let myEscapedString = myNormalString.addingPercentEncoding(withAllowedCharacters: .urlHostAllowed)!

实际上,逃脱可能导致开放失败。

swift语法:

UIApplication.sharedApplication().openURL(NSURL(string:"http://www.reddit.com/")!)

iOS 9.3和更早的新闻迅速语法

在某些新版本的Swift(可能是Swift 2?)中,uiapplication.sharedapplication()现在是Uiapplication.shared(可以更好地利用我猜测的计算属性)。另外,URL不再隐式转换为nsurl,必须明确转换为!
UIApplication.sharedApplication.openURL(NSURL(string:"http://www.reddit.com/") as! URL)

172
投票

iOS 10.0开始新的Swift语法

openURL方法已被弃用并用更通用的方法替换,该方法采用了选项对象和一个异步完成处理程序,从iOS 10.0
开始

UIApplication.shared.open(NSURL(string:"http://www.reddit.com/")! as URL)

在此需要检查一个检查,即可通过设备或模拟器打开URL是否打开。因为有时(模拟器中的多数)我发现它会导致崩溃。

对象c


NSURL *url = [NSURL URLWithString:@"some url"]; if ([[UIApplication sharedApplication] canOpenURL:url]) { [[UIApplication sharedApplication] openURL:url]; }

Swift2.0

let url : NSURL = NSURL(string: "some url")!
if UIApplication.sharedApplication().canOpenURL(url) {
     UIApplication.sharedApplication().openURL(url)
}

55
投票
Swift4.2

guard let url = URL(string: "some url") else { return } if UIApplication.shared.canOpenURL(url) { UIApplication.shared.open(url, options: [:], completionHandler: nil) }

查看uiapplication的

-openURL:方法。它应该允许您将NSURL实例传递给系统,该实例将确定要在其中打开的应用程序并启动该应用程序。 (请记住,您可能需要首先检查

-canOpenURL:
,以防万一无法通过系统上安装的应用程序处理该URL  - 尽管这可能不是Pline
http://

链接的问题。)

,如果您不确定提供的URL是否有一个方案:

21
投票
text

未弃用的Objective-C版本将是:

NSString* text = @"www.apple.com";
NSURL*    url  = [[NSURL alloc] initWithString:text];

if (url.scheme.length == 0)
{
    text = [@"http://" stringByAppendingString:text];
    url  = [[NSURL alloc] initWithString:text];
}

[[UIApplication sharedApplication] openURL:url];


17
投票
更新以避免折旧:

[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"http://apple.com"] options:@{} completionHandler:nil];

因此,我们将不得不使用此初始化器:
SFSafariViewController(url: url, entersReaderIfAvailable: true) // was deprecated in iOS 11.0

17
投票
就像以下:

SFSafariViewController(url: URL, configuration: SFSafariViewController.Configuration)


12
投票
Swift3

用完成按钮的解决方案 不要忘记

if let url = URL(string: "http://www.yoururl.com/") { let config = SFSafariViewController.Configuration() config.entersReaderIfAvailable = true let vc = SFSafariViewController(url: url, configuration: config) present(vc, animated: true) }

import SafariServices


Swift3.0



if let url = URL(string: "http://www.yoururl.com/") { let vc = SFSafariViewController(url: url, entersReaderIfAvailable: true) present(vc, animated: true) }

这支持运行iOS旧版本的设备以及

由于iOS 10.0以来,此答案被弃用,因此更好的答案是:

if let url = URL(string: "https://www.reddit.com") {
    if #available(iOS 10.0, *) {
        UIApplication.shared.open(url, options: [:])
    } else {
        UIApplication.shared.openURL(url)
    }
}

thouge en repocients-c
if #available(iOS 10.0, *) {
    UIApplication.shared.open(url, options: [:], completionHandler: nil)
}else{
    UIApplication.shared.openURL(url)
}

11
投票

swift 5:

[[UIApplication sharedApplication] openURL:@"url string" options:@{} completionHandler:^(BOOL success) { if (success) { NSLog(@"Opened url"); } }];
usage:

func open(scheme: String) { if let url = URL(string: scheme) { if #available(iOS 10, *) { UIApplication.shared.open(url, options: [:], completionHandler: { (success) in print("Open \(scheme): \(success)") }) } else { let success = UIApplication.shared.openURL(url) print("Open \(scheme): \(success)") } } }

5
投票
参考:

ios10

中的openurl
    

ios 10.0中对openurl(

:)

4
投票
oopen(

:options:postion:pleastionhandler:)

使用swift进行示例 这将在Safari中打开“

Https://apple.com
”。

open(scheme: "http://www.bing.com")

https://developer.apple.com/reference/uikit/uiapplication/1648685-open


3
投票

if let url = URL(string: "https://apple.com") { if UIApplication.shared.canOpenURL(url) { UIApplication.shared.open(url, options: [:], completionHandler: nil) } } trone this: if let url = URL(string: "https://www.google.com") { UIApplication.shared.open(url, options: [:]) } 在Swift 1.2,尝试以下操作:

NSString *URL = @"xyz.com"; if([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:URL]]) { [[UIApplication sharedApplication] openURL:[NSURL URLWithString:URL]]; }
Swift4解决方案:

let pth = "http://www.google.com" if let url = NSURL(string: pth){ UIApplication.sharedApplication().openURL(url)
    

© www.soinside.com 2019 - 2024. All rights reserved.