ios与html数据交互,iOS iOS与html进行交互

实现的

效果就是上边那样:首先通过webview 进行网络请求 然后进行显示。

然后点击下一页的按钮 通过js的响应显示另一个网页

最后通过下一页的按钮可以返回到首页。

本文仅仅是h5跟ios 的交互的入门 所以没有做细致的描述。

首先先说一下思路:我的项目中是那样的:首先h5从后台拿到数据,然后我请求h5的界面,然后通过h5的按钮进行选择,通过ios控制按钮到那个界面。

这个小demo不涉及数据传输,只是界面的交互。

1 我自己写了两个小网页。

代码如下

首页的indexPage.html

nextPage(下一页)

alert("123");

function next(){

WOSS.goForward("下一页","外链网址已屏蔽");

}

第二个界面的html index2.html

这是第二个网页,欢迎你跳转成功了

返回首页

function returnFirst(){

WOSS.goHome("返回","外链网址已屏蔽");

}

2 进行ios代码的编写

(1)创建Navigation.h

#import

@interface LSNavigation : UINavigationController

@end

Navigation.m

#import "LSNavigation.h"

@implementation LSNavigation

@end

(2)appDelegate的设置

appDelegate.h

#import

#import "LSNavigation.h"

@interface AppDelegate : UIResponder

@property (strong, nonatomic) UIWindow *window;

@property (strong, nonatomic) LSNavigation *baseNavigationController;

@end

appdelagete.m

#import "AppDelegate.h"

#import "LSNavigation.h"

#import "LSWebVC.h"

@interface AppDelegate ()

@end

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

self.baseNavigationController = [[LSNavigation alloc] init];

self.window.rootViewController = self.baseNavigationController;

LSWebVC *vc = [[LSWebVC alloc]init];

[self.baseNavigationController pushViewController:vc animated:YES];

return YES;

}

(3)创建:LSwebViewVC用来显示加载webView

webVIewVC.h

#import

#import

@interfaceLSWebVC : UIViewController

@property (nonatomic,strong) UIWebView*webView;//@property (nonatomic,assign) BOOL needRefresh;

@property (nonatomic,copy) NSString *webTitle;

@property (nonatomic,copy) NSString*webUrl;@end

webViewVC.m

#import "LSWebVC.h"

#import "LSInterActive.h"

@interface LSWebVC()

@property (nonatomic,strong) JSContext *context;

@end

@implementation LSWebVC

-(void)viewDidLoad

{

_webView = [[UIWebView alloc]initWithFrame:CGRectMake(0, 0,([UIScreen mainScreen].bounds.size.width ) , ([UIScreen mainScreen].bounds.size.height))];

self.view.backgroundColor = [UIColor yellowColor];

self.title = self.webTitle;

self.webView.delegate = self;

[self.view addSubview:self.webView];

if(!self.webUrl)

{

self.webUrl=@"外链网址已屏蔽";

}else{

self.webUrl = [self.webUrl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

}

NSURL *url = [NSURL URLWithString:self.webUrl];

NSMutableURLRequest *request = [[NSMutableURLRequest alloc]initWithURL:url cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:20];

[self.webView loadRequest:request];

}

-(void)webViewDidFinishLoad:(UIWebView *)webView

{

self.context = [webView valueForKeyPath:@"documentView.webView.mainFrame.javaScriptContext"];

self.context.exceptionHandler = ^(JSContext *con, JSValue *exception) {

NSLog(@"exception==========================================================:%@", exception);

con.exception = exception;

};

//设置对象别名

LSInterActive *interactive = [[LSInterActive alloc] init];

self.context[@"WOSS"] = interactive;

}

@end

(4)创建进行点击交互的类(用于存放一些点击事件交互用)

LSINterActive.h

#import

#import

@protocol FCInteractiveProtocol

//1.1前进 //goForward(title,forwardUrl)

- (void)go:(NSString *)title forward:(NSString *)url;

- (void)go:(NSString *)title home:(NSString *)url;

@end

@interface LSInterActive : NSObject

@end

LSInterActive.m

#import "LSInterActive.h"

#import "LSWebVC.h"

#import "LSNavigation.h"

#import "AppDelegate.h"

@implementation LSInterActive

//下一页

-(void)go:(NSString *)title forward:(NSString *)url

{

NSLog(@"FCInteractive-------goForward:%@,%@",title,url);

//当前是异步线程

dispatch_async(dispatch_get_main_queue(), ^{

AppDelegate *delegate = (AppDelegate *)[UIApplication sharedApplication].delegate;

LSNavigation *navi = delegate.baseNavigationController;

LSWebVC *nextVc = [[LSWebVC alloc] init];

nextVc.webTitle = title;

nextVc.webUrl = url;

[navi pushViewController:nextVc animated:YES];

});

}

//返回主页

- (void)go:(NSString *)title home:(NSString *)url{

NSLog(@"FCInteractive-------goHome:%@,%@",title,url);

//当前是异步线程

dispatch_async(dispatch_get_main_queue(), ^{

AppDelegate *delegate = (AppDelegate *)[UIApplication sharedApplication].delegate;

LSNavigation *navi = delegate.baseNavigationController;

UIViewController *vc = navi.viewControllers[0];

if ([vc isKindOfClass:[LSWebVC class]]) {

LSWebVC *firstVc = (LSWebVC *)vc;

// firstVc.needRefresh = YES;

}

[navi popToRootViewControllerAnimated:YES];

});

}

@end

这样的话就可以了。简单的实现了交互。如有不足,欢迎指出 本人邮箱673658917@

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值