iOS 跳轉(zhuǎn)網(wǎng)頁的四種方法

跳轉(zhuǎn)界面 push 展示網(wǎng)頁

1.Safari :

openURL:自帶很多功能 (進(jìn)度條,刷新,前進(jìn),倒退..)就是打開了一個瀏覽器,跳出自己的應(yīng)用

2.UIWebView:

沒有功能,在當(dāng)前應(yīng)用中打開網(wǎng)頁,自己去實(shí)現(xiàn)某些功能,但不能實(shí)現(xiàn)進(jìn)度條功能(有些軟件做了假進(jìn)度條,故意卡到70%不動,加載完成前秒到100%)

3.SFSafariViewController:

iOS9+ 專門用來展示網(wǎng)頁 需求:既想要在當(dāng)前應(yīng)用展示網(wǎng)頁,又想要safari功能

  • 需要導(dǎo)入#import <SafariServices/SafariServices.h>框架
#pragma mark - UICollectionViewDelegate
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{    // 小盒子的點(diǎn)擊事件
    BQLog(@"%@",indexPath);
    // 跳轉(zhuǎn)界面 push 展示網(wǎng)頁
    /*
        1.Safari openURL:自帶很多功能 (進(jìn)度條,刷新,前進(jìn),倒退..)就是打開了一個瀏覽器,跳出自己的應(yīng)用
        2.UIWebView:沒有功能,在當(dāng)前應(yīng)用中打開網(wǎng)頁,自己去實(shí)現(xiàn)某些功能,但不能實(shí)現(xiàn)進(jìn)度條功能
        3.SFSafariViewController:iOS9+ 專門用來展示網(wǎng)頁 需求:既想要在當(dāng)前應(yīng)用展示網(wǎng)頁,又想要safari功能
            需要導(dǎo)入#import <SafariServices/SafariServices.h>框架
        4.WKWebView:iOS8+ (UIWebView升級版本)添加功能:1)監(jiān)聽進(jìn)度條 2)緩存
     */
    BQSquareItem *item = self.squareItems[indexPath.row];
    
    if (![item.url containsString:@"http"]) {
        return;
    }
    
    SFSafariViewController *safariVc = [[SFSafariViewController alloc] initWithURL:[NSURL URLWithString:item.url]];
//    safariVc.delegate = self;
//    self.navigationController.navigationBarHidden = YES;
//    [self.navigationController pushViewController:safariVc animated:YES];
    [self presentViewController:safariVc animated:YES completion:nil]; // 推薦使用modal自動處理 而不是push
}

4.WKWebView:

iOS8+ (UIWebView升級版本)添加功能:1)監(jiān)聽進(jìn)度條 2)緩存

  • 需要手動導(dǎo)入WebKit框架 編譯器默認(rèn)不會導(dǎo)入
- (void)viewDidLoad {
    [super viewDidLoad];
    // 添加WebView
    WKWebView *webView = [[WKWebView alloc] init];
    _webView = webView;
    [self.contentView addSubview:webView];
    
    // 加載網(wǎng)頁
    NSURLRequest *request = [NSURLRequest requestWithURL:self.url];
    [webView loadRequest:request];
    
    // KVO監(jiān)聽屬性改變
    /*
     KVO使用:
        addObserver:觀察者
        forKeyPath:觀察webview哪個屬性
        options:NSKeyValueObservingOptionNew觀察新值改變
     注意點(diǎn):對象銷毀前 一定要記得移除 -dealloc
     */
    [webView addObserver:self forKeyPath:@"canGoBack" options:NSKeyValueObservingOptionNew context:nil];
    [webView addObserver:self forKeyPath:@"canGoForward" options:NSKeyValueObservingOptionNew context:nil];
    [webView addObserver:self forKeyPath:@"title" options:NSKeyValueObservingOptionNew context:nil];
    
    // 進(jìn)度條
    [webView addObserver:self forKeyPath:@"estimatedProgress" options:NSKeyValueObservingOptionNew context:nil];
}

- (void)viewDidLayoutSubviews{
    [super viewDidLayoutSubviews];
    
    _webView.frame = self.contentView.bounds;
}

#pragma mark - KVO
// 只要觀察者有新值改變就會調(diào)用
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context{
    self.backItem.enabled = self.webView.canGoBack;
    self.forwardItem.enabled = self.webView.canGoForward;
    self.title = self.webView.title;
    self.progressView.progress = self.webView.estimatedProgress;
    self.progressView.hidden = self.webView.estimatedProgress>=1;
}

- (void)dealloc {
    [self.webView removeObserver:self forKeyPath:@"canGoBack"];
    [self.webView removeObserver:self forKeyPath:@"canGoForward"];
    [self.webView removeObserver:self forKeyPath:@"title"];
    [self.webView removeObserver:self forKeyPath:@"estimatedProgress"];
}

#pragma mark - 按鈕的點(diǎn)擊事件
- (IBAction)goBack:(id)sender { // 回退
    [self.webView goBack];
}

- (IBAction)goForward:(id)sender {  // 前進(jìn)
    [self.webView goForward];
}

- (IBAction)reload:(id)sender { //刷新
    [self.webView reload];
}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容