UIWebView

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

    // webView继承自scrollView,也设置内容尺寸
//    self.webView.scrollView.contentInset = UIEdgeInsetsMake(20, 0, 0, 0);

    // 网页内容是否适应屏幕尺寸
//    self.webView.scalesPageToFit = YES;

    // 检测各种特殊的字符串:比如电话、网站
    /*
     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
     */
    self.webView.dataDetectorTypes = UIDataDetectorTypeAll;


    // webView也可以加载网页、ppt等等
//    [self.webView loadData:<#(nonnull NSData *)#> MIMEType:<#(nonnull NSString *)#> textEncodingName:<#(nonnull NSString *)#> baseURL:<#(nonnull NSURL *)#>];

//    [self.webView loadHTMLString:@"<html><body><div style=\"color:red;font-size:1000px;border:10px solid blue;\">wu zhuangzhuang</div></body></html>" baseURL:nil];

//    [self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:@"/Users/xiaomage/Desktop/test.pptx"]]];

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

    [self.webView loadRequest:[NSURLRequest requestWithURL:[[NSBundle mainBundle] URLForResource:@"test" withExtension:@"html"]]];
  • 这些方法可以对网页进行操作
- (void)reload;
- (void)stopLoading;

- (void)goBack;
- (void)goForward;
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
    self.gobackBtn.enabled = webView.canGoBack;
    self.fowardBtn.enabled = webView.canGoForward;

}
  • 利用JavaScript获取当前网页的标题
// 利用JavaScript获取当前网页的标题
    self.title = [webView stringByEvaluatingJavaScriptFromString:@"document.title"];
  • 通过这个方法完成JS调用OC
/**
 * 每当webView即将发送一个请求之前,都会调用这个方法
 * 返回YES:允许加载这个请求
 * 返回NO:禁止加载这个请求
 */
/**
 * 通过这个方法完成JS调用OC
 * JS和OC交互的第三方框架:WebViewJavaScriptBridge
 */
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
//    NSLog(@"%@",request.URL);

    // 当前要加载的地址
    NSString *url = request.URL.absoluteString;
    // 自定义的协议头
    NSString *scheme = @"wzz://";

    // 如果加载的地址包含自定义的协议头,说明是要调用OC的方法
    if ([url hasPrefix:scheme]) {

        // 用自定义协议头的长度来切割加载的地址获得协议头后面的内容 sendMessage_number_?10086&11111
        NSString *path = [url substringFromIndex:scheme.length];

        // 利用?来切割方法名
        NSArray *subPath = [path componentsSeparatedByString:@"?"];

        // 取出subPath第一元素,把_改为:获得方法名

        NSString *methodName = [[subPath firstObject] stringByReplacingOccurrencesOfString:@"_" withString:@":"];

        // 获得参数
        NSString *parma = [subPath lastObject];
        NSArray *subParmas = nil;

        // 判断有没有参数
        if (subPath.count == 2 || [parma containsString:@"&"]) {

            subParmas = [parma componentsSeparatedByString:@"&"];
        }

        // 取出前面两个参数
        NSString *firstParma = [subParmas firstObject];
        NSString *secondParma = subParmas.count <= 1 ? nil :  [subParmas lastObject];

        // 调用方法
        [self performSelector:NSSelectorFromString(methodName) withObject:firstParma withObject:secondParma];

        return NO;
    }

    NSLog(@"Want Load Other Request");

    return YES;
}
  • 为NSObject添加分类
#import "NSObject+Extension.h"

@implementation NSObject (Extension)
/*
 * 这个分类用来调用方法,并传入参数
 */
- (id)performSelector:(SEL)selector withObjects:(NSArray *)objects
{
    // 方法签名(签名的描述)
    NSMethodSignature *signature = [[self class] instanceMethodSignatureForSelector:selector];

    // 判断是否signature有没有值
    if (! signature) {

        // 没有值就给警告,防止程序闪退
        // 第一种
//        @throw [NSException exceptionWithName:@"强" reason:@"方法找不到" userInfo:nil];
        // 第二种
        [NSException raise:@"强" format:@"%@ 方法找不到",NSStringFromSelector(selector)];
    }

    // NSInvocation : 利用一个NSInvocation对象包装一次方法调用(方法调用者、方法名、方法参数、方法返回值)
    // You must set the receiver’s target, selector, and argument values before calling this method.
    NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:signature];
    // 设置invocation信息
    invocation.target = self;
    invocation.selector = selector;

    // 设置参数
    // signature.numberOfArguments : 里面包含了self、_cmd
    NSInteger paramsCount = signature.numberOfArguments - 2;

    // 获得参数个数
    paramsCount = MIN(paramsCount, objects.count);

    for (NSUInteger i = 0; i < paramsCount; i++) {

        id object = objects[i];

        // 参数值为空,防止报错
        if ([object isKindOfClass:[NSNull class]]) continue;

        [invocation setArgument:&object atIndex:i + 2];
    }

    // 调用方法
    [invocation invoke];

    // 获取返回值
    id returnValue = nil;
    // methodReturnLength 没有返回值时,为0
    if (signature.methodReturnLength) { // 有返回值类型,才去获得返回值
        // 这个方法会将返回值传到这个参数
        [invocation getReturnValue:&returnValue];
    }

    return returnValue;
}
@end
  • 强制去除警告信息
/ 强制去除警告
//#pragma clang diagnostic push

#pragma clang diagnostic ignored "-Warc-performSelector-leaks"// Wincompatible-pointer-types 警告的类型

// 在这里写会发出警告的代码

//#pragma clang diagnostic pop

results matching ""

    No results matching ""