小男孩‘自慰网亚洲一区二区,亚洲一级在线播放毛片,亚洲中文字幕av每天更新,黄aⅴ永久免费无码,91成人午夜在线精品,色网站免费在线观看,亚洲欧洲wwwww在线观看

分享

UIWebView

 螢火與皓月 2016-08-23
目錄
    1.1 加載某個(gè)網(wǎng)頁(yè)
    1.2 設(shè)置ua
    1.3 獲取標(biāo)題
    1.4 獲取當(dāng)前頁(yè)面URL
    1.5 清除頁(yè)面緩存
    1.6 停止加載
    1.7 獲取Scheme
    1.8 返回添加關(guān)閉按鈕
    1.9 檢測(cè)頁(yè)面上的電話
    1.10 webView 禁止?jié)L動(dòng)
    1.11 獲取HTML高度
    1.12 js調(diào)用oc方法
    1.13 oc調(diào)用js方法
1.1 加載頁(yè)面

1.加載某個(gè)URL

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:
[NSURL URLWithString:self.url]];

self.webView.scalesPageToFit = YES;

[self.webView loadRequest:request];


2.加載本地文件

NSURL* url = [NSURL fileURLWithPath:filePath]; // 需要加載的文件路徑

NSURLRequest* request = [NSURLRequest requestWithURL:url];

[webView loadRequest:request];
1.2 設(shè)置ua

//通過(guò)ua,可以區(qū)分哪個(gè)應(yīng)用訪問(wèn)了當(dāng)前網(wǎng)頁(yè)。
//需求:移動(dòng)端隱藏H5頂部狀態(tài)欄,使用原生的NavgationBar,PC端正常顯示。

NSString *userAgent = [self.webView stringByEvaluatingJavaScriptFromString:
@"navigator.userAgent"];

NSString *executableFile = @"xxxx"; 

NSString *version = [[[NSBundle mainBundle] infoDictionary] objectForKey:
(NSString *)kCFBundleVersionKey];

NSString *ua = [NSString stringWithFormat:@"%@ %@ %@", executableFile,
 version,userAgent];

[[NSUserDefaults standardUserDefaults] 
registerDefaults:@{@"UserAgent" : ua, @"User-Agent" : ua}];
1.3 獲取標(biāo)題

self.webTitle = [webView stringByEvaluatingJavaScriptFromString:
@"document.title"];

[self setNavigationItemTitle:self.webTitle];
1.4 獲取當(dāng)前頁(yè)面URL

//分享時(shí)可能會(huì)需要,動(dòng)態(tài)URL。
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request
navigationType:(UIWebViewNavigationType)navigationType
{
    self.shareUrl = [request.URL description];
    return YES;
}
1.5 清除頁(yè)面緩存

NSHTTPCookie *cookie;

NSHTTPCookieStorage *storage = [NSHTTPCookieStorage sharedHTTPCookieStorage];

for (cookie in [storage cookies])
{
    [storage deleteCookie:cookie];
}
1.6 停止加載

[self.mainWebView stopLoading];
1.7 獲取Scheme

需求:登錄頁(yè)面,自定義SSO協(xié)議,當(dāng)各app端拿到自己規(guī)定的scheme,停止頁(yè)面加載,拿到對(duì)應(yīng)數(shù)據(jù)。

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
    NSLog(@"%@",request.URL.scheme);
    return YES;
}
1.8 返回添加關(guān)閉按鈕

需求:當(dāng)點(diǎn)擊進(jìn)入兩次以上網(wǎng)頁(yè),想直接返回app,那么就需要一個(gè)關(guān)閉按鈕
- (void)leftBarButtonClick:(id)sender
{

    if (self.webView.canGoBack)

    {

        [self.webView goBack];

        [self showWebCloseButton];

    }
    else
    {

        [self popViewController];

    }

}
1.9 檢測(cè)頁(yè)面上的電話

1.檢測(cè)網(wǎng)頁(yè)上的電話號(hào)碼,點(diǎn)擊可撥打

webView.detectsPhoneNumbers = YES;


2.自動(dòng)識(shí)別 網(wǎng)址,電話等

webView.dataDetectorTypes = UIDataDetectorTypeAll;
1.10 webView 禁止?jié)L動(dòng)

當(dāng)把webView放在tableView上,手勢(shì)會(huì)沖突,這時(shí)需要禁止webView滾動(dòng)

webView.scrollView.scrollEnabled=NO;
1.11 獲取HTML高度

獲取HTML高度后,可以設(shè)置webView高度,然后拼接在tableView上。

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

    CGRect frame = webView.frame;

    frame.size.height = webView.scrollView.contentSize.height;

    webView.frame = frame;

    self.tableView reloadData];

}
1.12  JS 調(diào)用 OC 方法

  需求:一個(gè)H5抽獎(jiǎng)運(yùn)營(yíng)活動(dòng),促進(jìn)老用戶升級(jí)到App最新版本。

  最新版本:1.4.0~

  解決方案:oc 獲取到當(dāng)前App版本號(hào),H5調(diào)用判斷是否為1.4.0版本。
  如果是,用戶抽獎(jiǎng),否則提示升級(jí)。

  實(shí)戰(zhàn):規(guī)定
  規(guī)定名稱(chēng):JSInterFace
  規(guī)定方法:- (nonnull NSString *)getVersionName;

  第一步:導(dǎo)入 JavaScriptCore.framework ,如圖1。

  第二步:導(dǎo)入頭文件 #import <JavaScriptCore/JavaScriptCore.h>

  第三步:創(chuàng)建webView,并加載到視圖。

  第四步:建立 JSContext 橋梁。

  @property (nonatomic, strong) JSContext *jsContext;

  - (void)webViewDidFinishLoad:(UIWebView *)webView{
    //建立連接
    self.jsContext = (JSContext *)[webView valueForKeyPath:@"documentView.webView.mainFrame.javaScriptContext"];

    self.jsContext.exceptionHandler = ^(JSContext *con, JSValue *exception) {
        NSLog(@"%@", exception);
        con.exception = exception;
    };
    //創(chuàng)建對(duì)象,處理來(lái)自JS調(diào)用的類(lèi)
    JSInterFace *interFace = [[JSInterFace alloc] init];
    self.jsContext[@"JSInterFace"]= interFace;

}
  第五步:寫(xiě) JSInterFace類(lèi)
  JSInterFace.h

  #import <Foundation/Foundation.h>

  @class JSInterFace;

  @import JavaScriptCore;

  @protocol EPJSExport <JSExport>

  - (nonnull NSString *)getVersionName;

  @end


  @interface JSInterFace : NSObject <EPJSExport>


  @end


  JSInterFace.m

  #import "JSInterFace.h"

  @implementation JSInterFace

  - (nonnull NSString *)getVersionName
  {

    //這里打斷點(diǎn)就可以查看是否被調(diào)用了,
    //如需回調(diào)到控制器實(shí)現(xiàn)功能,可選用block或者代理。
      return (NSString *)[[NSBundle mainBundle]   
    objectForInfoDictionaryKey:@"CFBundleVersion"];
  }

@end

如需Demo,請(qǐng)關(guān)注后聯(lián)系作者~

圖1


屏幕快照 2016-08-23 上午10.54.13.png
1.13 oc調(diào)用js方法

這個(gè)場(chǎng)景非常少見(jiàn),作者詢問(wèn)了幾個(gè)H5開(kāi)發(fā),基本都沒(méi)有用到這個(gè)場(chǎng)景。我能想到的就是App外殼包裝的H5,遇到異常的時(shí)候,做出相應(yīng)的提示而已。

第一步:導(dǎo)入 JavaScriptCore.framework ,如圖1。

第二步:導(dǎo)入頭文件 #import <JavaScriptCore/JavaScriptCore.h>

第三步:創(chuàng)建webView,并加載到視圖。

第四步:建立 JSContext 橋梁。

#import "ViewController.h"

#import <JavaScriptCore/JavaScriptCore.h>

#define SCREEN_WIDTH [UIScreen mainScreen].bounds.size.width //屏幕寬度

#define SCREEN_HEIGHT [UIScreen mainScreen].bounds.size.height //屏幕高度

@interface ViewController ()<UIWebViewDelegate>

@property (nonatomic , strong)UIWebView *mainWebView;

@end

@implementation ViewController

- (void)viewDidLoad {

    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    self.mainWebView = [[UIWebView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
    self.mainWebView.delegate = self;
    NSURL *url = [NSURL URLWithString:@"http://www.jianshu.com/users/d10b02ea2d91/latest_articles"];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];

    self.mainWebView.scalesPageToFit = YES;

    [self.mainWebView loadRequest:request];
    [self.view addSubview:self.mainWebView];

}
-(void)webViewDidFinishLoad:(UIWebView *)webView
{
    //網(wǎng)頁(yè)加載完成調(diào)用此方法
    //首先創(chuàng)建JSContext 對(duì)象
    JSContext *context=[webView valueForKeyPath:@"documentView.webView.mainFrame.javaScriptContext"];
    OC 調(diào)用 JS alert 彈框
    NSString *alertJS=@"alert('這個(gè)博客非常好')";
    [context evaluateScript:alertJS];//通過(guò)oc方法調(diào)用js的alert

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

    本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購(gòu)買(mǎi)等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評(píng)論

    發(fā)表

    請(qǐng)遵守用戶 評(píng)論公約

    類(lèi)似文章 更多