[iOSWeb頁] - 相關知識點

近來做了個項目基本上都是加載Web頁,心里那是一個爽啊,都是直接給個鏈接完事,太他媽爽了,根本都不用寫界面,爽爽爽。。。
這種加載Web頁開發的方式有啥優缺點呢?
優點:節省開發時間和成本。當然我們移動端開發基本上搭個架子就完事了
缺點:體驗不好。
隨著項目的推進,為了讓用戶不覺的這是加載Web頁的空殼子,我發現還是有很多問題和知識我是欠缺的

遇到的問題

- 如何在Web 頁內返回上一級
- 如何獲取網頁的title顯示在我們的導航欄上
- 如何關閉在Web 頁長按彈出網頁鏈接的現象(如下圖所示)

F78E03B3043C71A5CC8E5EB5EE290CD0.jpg

- 如何關閉下拉上拉時露底的現象
- 如何獲取即將加載網頁URL
- 如何在加載失敗后不出現404的字樣呢
- 如何修改Web頁提示的Alert title是網址的方法

在寫解決方法之前呢,我們先了解一下WebView傳送門

如何解決遇到的問題

<a id = "1">加載Web頁如何返回上一級</a>
@property (nonatomic, readonly, getter=canGoBack) BOOL canGoBack;
//獲取能否返回上一級
-- (void)goBack;
//返回上一級

if ([_webView canGoBack]) {
        [_webView goBack];
    } else {
        [self.navigationController popViewControllerAnimated:YES];
    }

<a id = "2">- 如何獲取網頁的title顯示在我們的導航欄上</a>
-- (nullable NSString *)stringByEvaluatingJavaScriptFromString:(NSString *)script;
//通過javaScript操作web數據

self.title = [_webView stringByEvaluatingJavaScriptFromString:@"document.title"];//一定要寫@“document.title”

<a id = "3">- 如何關閉在Web 頁長按彈出網頁鏈接的現象</a>

 -(void)webViewDidFinishLoad:(UIWebView*)theWebView{
    [self.webView stringByEvaluatingJavaScriptFromString:@"document.documentElement.style.webkitUserSelect='none';"];
    [self.webView stringByEvaluatingJavaScriptFromString:@"document.documentElement.style.webkitTouchCallout='none';"];
}

<a id = "4">- 如何關閉下拉上拉時露底的現象</a>

-(void)webViewDidFinishLoad:(UIWebView*)theWebView{
    [(UIScrollView *)[[self.webView subviews] objectAtIndex:0] setBounces:NO];
}

<a id = "5">- 如何獲取即將加載網頁URL</a>

 -(BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType{
    
    NSString * requestString = request.URL.absoluteString;
    NSLog(@"這個就是即將加載的URL可以拿到做一些事情:%@",requestString);
    NSLog(@"%ld",(long)navigationType);
    NSHTTPURLResponse *response = nil;
    return YES;
}

<a id = "6">- 如何在加載失敗后不出現404的字樣呢</a>

 -(BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType{

    [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:nil];
    if (response.statusCode != 200) {
        UIImageView *imageV = [[UIImageView alloc] init];
        imageV.frame = [UIScreen mainScreen].bounds;
        imageV.image = [UIImage imageNamed:@"失敗背景圖"];
        [self.webView addSubview:imageV];
        return NO;
    }
    return YES;
}

<a id = "7">- 如何修改Web頁提示的Alert title是網址的方法</a>

1.我們需要創建一個WebView的分類,寫上下面的代碼 運行試試 完美解決

.h

@interface UIWebView (hr_ent)
-(void)webView:(UIWebView *)sender runJavaScriptAlertPanelWithMessage:(NSString *)message initiatedByFrame:(id)frame;
@end

.m

@implementation UIWebView (hr_ent)
-(void)webView:(UIWebView *)sender runJavaScriptAlertPanelWithMessage:(NSString *)message initiatedByFrame:(id)frame {
    UIAlertView* customAlert = [[UIAlertView alloc] initWithTitle:@""
                                                          message:message
                                                         delegate:nil
                                                cancelButtonTitle:@"確定"
                                                otherButtonTitles:nil];
    [customAlert show];
}
@end
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容

  • 這部分主要是開源Java EE框架方面的內容,包括Hibernate、MyBatis、Spring、Spring ...
    雜貨鋪老板閱讀 1,424評論 0 2
  • 1. Java基礎部分 基礎部分的順序:基本語法,類相關的語法,內部類的語法,繼承相關的語法,異常的語法,線程的語...
    子非魚_t_閱讀 31,765評論 18 399
  • 近日,越來越越疑惑一件習以為常的事兒,人作為一個獨立的個體,有時,是不是太過分地用“我覺得”去評價別人的選...
    雯v子閱讀 217評論 0 0
  • 車廂里充斥著一種 難聞的味道 時不時地夾雜著南腔北調 高談闊論著 道聽途說的小道消息 公共場合成了一個人的說笑 個...
    琢玉書生閱讀 256評論 0 1
  • 人總還是要給自己留退路的,或棄醫從文,或棄醫從藝,總要有那么一個兩個一技之長,在混不下去的時候聊以為生,或...
    李燦燦燦燦閱讀 75評論 0 0