iOS開発-UIWebView

2634 ワード

一、WebViewの基本使用
1      
    01 webView     ,       ,               。
    02      safai     webView    
    03                ,        +Html5   
    04 webView OC   html           

2     
/*A*        **/
    [self.webView goBack];        
    [self.webView goForward];     
    [self.webView reload];        

    //           
    self.goBackBtn.enabled = webView.canGoBack;
    self.fowardBtn.enabled = webView.canGoForward;

/*B*       **/
    self.webView.scalesPageToFit = YES;         
    self.webView.dataDetectorTypes = UIDataDetectorTypeAll; 
                ,all             、    、   。
    self.webView.scrollView.contentInset = UIEdgeInsetsMake(50, 0, 0, 0);

/*C*      **/
    //               ,  YES        ,  NO         
    //           
    -(BOOL)webView:(UIWebView *)webView 
           shouldStartLoadWithRequest:(NSURLRequest *)request 
           navigationType:(UIWebViewNavigationType)navigationType
    {
        return ![request.URL.absoluteString containsString:@"dushu"];
    }

    //      ,           ,           
    -(void)webViewDidStartLoad:(UIWebView *)webView

    //              
    -(void)webViewDidFinishLoad:(UIWebView *)webView

    //           
    -(void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error

/*D*     -      **/
    NSURL *url = [[NSBundle mainBundle] URLForResource:@"text.html" withExtension:nil];
    [self.webView loadRequest:[NSURLRequest requestWithURL:url]];

二、OCとJSコードの相互調整
01 OC  JS   
    NSString *str = [self.webView stringByEvaluatingJavaScriptFromString:@"sum()"];

02 JS    OC   
        :           
               ,       ,                      ,
            ,                 'abc://',
                  ,                            JS  。
    OC          SEL     ,               
    1)       
    2)            SEL
    3)  performSelect     SEL

03         
    [@"abcdef" hasPrefix:@"A"] //                 ,   A
    //    
    - (NSString *)substringFromIndex:(NSUInteger)from;
    //     ,      
    - (NSArray *)componentsSeparatedByString:(NSString *)separator;
    //    
    - (NSString *)stringByReplacingOccurrencesOfString:(NSString *)target withString:(NSString *)replacement
    // string   SEL

    SEL selector = NSSelectorFromString(sel);

04       
    #pragma clang diagnostic push
    #pragma clang diagnostic ignored "-Warc-performSelector-leaks"
            //-Warc-performSelector-leaks        
            [self performSelector:selector withObject:nil];
    #pragma clang diagnostic pop