1. 程式人生 > >ios開發之--UIWebView全屬性

ios開發之--UIWebView全屬性

ebe excel 武器 秘密武器 可能 自適應 天氣 2個 ntb

最近的項目當中需要用到html和ios的交互,所以就湊空整理一下,所有webView相關的方法和屬性,如有不對的地方,請大家不吝指教!

代碼如下:

1,創建webview並設置代理

UIWebView *webView = [[UIWebView alloc]initWithFrame:self.view.bounds];
    [self.view addSubview:webView];
    webView.delegate = self;

2,加載一個url

    [webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"
http://www.baidu.com"]]];

3,設置相關的屬性

    webView.scalesPageToFit = YES;//自動對頁面進行縮放以適應屏幕比例
    webView.dataDetectorTypes = UIDataDetectorTypePhoneNumber;//檢測功能
[webView sizeToFit];//記得添加此自適應屬性,否則加載會有問題

補充:關於detectsPhoneNumbers方法已廢棄,替代方法為:dataDetectorTypes,枚舉內容如下:

    typedef NS_OPTIONS(NSUInteger, UIDataDetectorTypes) {
        UIDataDetectorTypePhoneNumber                                        
= 1 << 0, // Phone number detection-->檢測電話 UIDataDetectorTypeLink = 1 << 1, // URL detection-->檢測網址和郵箱 UIDataDetectorTypeAddress NS_ENUM_AVAILABLE_IOS(4_0) = 1 << 2, // Street address detection-->檢測街道地址 UIDataDetectorTypeCalendarEvent NS_ENUM_AVAILABLE_IOS(4_0) = 1
<< 3, // Event detection-->檢測事件 UIDataDetectorTypeShipmentTrackingNumber NS_ENUM_AVAILABLE_IOS(10_0) = 1 << 4, // Shipment tracking number detection-->貨物追蹤號碼檢測 UIDataDetectorTypeFlightNumber NS_ENUM_AVAILABLE_IOS(10_0) = 1 << 5, // Flight number detection-->班機號碼檢測 UIDataDetectorTypeLookupSuggestion NS_ENUM_AVAILABLE_IOS(10_0) = 1 << 6, // Information users may want to look up-->用戶可能要查找的信息 UIDataDetectorTypeNone = 0, // Disable detection-->禁用檢測 UIDataDetectorTypeAll = NSUIntegerMax // Enable all types, including types that may be added later-->檢測電話網址和郵箱 }

4,加載本地文件(我的文件都是直接復制到工程根目錄下,add進去的,直接拖進去的,我不用),如圖:

技術分享

a 加載本地pdf文件

    NSURL *fileURL = [[NSBundle mainBundle] URLForResource:@"test.pdf" withExtension:nil];
    [webView loadRequest:[NSURLRequest requestWithURL:fileURL]];

b 加載本地word

NSString *path = [[NSBundle mainBundle] pathForResource:@"Test" ofType:@"docx"];

c 加載本地excel文件

    NSString *path = [[NSBundle mainBundle]pathForResource:@"湘潭物業管理項目計劃清單" ofType:@"xlsx"];
    NSURL *url = [NSURL fileURLWithPath:path];
    [webView loadRequest:[NSURLRequest requestWithURL:url]];

d 加載本地html文件,文件裏面可以寫一下aler()和a標簽

    NSString *path = [[NSBundle mainBundle]pathForResource:@"video的副本" ofType:@"html"];
    NSURL *url = [NSURL fileURLWithPath:path];
    [webView loadRequest:[NSURLRequest requestWithURL:url]];

如下圖:

技術分享

5 加載html5的代碼,以前是在做項目的時候,用來占位用的,在此總結進去,沒有什麽太大的意義

NSString *title=@"韓寒《後會無期》奇葩的吸金3秘籍";
    
    NSString *linkStr=[NSString stringWithFormat:@"<a [email protected]>我的博客</a> <a [email protected]>原文</a>",@"http://blog.csdn.net/wildcatlele",@"http://jincuodao.baijia.baidu.com/article/26059"];
    
    NSString *p1=@"韓寒《後會無期》的吸金能力很讓我驚訝!8月12日影片票房已成功沖破6億大關。而且排片量仍保持10 以上,以日收千萬的速度穩步向七億進軍。";
    
    NSString *p2=@"要知道,《後會無期》不是主流類型片,是一個文藝片。不像《小時代》,是一個商業主流的偶像電影。";

    NSString *image2=[NSString stringWithFormat:@"<img [email protected]  height=‘280‘ width=‘300‘ />",@"http://f.hiphotos.baidu.com/news/w%3D638/sign=78315beeb1fb43161a1f797918a44642/2934349b033b5bb58cb61bdb35d3d539b600bcb5.jpg"];
    
    NSString *p3=@"太奇葩了!有人說,這是中國電影市場的紅利,是粉絲電影的成功。但是,有一部投資3000萬的粉絲電影《我就是我》,有明星,制作也不錯,基本上是慘敗。";
    
    NSString *p4=@"《後會無期》賣的不是好故事,是優越感。特別是針對80、90後的人群,你有沒有發現,看《後會無期》比看《小時代3》有明顯的優越感。故事雖然一般,但是很多人看完後,會在微博、微信上曬照片。所以說,對一個族群靠的不是廣度,而是深度。<br>        很兇殘,值得大家借鑒。韓寒《後會無期》還有什麽秘密武器,歡迎《後會無期》團隊或相關方爆料,直接留言即可,有料的可以送黎萬強親筆簽名的《參與感》一書。";
    //初始化和html字符串
    NSString *htmlURlStr=[NSString stringWithFormat:@"<body style=‘background-color:#EBEBF3‘><h2>%@</h2><p>%@</p> <p>%@ </p> <br><p> %@</p> <p>%@</p>%@<p>%@</p></body>",title,linkStr,p1,p2,p3,image2,p4];
    [webView loadHTMLString:htmlURlStr baseURL:nil];

效果如下:

技術分享

註:記得在plist文件裏面添加https功能:

<key>NSAppTransportSecurity</key>
    <dict>
          <key>NSAllowsArbitraryLoads</key>
          <true/>
    </dict>

6 shouldStartLoadWithRequest代理方法的實現,也就是和js端的交互,代碼如下:

-(BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
    NSURL *url = [request URL];
    NSString *urlStr = [NSString stringWithFormat:@"%@",url];
    NSLog(@"url => %@",urlStr);
    return YES;
}

會在控制臺打印出,鏈接的內容:

技術分享

這樣就可以進行相應的操作了,還有一種混合式開發,可以和後臺互傳參數的方法,下次可以更新上去(不過混合是開發問題比較多,只做過2個項目,以後就沒怎麽開發過了);

備註:html文件裏面的內容,自己寫的,比較亂,僅做參考吧

<!DOCTYPE html>
<html lang="en">

    <head>
        <meta charset="UTF-8">
        <title>hgl的網頁</title>

        <style>
            body {
                background-color: antiquewhite;
            }
            
            h1 {
                background-color: #ff9733;
            }
            
            p {
                background-color: red;
            }
            
            #div1 {
                width: 198px;
                height: 66px;
                padding: 10px;
                border: 1px solid #aaaaaa;
            }
            
            #div2{
                background-color: red;
                width: 80px;
                height: 50px;
                transform: rotate(30deg);
                -moz-transform: rotate(30deg);
                border: solid;
                border-radius: 10px;
                border-color: yellow;
                padding: 10px,5px,5px,5px;
                box-shadow: 10px 10px 5px #B8860B;                
            }
            
            *{
                margin: 0;
                padding: 0;
            }
            
            #div3{
                
            }
            
        </style>

    </head>

    <body>

        <div style="text-align: center">
            <button onclick="playPause()">播放/暫停</button>
            <button onclick="makeBig()">變大</button>
            <button onclick="makeNormal()">正常</button>
            <button onclick="makeSmall()">變小</button>
        </div>

        <video id="video1" width="300" height="200">

            <source src="Video/張韶涵 - 終於.mp4" type="video/mp4">

        </video>

        <article>
            <h1>我是h1標題</h1>
            <p>我是段落我是段落我是段落我是段落我是段落我是段落</p>
        </article>

        <script type="text/javascript">
            var myVideo = document.getElementById("video1")

            function playPause() {

                if(myVideo.pause) {
                    myVideo.play();
                    alert("s");
                } else {
                    myVideo.pause();
                    alert("l");
                }

            }

            function makeBig() {
                myVideo.width = 560;
            }

            function makeNormal() {
                myVideo.width = 300;
            }

            function makeSmall() {
                myVideo.width = 250;
            }
        </script>

        <script>
            function goBack() {
                window.history.back();
            }
        </script>

        <input type="button" value="Back" onclick="goBack()" />

        <img draggable="true" />

        <script type="text/javascript">
            function allowDrop(ev) {
                ev.preventDefault();
            }

            function drag(ev) {
                ev.dataTransfer.setData("Text", ev.target.id);
            }

            function drop(ev) {
                ev.preventDefault();
                var data = ev.dataTransfer.getData("Text");
                ev.target.appendChild(document.getElementById(data));
            }
            
            //返回上一個界面
            function goBack()
            {
                window.history.back();
            }
            
            //前往下一個頁面
            function goForword()
            {
                window.history.forward();
            }
            function hello(){
                alert("hello");
            }
            
        </script>

        <div id="div1" ondrop="drop(event)" ondragover="allowDrop(event)"></div>
        <img id="drag1" src="img/sg的副本 2.gif" draggable="true" ondragstart="drag(event)" width="220" height="69" />
        
        
        <div id="div2">天氣晴朗</div>
        <div id="div3">陽光明媚</div>
        
        <div id="div4">處處好風光</div>
        
        <div><a href="http://www.baidu.com">點擊跳轉</a></div>
        
        <button type="button" onclick="goBack()">返回</button>
        <button type="button" onclick="goForword()">前往</button>
        
        <button type="button" onclick="hello()">點擊</button>
        
    </body>

</html>

以上就是webview相關總結,如果有錯誤的地方,或者更好的思路,希望多多交流!

ios開發之--UIWebView全屬性