源码0604-02-了解-网页开发

 

//  ViewController.m
//  02-了解-网页开发
#import "ViewController.h"
//#import <JavaScriptCore/JavaScriptCore.h>

@interface ViewController () <UIWebViewDelegate>
@property (weak, nonatomic) IBOutlet UIWebView *webView;

@end

@implementation ViewController

/**
 iOS : Xcode
 Java : eclipse\MyEclipse
 Android : eclipse\MyEclipse\Android Studio
 网页(前端):Dreamweaver(美工)、Sublime(前端)、WebStorm(前端)
 
 iOS App == Native + HTML5
 
 完整的网页组成:
 HTML:内容(文字、图片)
 CSS:美化、样式
 JS:动态效果、事件处理、跟用户进行交互
 */

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self.webView loadRequest:[NSURLRequest requestWithURL:[[NSBundle mainBundle] URLForResource:@"index" withExtension:@"html"]]];
}

- (void)call
{
    NSLog(@"%s", __func__);
//    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"tel://10086"]];
}

- (void)sendMessage
{
    NSLog(@"%s", __func__);
}

- (void)openCamera
{
    NSLog(@"%s", __func__);
}

#pragma mark - <UIWebViewDelegate>
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
//    [webView stringByEvaluatingJavaScriptFromString:@"alert(100);"];
    
    // 利用JS获得当前网页的标题
//    self.title = [webView stringByEvaluatingJavaScriptFromString:@"document.title;"];
    
//    NSString *result = [webView stringByEvaluatingJavaScriptFromString:@"login();"];
//    NSLog(@"%@", result);
}

/**
 * 通过这个方法完成JS调用OC
 * JS和OC交互的第三方框架:WebViewJavaScriptBridge
 */
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
    NSString *url = request.URL.absoluteString;
    NSString *scheme = @"xmg://";
    if ([url hasPrefix:scheme]) {
        
        NSString *methodName = [url substringFromIndex:scheme.length];
        [self performSelector:NSSelectorFromString(methodName) withObject:nil];
        
        return NO;
    }
    
    NSLog(@"想加载其他请求,不是想调用OC的方法");
    
    return YES;
}

@end

03-了解-网页开发(JS-OC)

//
//  ViewController.m
//  02-了解-网页开发
#import "ViewController.h"

// 去除Xcode编译警告
//#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Warc-performSelector-leaks"
//#pragma clang diagnostic pop

//#import <JavaScriptCore/JavaScriptCore.h>

@interface ViewController () <UIWebViewDelegate>
@property (weak, nonatomic) IBOutlet UIWebView *webView;

@end

@implementation ViewController

/**
 iOS : Xcode
 Java : eclipse\MyEclipse
 Android : eclipse\MyEclipse\Android Studio
 网页(前端):Dreamweaver(美工)、Sublime(前端)、WebStorm(前端)
 
 iOS App == Native + HTML5
 
 完整的网页组成:
 HTML:内容(文字、图片)
 CSS:美化、样式
 JS:动态效果、事件处理、跟用户进行交互
 */

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self.webView loadRequest:[NSURLRequest requestWithURL:[[NSBundle mainBundle] URLForResource:@"index" withExtension:@"html"]]];
}

- (void)call:(NSString *)number
{
    NSLog(@"%s %@", __func__, number);
}


- (void)sendMessage:(NSString *)number number2:(NSString *)number2
{
    NSLog(@"%s %@ %@", __func__, number, number2);
}

- (void)openCamera
{
    NSLog(@"%s", __func__);
}

#pragma mark - <UIWebViewDelegate>
/**
 * 通过这个方法完成JS调用OC
 * JS和OC交互的第三方框架:WebViewJavaScriptBridge
 */
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
    // url == xmg://sendMessage_?200
    NSString *url = request.URL.absoluteString;
    NSString *scheme = @"xmg://";
    if ([url hasPrefix:scheme]) {
        // 获得协议后面的路径  path == sendMessage_number2_?200&300
        NSString *path = [url substringFromIndex:scheme.length];
        // 利用?切割路径
        NSArray *subpaths = [path componentsSeparatedByString:@"?"];
        // 方法名 methodName == sendMessage:number2:
        NSString *methodName = [[subpaths firstObject] stringByReplacingOccurrencesOfString:@"_" withString:@":"];
        // 参数  200&300
        NSString *param = [subpaths lastObject];
        NSArray *subparams = nil;
        if (subpaths.count == 2 || [param containsString:@"&"]) {
            subparams = [param componentsSeparatedByString:@"&"];
        }
        // 取出前面的2个参数
        NSString *firstParam = [subparams firstObject];
        NSString *secondParam = subparams.count <= 1 ? nil : [subparams lastObject];
        
        [self performSelector:NSSelectorFromString(methodName) withObject:firstParam withObject:secondParam];
        
        return NO;
    }
    
    NSLog(@"想加载其他请求,不是想调用OC的方法");
    
    return YES;
}

@end

 

04-了解-网页开发(JS-OC)

//
//  ViewController.m
//  02-了解-网页开发
#import "ViewController.h"
#import "NSObject+Extension.h"

// 去除Xcode编译警告
//#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Warc-performSelector-leaks"
//#pragma clang diagnostic pop

//#import <JavaScriptCore/JavaScriptCore.h>

@interface ViewController () <UIWebViewDelegate>
@property (weak, nonatomic) IBOutlet UIWebView *webView;

@end

@implementation ViewController

/**
 iOS : Xcode
 Java : eclipse\MyEclipse
 Android : eclipse\MyEclipse\Android Studio
 网页(前端):Dreamweaver(美工)、Sublime(前端)、WebStorm(前端)
 
 iOS App == Native + HTML5
 
 完整的网页组成:
 HTML:内容(文字、图片)
 CSS:美化、样式
 JS:动态效果、事件处理、跟用户进行交互
 */

// 程序崩溃分析报告

- (void)viewDidLoad {
    [super viewDidLoad];
    
//    @try {
//        @[][0];
//        
//        NSLog(@"--------2222");
//    }
//    @catch (NSException *exception) {
//        NSLog(@"-----代码有异常----%@", exception);
//    }
//    @finally {
//        NSLog(@"---------33333-");
//    }
    
    
    NSLog(@"-------44444");
    
//    [self.webView loadRequest:[NSURLRequest requestWithURL:[[NSBundle mainBundle] URLForResource:@"index" withExtension:@"html"]]];
    
//    [self performSelector:@selector(abc) withObjects:nil];
//    [self performSelector:@selector(abc)];
    
    NSString *result = [self performSelector:@selector(openCamera) withObjects:@[@1, @"432"]];
    NSLog(@"%@", result);
    
//    NSLog(@"%@", [self openCamera]);
    
    [self performSelector:@selector(call:) withObjects:nil];
    
//    [self call:@"10086"];
//    
//    [self performSelector:@selector(sendMessage:number2:number3:) withObjects:@[
//                                                                                [NSNull null],
//                                                                        [NSNull null],
//                                                                        @"10010"
//                                                                        ]];
//    [self sendMessage:nil number2:nil number3:@"10010"];
}

- (void)call:(NSString *)number
{
    NSLog(@"%s %@", __func__, number);
}

- (void)sendMessage:(NSString *)number number2:(NSString *)number2 number3:(NSString *)number3
{
    NSLog(@"%s %@ %@ %@", __func__, number, number2, number3);
}

- (NSString *)openCamera
{
    NSLog(@"%s", __func__);
    
    return @"相机";
}

#pragma mark - <UIWebViewDelegate>
/**
 * 通过这个方法完成JS调用OC
 * JS和OC交互的第三方框架:WebViewJavaScriptBridge
 */
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
    // url == xmg://sendMessage_?200
    NSString *url = request.URL.absoluteString;
    NSString *scheme = @"xmg://";
    if ([url hasPrefix:scheme]) {
        // 获得协议后面的路径  path == sendMessage_number2_?200&300
        NSString *path = [url substringFromIndex:scheme.length];
        // 利用?切割路径
        NSArray *subpaths = [path componentsSeparatedByString:@"?"];
        // 方法名 methodName == sendMessage:number2:
        NSString *methodName = [[subpaths firstObject] stringByReplacingOccurrencesOfString:@"_" withString:@":"];
        // 参数  200&300
        NSArray *params = nil;
        if (subpaths.count == 2) {
            params = [[subpaths lastObject] componentsSeparatedByString:@"&"];
        }
        // 调用
        [self performSelector:NSSelectorFromString(methodName) withObjects:params];
        
        return NO;
    }
    
    NSLog(@"想加载其他请求,不是想调用OC的方法");
    
    return YES;
}

@end
//  NSObject+Extension.h
//  02-了解-网页开发
#import <Foundation/Foundation.h>

@interface NSObject (Extension)
- (id)performSelector:(SEL)selector withObjects:(NSArray *)objects;
@end
//  NSObject+Extension.m
//  02-了解-网页开发
#import "NSObject+Extension.h"

@implementation NSObject (Extension)
- (id)performSelector:(SEL)selector withObjects:(NSArray *)objects
{
    // 方法签名(方法的描述)
    NSMethodSignature *signature = [[self class] instanceMethodSignatureForSelector:selector];
    if (signature == nil) {
//        @throw [NSException exceptionWithName:@"牛逼的错误" reason:@"方法找不到" userInfo:nil];
        [NSException raise:@"牛逼的错误" format:@"%@方法找不到", NSStringFromSelector(selector)];
    }
    
    // NSInvocation : 利用一个NSInvocation对象包装一次方法调用(方法调用者、方法名、方法参数、方法返回值)
    NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:signature];
    invocation.target = self;
    invocation.selector = selector;
    
    // 设置参数
    NSInteger paramsCount = signature.numberOfArguments - 2; // 除self、_cmd以外的参数个数
    paramsCount = MIN(paramsCount, objects.count);
    for (NSInteger 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;
    if (signature.methodReturnLength) { // 有返回值类型,才去获得返回值
        [invocation getReturnValue:&returnValue];
    }
    
    return returnValue;
}
@end

 

posted @ 2017-03-24 17:42  laugh  阅读(222)  评论(0编辑  收藏  举报