实现的
效果就是上边那样:首先通过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@
更多推荐
ios与html数据交互,iOS iOS与html进行交互
发布评论