sfsafariviewios safaricontrollerr 怎么创建

iOS 9之SFSafariViewController
iOS 9之SFSafariViewController
& & &有时候需要在App内部打开一个网页,例如为了展示公司官网,产品列表信息,Facebook,微博等。以前都是使用 UIWebView,iOS 8引入了WKWebView。但他们都存在各自的一些问题。
&&&& UIWebView:
始祖级别,支持的iOS版本比较多
可支持打开URL,包括各种URL模式,例如 Https,FTP等
可支持打开各种不同文件格式,例如 txt,docx,ppt,,音视频文件等,很多文档阅读器会经常使用这个特性,感兴趣的可以查一下Apple的文档,支持的格式还是挺多,只是不同iOS 版本的支持程度不太一样,使用时请多留意测试确认~
占用内存比较多,尤其是网页中包含比较多CSS+DIV之类内容时,很容易出现内存警告(Memory Warning)
效率低,不灵活,尤其是和 JavaScript交互时
无法清除本地存储数据(Local Storage)
代理(delegate)之间的回调比较麻烦,提供的内容比较低级,尤其是UI部分。如果想自己定制一个类似 Safari 的内嵌浏览器(Browser),那就坑爹无极限了,例如我们PDF Reader系列中的内嵌Browser,自己手动模拟实现Tab切换,底部Tool及各种Menu等,说多了都是泪~~
WKWebView:
iOS 8引入的,比较年轻
在内存和执行效率上要比UIWebView高很多
开放度较高但据说Bug成吨
类似UIWebView,UI定制比较麻烦···
没具体测试使用过,就不继续列举了 L~
好,终于轮到今天的主人公了,SFSafariViewController:
iOS 9引入,更加年轻,意味着是Apple的新菜,总是有什么优势的
也是用来显示网页内容的
这是一个特殊的View Controller,而不是一个单独的 View,和前面两个的区别
在当前App中使用Safari的UI框架展现Web内容,包括相同的地址栏,工具栏等,类似一个内置于App的小型Safari
共享Safari的一些便利特性,包括:相似的用户体验,和Safari共享Cookie,iCloud Web表单数据,密码、证书自动填充,Safari阅读器(Safari Reader)
可定制性比较差,甚至连地址栏都是不可编辑的,只能在init的时候,传入一个URL来指定网页的地址
只能用来展示单个页面,并且有一个完成按钮用来退出
图1&SFSafariViewController演示截图
如果你的App需要显示网页,但是又不想自己去定制浏览器界面的话,可以考虑用SFSafariViewController来试试。从好的方面看,SFSafariViewController也去掉了从App中跳转到Safari的撕裂感,不同App之间切换总是让人感觉麻烦和不舒服。
- (IBAction)onButtonClick:(id)sender
&&& NSString *urlString = @"";
&&& SFSafariViewController *sfViewControllr = [[SFSafariViewController alloc] initWithURL:[NSURL URLWithString:urlString]];
&&& sfViewControllr.delegate =
&&& [self presentViewController:sfViewControllr animated:YES completion:^{
& & & &//...
// Done 按钮
- (void)safariViewControllerDidFinish:(nonnull SFSafariViewController *)controller
&&& [controller dismissViewControllerAnimated:YES completion:nil];
SFSafariViewController 的接口比较少,就不再继续一一列举了。另外一个定制功能在于SFSafariViewControllerDelegate里面的一个方法:
-(NSArray&UIActivity *& *)safariViewController:(SFSafariViewController *)controller activityItemsForURL:(NSURL *)URL title:(nullable NSString *)
这个代理会在用户点击动作(Action)按钮(底部工具栏中间的按钮)的时候调用,可以传入UIActivity的数组,创建添加一些自定义的各类插件式的服务,比如分享到微信,微博什么的。
图2&SFSafariViewController演示截图
SFSafariViewController有保存Cookies的功能,但是貌似不能和Safari浏览器共享,也可能是Beta版的bug
版权所有,转载须浏览作者()及原文出处()。
发表评论:
TA的最新馆藏iOS 9: Getting Started With SFSafariViewController - 推酷
iOS 9: Getting Started With SFSafariViewController
Mobile apps and viewing content on the web are ubiquitous now. For years, iOS developers have been charged with either creating their own web viewing experience inside their app or handing off the URL to Safari. Both of these approaches bring inherent disadvantages that were previously unavoidable.
That's all changed with iOS 9 and the introduction of the SFSafariViewController class.&With it, you can now provide a full web viewing experience inside your app without having to spend important development time providing it.
1. Demo Overview
Before we begin, I'm going to lay out the approach I've taken with the
that goes along with this tutorial. As you'll see later, there really isn't much code involved with using the safari view controller. The real value of the safari view controller comes from understanding when to use it and, more importantly,& why .
Options for Showing Web Content
As of iOS 9, developers have three options to display web content to a user:
Safari: &Use openURL(_:) to show the page inside of safari, forcing the user to leave your application.
Custom Viewing Experience: &You can leverage& WKWebView or UIWebView to create a browsing experience from scratch.
SFSafariViewController : &With& SFSafariViewController , you can use nearly all of&the benefits of viewing web content inside Safari without forcing users to leave your app.
Before iOS 9, the first two options were the only options for developers. Knowing when you had to use one or the other depended on the context of the presented content.&While working on the demo application, we are going to use all three options.
Now that we know how content can be displayed, let's cover why it might be&displayed in an app. On iOS, there are two major use cases for viewing web content.
Custom Web Content: &This is&content not meant for browsing. This could be a report or something similar generated from an API or server. Here, the user is looking at one piece of content and not doing much else.
Viewing Websites:& This is the most common scenario. The user needs to browse the web momentarily to either log in to a service or navigate a website.
Also keep in mind that there is a third use case, web-based authentication. For this tutorial, &we aren't going to focus on that scenario.
Custom Web Content
If the extent of your user's web experience inside of your app falls into the first use case, the safari view controller is probably not what you need.&In those cases, you are displaying content that you own and control, and may need to extensively customize.
If you find your app fits into this scenario, then use WKWebView . It's the successor to& UIWebView and includes&several enhancements, such as utilizing the
. This approach lets you build the entire user interface from scratch. You also have other affordances, such as loading files securely and using& WKWebsiteDataStore for querying cookies.
Viewing Websites
The majority of apps, however, just need to provide a generalized web viewing experience. This is the perfect scenario for the safari view controller. Before iOS 9, developers spent time creating their own user interface for web browsing, which could lead to&problems for users.
The browsing experience is inconsistent across different apps,&which may confuse the user. Some interfaces may also&lack the things users expect, such as a progress bar indicating how much of the page is loaded.
Further, you don't have access to all of Safari's features. This includes the reader view, the iCloud keychain for autofill capabilities, and more. If you wanted to have those features before iOS 9,&you were forced to have the user leave your app entirely by opening the content inside Safari. The SFSafariViewController class&solves every one of these problems.
2. Run the Demo App
To begin, build and run the demo application. You'll be presented with a very minimal user interface that has three options.&Each option corresponds to one of the&methods mentioned earlier to present web content.
3. Opening Content in Safari
The first option we'll demonstrate is the more traditional route, which is handing off the URL to Safari. Open ViewController.swift and notice the urlString property at the top of the file. This will define what's presented in the coming&examples, so feel free to set it to whatever you desire.
private var urlString:String = &&
One important thing to note is that in iOS 9, TLS 1.2 is enforced by default. If the server you are trying to reach doesn't support this, you may see the following error in the console:
There are ways around this, such as adding a key to your app's Info.plist file. This was a change made by Apple to increase security around the web browsing experience.&Moving on, add the following code to the& openInSafari(_:) action:
@IBAction func openInSafari(sender: AnyObject)
let url = NSURL(string: self.urlString)!
UIApplication.sharedApplication().openURL(url)
Build and run the app. When you tap the top button, &Open in safari&, the operating system will leave your app and&open the URL in Safari.
While this option is certainly viable, we've forced the user to leave our app. As developers, ideally we want the experience to stay contained within our app. One improvement iOS 9 has made with this approach, however, is the small back button in the top left:
Tapping this button will take the user back to the app that handed off the URL to Safari. To fix the issue of the user being forced to leave our app, let's move on to the next approach.
4. Opening Content in Webkit or WebView
We'll now&open the same URL inside our app. To do this, we'll use an embedded UIWebView . The logic for this simple web browser is found in&the& CustomWebViewController class.
Since we don't need any of the advanced features of WebKit, we'll just open the page inside a web view. In the ViewController class, replace the code in prepareForSegue(_:sender:) as follows:
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?)
if let navVC = segue.destinationViewController as? UINavigationController
let cwvc = navVC.topViewController as! CustomWebViewController
cwvc.urlString = self.urlString
Go ahead and run the app. Tap the middle button, &Open with webview&, and the page should now load inside the app.
Even though the user stays in the app, the disadvantages with this approach are obvious. Without more development work on our end, there is no loading indication, URL address bar, and other things users expect when browsing the web.&Let's now&use the SFSafariViewController class to solve those issues.
5. Opening Content in Safari View Controller
Before we can use the SFSafariViewController class, we need to import Safari Services . At the top of ViewController.swift , add the following import statement below the import statement for UIKit:
import SafariServices
Next, update the implementation of openWithSafariVC(_:) as shown below:
@IBAction func openWithSafariVC(sender: AnyObject)
let svc = SFSafariViewController(URL: NSURL(string: self.urlString)!)
self.presentViewController(svc, animated: true, completion: nil)
Run the app and tap the bottom button, &Open with safari view controller&, to see the content now being displayed inside a SFSafariViewController instance.
We've now&let the user stay inside of our app and they have all the advantages of Safari. Share sheets are available from the tab bar along with the option to add the page as a favorite or open the content in Safari.
There are a number of interesting configurations to take advantage of. For instance,&we could&let the user easily launch the browser in reader mode by passing true to entersReaderIfAvailable .
@IBAction func openWithSafariVC(sender: AnyObject)
let svc = SFSafariViewController(URL: NSURL(string: self.urlString)!, entersReaderIfAvailable: true)
self.presentViewController(svc, animated: true, completion: nil)
Additionally, the Safari view controller&respects tint color. This makes it easy to keep the branding of your app present while still keeping&Safari's familiar user interface intact.
However, one issue we need to resolve is that the user currently can't dismiss the view controller. Let's fix that now.
6. SFSafariViewControllerDelegate Protocol
To dismiss the view controller, we need to conform to the SFSafariViewControllerDelegate protocol.&Open ViewController.swift and make the ViewController class conform to the& SFSafariViewControllerDelegate protocol.
class ViewController: UIViewController, SFSafariViewControllerDelegate
Next, add the following delegate method to the ViewController class:
func safariViewControllerDidFinish(controller: SFSafariViewController)
controller.dismissViewControllerAnimated(true, completion: nil)
This delegate method is called when the user taps the Done button in the Safari view controller. It should be used to dismiss the view controller and return to your app.
The only thing left to do is assign our view controller to be the delegate of the Safari view controller. Update the implementation of the openWithSafariVC(_:) method as shown below:
@IBAction func openWithSafariVC(sender: AnyObject)
let svc = SFSafariViewController(URL: NSURL(string: self.urlString)!)
svc.delegate = self
self.presentViewController(svc, animated: true, completion: nil)
If you run the app and open the Safari view controller, you'll see that you can now dismiss the Safari view controller by tapping the Done button in the top right.
Conclusion
The Safari view controller is incredibly easy to use. In fact, it's one of the smallest APIs&I've seen with only two initializers and two delegate methods. Even so, it brings all of the features&users expect from Safari over to your app.
What's perhaps just as exciting is that developers will no longer have to spend time creating custom web browsers. Bringing a first class web browsing experience to your app&takes a few lines of code with the SFSafariViewController class.
已发表评论数()
请填写推刊名
描述不能大于100个字符!
权限设置: 公开
仅自己可见
正文不准确
标题不准确
排版有问题
主题不准确
没有分页内容
图片无法显示
视频无法显示
与原文不一致ios开发(60)
始祖级别,支持的iOS版本比较多
可支持打开URL,包括各种URL模式,例如 Https,FTP等
可支持打开各种不同文件格式,例如 txt,docx,ppt,,音视频文件等,很多文档阅读器会经常使用这个特性,感兴趣的可以查一下Apple的文档,支持的格式还是挺多,只是不同iOS 版本的支持程度不太一样,使用时请多留意测试确认~
占用内存比较多,尤其是网页中包含比较多CSS+DIV之类内容时,很容易出现内存警告(Memory Warning)
效率低,不灵活,尤其是和 JavaScript交互时
无法清除本地存储数据(Local Storage)
代理(delegate)之间的回调比较麻烦,提供的内容比较低级,尤其是UI部分。如果想自己定制一个类似 Safari 的内嵌浏览器(Browser),那就坑爹无极限了,例如我们PDF Reader系列中的内嵌Browser,自己手动模拟实现Tab切换,底部Tool及各种Menu等,说多了都是泪~~
iOS 8引入的,比较年轻
在内存和执行效率上要比UIWebView高很多
开放度较高但据说Bug成吨
类似UIWebView,UI定制比较麻烦···
没具体测试使用过,就不继续列举了 L~
SFSafariViewController
iOS 9引入,更加年轻,意味着是Apple的新菜,将来应该会被广泛的使用
也是用来显示网页内容的
这是一个特殊的View Controller,而不是一个单独的 View,和前面两个的区别
在当前App中使用Safari的UI框架展现Web内容,包括相同的地址栏,工具栏等,类似一个内置于App的小型Safari
共享Safari的一些便利特性,包括:相似的用户体验,和Safari共享Cookie,iCloud Web表单数据,密码、证书自动填充,Safari阅读器(Safari Reader)
可定制性比较差,甚至连地址栏都是不可编辑的,只能在init的时候,传入一个URL来指定网页的地址
如果想知道使用SFSafariViewController的优势具体分析,请查看下方文章
这里介绍如何通过SFSafariViewController来获取Safari上网站的cookie。
SFSafariViewController并没有提供给我们直接获取cookie的方法,告诉我们SFSafariViewController与Safari共享了cookie,这样就提供了给我们通过其他方式获取cookie
这边详细介绍测试步骤
1.创建一个index.html文件
name="viewport" content="width=device-width"&
var c = document.
var m = c.match(/name=(\w+)/);
document.writeln("You are " + m[1] + '.');
name = m[1];
document.writeln("You are anonymous.");
name = "";
if (location.search.match(/redirect/)) {
location.href = "svclogintest://name/" +
function saveName() {
document.cookie = 'name=' + document.getElementById('name').value + ';max-age=3600';
location.reload();
type="text" id="name"&
type="submit" value="Save" onclick="saveName();"&
上面的代码主要作用是当用户输入一个名字之后,点击保存(视为登录操作),这是回调saveName方法把用户输入的name存到cookie里面。
var c = document.
var m = c.match(/name=(\w+)/);
document.writeln("You are " + m[1] + '.');
name = m[1];
document.writeln("You are anonymous.");
name = "";
if (location.search.match(/redirect/)) {
location.href = "svclogintest://name/" +
上面的代码会在用户每次访问从cookie里面取出你所存入的内容,然后拼接”svclogintest://name/” + name字符串赋给location.href,这样location.href的内容会被我的app的oc代码获取,也就变相读取了cookie
2.打开终端,找到index.html文件启动apache服务器
python -m SimpleHTTPServer
3.创建OC工程,核心代码如下
ViewController.m
- (void)viewDidLoad {
[super viewDidLoad];
UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(50, 100, 250, 50)];
[button setTitle:@"Open Safari in app" forState:UIControlStateNormal];
[button setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
[button addTarget:self action:@selector(openSafariInApp) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
UIButton *button1 = [[UIButton alloc] initWithFrame:CGRectMake(50, 180, 250, 50)];
[button1 setTitle:@"Open webView in app" forState:UIControlStateNormal];
[button1 setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
[button1 addTarget:self action:@selector(openWebViewInApp) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button1];
-(void)openSafariInApp
NSString *urlString = @"";
SFSafariViewController *sfViewControllr = [[SFSafariViewController alloc] initWithURL:[NSURL URLWithString:urlString]];
sfViewControllr.delegate = self;
[self.navigationController pushViewController:sfViewControllr animated:YES];
-(void)openWebViewInApp
NSString *urlString = @"";
UIWebView *web = [[UIWebView alloc] initWithFrame:self.view.bounds];
[web loadRequest:[[NSURLRequest alloc] initWithURL:[NSURL URLWithString:urlString]]];
UIViewController *controller = [[UIViewController alloc] init];
[controller.view addSubview:web];
[self.navigationController pushViewController:controller animated:YES];
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
参考知识库
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:64959次
积分:1832
积分:1832
排名:第16909名
原创:113篇
转载:51篇
(1)(1)(3)(3)(2)(5)(13)(26)(16)(10)(18)(8)(29)(5)(3)(3)(1)(1)(5)(11)(1)iOS 9学习系列:通过SFSafariViewController提供完整的Web浏览体验
招聘信息:
本文由译者翻译自校对:原文:?当前,移动应用和各种网络上的内容已无所不在。多年以来, iOS开发者都是要么在APP中创建自己的web浏览体验,要么让Safari来打开URL。这两种方法都有不可避免的缺点。不过,现在这一切都改变了, iOS 9引入了SFSafariViewController类。你可以通过它在APP中提供完整的web浏览体验,无需浪费重要的开发时间。1.Demo演示概要开始前,先讲下本教程使用的中用到的几种方法。正如您稍后将看到的,使用Safari的view controller没有涉及太多的代码。Safari view controller的真正价值在于知道何时使用它,更重要的是为什么使用它。显示Web内容的三种方法在iOS 9中,开发者有三种方法来显示Web内容:Safari:使用openURL(_:)在Safari中展示页面,会不得不让用户离开你的应用。自定义浏览体验:你可以利用WKWebView或UIWebView从头开始创建浏览体验。SFSafariViewController :通过SFSafariViewController,你几乎可以使用所有Safari的一些便利特性,而无需让用户离开你的应用。iOS9之前,开发者只能使用前两种方法。了解何时使用其中一个或另一个取决于所呈现内容哪种方法。虽然本教程使用的是demo应用,但我们将使用全部三种方法。现在我们知道了如何显示内容,那来概括下为什么会在应用中展示。在iOS上,查看web内容主要有两种情况。自定义Web内容:这些内容不是用于浏览的。这可能是一个报告或是从API或服务器生成的类似的东西。这里,用户查看的是一块儿内容,而不是做别的。浏览网站:这是最常见的场景。用户需要随时浏览网页来登录到服务或浏览网站。也请记住还有第三种情况--基于Web的验证。在本教程中,我们不会把重点放在这个情景中。自定义Web内容如果用户在APP中的Web体验的属于第一个用例,那么view controller可能不是你需要的。在这种情况下,您将显示自己的控件和内容,并且可能需要大量的自定义。如果你发现你的应用适合这种情况下,使用WKWebView 。它是UIWebView的继任者,并包括几个增强功能,如使用 Nitro Javascript engine。这种方法可以让你从头开始构建整个用户界面。还有其他的功能,如安全加载文件和使用WKWebsiteDataStore查询cookies。浏览网站其实,大多数的应用,只需要提供一个一般的网络浏览体验。这是使用safari view controller的完美场景。在iOS9之前,开发者花时间创建自己的用户界面来浏览网页,用户在使用时也可能遇到一些问题。在不同的应用之间,浏览网页的体验是不同的,这可能会让用户感到疑惑。某些界面可能缺少用户期望的东西,比如表示网页加载进度的进度条。此外,你也不能使用Safari的全部功能。这包括阅读器视图,自动填充功能用到的iCloud钥匙串等等。如果你想在iOS 9 之前实现这些功能,就不得不让用户完全离开你的应用,去到Safari里打开这些网页。。而SFSafariViewController类解决了所有这些问题。2.运行演示应用首先,构建和运行演示应用。你将会看到,有三个选项一个很简单的用户界面。每个选项对应一个前面提到的展示web内容的方法。3. 在Safari中打开网页在第一个选项中,我们会展示一个比较传统的方法,就是让Safari来打开URL。打开ViewController.swift并注意在该文件的顶部urlString属性。它定义的是下面的例子要展示的内容,可以随意将其设置为任何你想要的。private?var?urlString:String?=?""需要注意的一件重要的事情是,在iOS中9 ,会默认使用TSL 1.2。如果您要访问的服务器不支持此功能,你会在控制台中看到以下错误:这种情况有解决办法,比如在你的应用的Info.plist文件中添加一个key。Apple的这个改动是为了提高浏览网络时的安全。接下来,把下面的代码添加到openInSafari ( _ :)方法中:@IBAction?func?openInSafari(sender:?AnyObject)
????let?url?=?NSURL(string:?self.urlString)!
????UIApplication.sharedApplication().openURL(url)
}构建并运行应用。当你点击顶部的"Open in Safari"按钮,操作系统会离开你的应用并在Safari浏览器中打开URL。虽然这种方法是可行的,但是我们不得不让用户离开我们的应用。作为开发人员,理想情况下,我们想让用户一直呆在我们的应用里。iOS 9已经对此做了一些改进,就是在左上角有一个小的返回按钮。点击此按钮将用户返回到用Safari打开URL的应用。为了解决用户不得不离开我们应用的问题,让我们进入下一个方法。4. 在WebKit或WebView中打开网页现在,我们将我们的应用内打开相同的URL 。要做到这一点,我们将嵌入一个UIWebView 。这个简单的网页浏览器的逻辑可以在CustomWebViewController类中找到。因为我们不需要任何的WebKit的高级功能,我们只需在web view中打开。在ViewController类中,把prepareForSegue(_:sender:)替换成下面的代码:override?func?prepareForSegue(segue:?UIStoryboardSegue,?sender:?AnyObject?)
????if?let?navVC?=?segue.destinationViewController?as??UINavigationController
????????let?cwvc?=?navVC.topViewController?as!?CustomWebViewController
????????cwvc.urlString?=?self.urlString
}运行应用。点击中间的"Open with webview"按钮,现在网页就会在应用里加载了?即使用户还呆在我们的应用中,这种方法的缺点是显而易见的。如果我们这边不进行额外的开发,那么界面上就没有进度条、地址栏等用户在浏览网页时需要的东西。现在让我们使用SFSafariViewController类来解决这些问题。5. 在Safari View Controller中打开网页在我们使用SFSafariViewController之前,需要引入Safari Services。在ViewController.swift的顶部,在UIKit的import语句下面添加如下的代码:import?SafariServices下一步,更新openWithSafariVC ( _ :)的实现,如下所示:@IBAction?func?openWithSafariVC(sender:?AnyObject)
????let?svc?=?SFSafariViewController(URL:?NSURL(string:?self.urlString)!)
????self.presentViewController(svc,?animated:?true,?completion:?nil)
}运行应用并点击底部的"Open with safari view controller"按钮,现在能看到网页是在SFSafariViewController实例中展示的。现在,我们既让用户停留在我们的应用中,又让用户能利用Safari的全部优点。在tab bar中,用户可以呼出分享菜单,也能把网页添加到收藏,或者是在Safari中打开网页。还有很多有意思的配置可以利用。比如,我们可以给entersReaderIfAvailable传入true,来让用户能轻松调出阅读器模式:@IBAction?func?openWithSafariVC(sender:?AnyObject)
????let?svc?=?SFSafariViewController(URL:?NSURL(string:?self.urlString)!,?entersReaderIfAvailable:?true)
????self.presentViewController(svc,?animated:?true,?completion:?nil)
}此外, Safari view controller 也使用tint color。这能让你的应用在保持统一风格的同时,也保留熟悉的Safari的UI然而,还有一个问题是目前用户无法关闭这个页面。现在让我们来解决这个问题。6. SFSafariViewControllerDelegate协议要关闭view controller,我们需要遵从SFSafariViewControllerDelegate协议。打开ViewController.swift并使ViewController类遵从SFSafariViewControllerDelegate协议。class?ViewController:?UIViewController,?SFSafariViewControllerDelegate
}接着,将下面的委托方法添加到ViewController类:func?safariViewControllerDidFinish(controller:?SFSafariViewController)
????controller.dismissViewControllerAnimated(true,?completion:?nil)
}当用户点击Safari view controller上的Done按钮时,会调用该委托方法。它应该被用来关闭该view controller,并返回到你的应用。剩下要做的事就是把我们的view controller设为Safari view controller的delegate。更新openWithSafariVC ( _ :)方法的实现,如下所示:@IBAction?func?openWithSafariVC(sender:?AnyObject)
????let?svc?=?SFSafariViewController(URL:?NSURL(string:?self.urlString)!)
????svc.delegate?=?self
????self.presentViewController(svc,?animated:?true,?completion:?nil)
}如果你运行应用并打开Safari view controller,你会看到,你现在可以通过点击右上角的Done按钮来关闭Safari view controller了。结论View controller是非常容易使用。事实上,这是我见过的最小的API之一了,它只有两个初始化方法和两个委托方法。即便如此,它将Safari中用户期望的全部功能带到了你的应用中。同样令人兴奋的是,开发者将不再需要花时间创建自定义的Web浏览器。只需SFSafariViewController的几行代码,就能为你的应用带来一流的web浏览体验。
微信扫一扫
订阅每日移动开发及APP推广热点资讯公众号:CocoaChina
点击量10263点击量9045点击量7872点击量7135点击量7092点击量6124点击量5723点击量5672点击量5653
&2015 Chukong Technologies,Inc.
京公网安备89

我要回帖

更多关于 viewcontroller 的文章

 

随机推荐