項(xiàng)目中可能會(huì)有這樣的需求:
1.單純的展示一個(gè)網(wǎng)頁(yè)來(lái)介紹說(shuō)明內(nèi)容; 一般我們都會(huì)使用UIWebViewController 進(jìn)行加載,有點(diǎn)麻煩性能也不好。
2.不用跳轉(zhuǎn)到Safari,就有了Safari瀏覽器完全功能
許多使用者想要在他們的 App 中使用完整的 Safari 瀏覽器功能,但是又不希望強(qiáng)制使用者跳離 App 去使用 Safari 瀏覽器(這會(huì)影響使用者留存率),而且能夠?qū)⒕W(wǎng)頁(yè)內(nèi)容整合到 Safari 瀏覽器之中。
這時(shí)候 SFSafariViewController 就是你的最佳選擇。 但是只能在iOS 9系統(tǒng)之后才可以使用。
步驟:
1.首先導(dǎo)入:
#import <SafariServices/SafariServices.h>
2.初始化瀏覽器
//加載一個(gè)url,是否啟用閱讀器功能 **只能加載標(biāo)準(zhǔn)的http https URL**, 不然會(huì)崩潰
SFSafariViewController *safariVC = [[SFSafariViewController alloc] initWithURL:[NSURL URLWithString:@"https://www.baidu.com"] entersReaderIfAvailable:YES];
safariVC.delegate = self;
[self presentViewController:safariVC animated:YES completion:nil];
3.引入代理 SFSafariViewControllerDelegate
entersReaderIfAvailable:說(shuō)明:是否使用閱讀器功能。如想要顯示的是 Wikipedia 的內(nèi)容,要使用的是閱讀器( Reader )的功能。閱讀器是 Safari 中一個(gè)很方便的功能,可以從網(wǎng)站截取并顯示重要的內(nèi)容。
對(duì)應(yīng)的代理方法解釋
@optional
說(shuō)明:主要使用這兩個(gè)代理方法,
/*! @abstract Delegate callback called when the user taps the Done button. Upon this call, the view controller is dismissed modally.
說(shuō)明:不用實(shí)現(xiàn)這個(gè)代理方法,點(diǎn)擊Done 也可以dismiss SFSafariViewController 親測(cè)可以的
*/
- (void)safariViewControllerDidFinish:(SFSafariViewController *)controller;
/*! @abstract Invoked when the initial URL load is complete.
@param didLoadSuccessfully YES if loading completed successfully, NO if loading failed.
@discussion This method is invoked when SFSafariViewController completes the loading of the URL that you pass to its initializer. It is not invoked for any subsequent page loads in the same SFSafariViewController instance.
說(shuō)明: 加載完成可以做一些你想做的事情。
*/
- (void)safariViewController:(SFSafariViewController *)controller didCompleteInitialLoad:(BOOL)didLoadSuccessfully;