
Simulator Screen Shot 2016年4月16日 00.27.57.png
One、UIWebView加載靜態(tài)頁(yè)面
做APP時(shí),我們會(huì)展示靜態(tài)頁(yè)面,或與靜態(tài)頁(yè)面交互
self.webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 40, self.view.frame.size.width, self.view.frame.size.height - 20)];
NSString *path = [[NSBundle mainBundle] bundlePath];
NSURL *baseURL = [NSURL fileURLWithPath:path];
NSString * htmlPath = [[NSBundle mainBundle] pathForResource:@"index" ofType:@"html"];
NSString * htmlCont = [NSString stringWithContentsOfFile:htmlPath encoding:NSUTF8StringEncoding error:nil];
[self.webView loadHTMLString:htmlCont baseURL:baseURL];
[self.view addSubview: _webView];
與APP交互時(shí),可以在靜態(tài)頁(yè)面中加如下代碼
<button id="btn_submit" onclick="Login('參數(shù)1')">返回</button>
在APP調(diào)用的文件中,
<blockquote>
先導(dǎo)入頭文件 #import <JavaScriptCore/JavaScriptCore.h>
遵守UIWebView的代理 <UIWebViewDelegate>
</blockquote>
JSContext *context = [_webView valueForKeyPath:@"documentView.webView.mainFrame.javaScriptContext"];
context[@"Login"] = ^() {
// 在這里處理你要做的事情
NSArray *args = [JSContext currentArguments];
// NSString *strUrl = args[0];
// NSLog(@"%@",strUrl);
for (id obj in args) {
NSLog(@"%@",obj);
}
};
Two、UIWebView加載網(wǎng)絡(luò)頁(yè)面和加載失敗處理
之前寫(xiě)過(guò)瀏覽器網(wǎng)頁(yè)與APP交互,在UIWebView內(nèi)嵌與APP交互同樣適用,開(kāi)啟穿越門(mén)

Simulator Screen Shot 2016年4月16日 01.20.24.png
導(dǎo)入文件 #import <JavaScriptCore/JavaScriptCore.h>
@interface ViewController ()
{
NSURLConnection *theConnection;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
UIWebView *myWebview = [[UIWebView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
[self.view addSubview:myWebview];
NSURL *url = [NSURL URLWithString:@"https://itunesconnect.apple.com/"];
NSURLRequest *request =[NSURLRequest requestWithURL:url
cachePolicy:NSURLRequestReloadIgnoringLocalCacheData
timeoutInterval:10.0];
[myWebview loadRequest:request];
if (theConnection)
{
[theConnection cancel];
// SAFE_RELEASE(theConnection);
NSLog(@"safe release connection");
}
theConnection= [[NSURLConnection alloc]initWithRequest:request delegate:self startImmediately:YES];
}
-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
if (theConnection) {
// SAFE_RELEASE(theConnection);
NSLog(@"safe release connection");
}
if ([response isKindOfClass:[NSHTTPURLResponse class]]){
NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse*)response;
if ((([httpResponse statusCode]/100) == 2)){
NSLog(@"connection ok");
}
else{
NSError *error = [NSError errorWithDomain:@"HTTP" code:[httpResponse statusCode] userInfo:nil];
if ([error code] == 404){
NSLog(@"404");
[self webViewFail];
}
else if ([error code] == 403){
NSLog(@"403");
[self webViewFail];
}
}
}
}
-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
if (theConnection) {
// SAFE_RELEASE(theConnection);
NSLog(@"safe release connection");
[self webViewFail];
}
if (error.code == 22) //The operation couldn’t be completed. Invalid argument
{
NSLog(@"22");
[self webViewFail];
}
else if (error.code == -1001) //The request timed out. webview code -999的時(shí)候會(huì)收到-1001
{
NSLog(@"-1001");
[self webViewFail];
}
else if (error.code == -1005) //The network connection was lost.
{
NSLog(@"-1005");
[self webViewFail];
}
else if (error.code == -1009) //The Internet connection appears to be offline
{
NSLog(@"-1009");
[self webViewFail];
}
}
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
{
/*
didFailLoadWithError中出現(xiàn)code = -999錯(cuò)誤。
錯(cuò)誤原因:一個(gè)頁(yè)面沒(méi)有被加載完成之前,收到下一個(gè)請(qǐng)求。
*/
CLog(@"error%@",error);
if ([error code] == NSURLErrorCancelled) {
return;
}
[self webViewFail];
}
- (void)webViewFail
{
UILabel *content = [[UILabel alloc]initWithFrame:(CGRectMake(20, 100, 300, 100))];
NSString *originStr = @"Hello,中秋節(jié)!";
NSMutableAttributedString *attributedStr01 = [[NSMutableAttributedString alloc] initWithString: originStr];
[attributedStr01 addAttribute: NSForegroundColorAttributeName value: [UIColor blueColor] range: NSMakeRange(0, 4)];
//分段控制,第5個(gè)字符開(kāi)始的3個(gè)字符,即第5、6、7字符設(shè)置為紅色
[attributedStr01 addAttribute: NSForegroundColorAttributeName value: [UIColor redColor] range: NSMakeRange(4, 3)];
//賦值給顯示控件label01的 attributedText
content.attributedText = attributedStr01;
[self.view addSubview:content];
// 這里為加載靜態(tài)頁(yè)面,進(jìn)行展示錯(cuò)誤信息
// UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
// NSString *path = [[NSBundle mainBundle] bundlePath];
// NSURL *baseURL = [NSURL fileURLWithPath:path];
// NSString * htmlPath = [[NSBundle mainBundle] pathForResource:@"webError" ofType:@"html"];
// NSString * htmlCont = [NSString stringWithContentsOfFile:htmlPath encoding:NSUTF8StringEncoding error:nil];
// [webView loadHTMLString:htmlCont baseURL:baseURL];
// [self.view addSubview: webView];
// JS 調(diào)用 OC
// 1、首先導(dǎo)入庫(kù) JavaScriptCore.framework
// JSContext *context = [webView valueForKeyPath:@"documentView.webView.mainFrame.javaScriptContext"];
// context[@"btnSubmit"] = ^() {
// NSLog(@"返回上一頁(yè)");
// };
}