iOS开发 - OC - UIWebView使用

一、简单属性

webView.scalesPageToFit; // 自动对页面进行缩放以适应屏幕。

webView.scrollView.bounces // 禁止页面滚动弹跳。

二、UIWebView与JS交互

1.OC调用JS代码:
- (NSString *)stringByEvaluatingJavaScriptFromString:(NSString *)script;@“document.documentElement.style.webkutTouchCallout= 'none';” -------- 禁止长按弹出菜单@“document.documentElement.style.webkutUserSelect= 'none';” -------- 禁止长按选择

2.JS调用OC代码:

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType;
在该代理方法中可以拿到webview每次加载的request的url, JS可以在需要调用OC代码的时候给这些url附加协议串,OC端拿到url的协议内容,执行本地native代码。

三、清除UIWebView缓存 

[[NSURLCache sharedURLCache ]removeAllCachedResponses];

NSHTTPCookie *cookie;
NSHTTPCookieStorage *storage = [NSHTTPCookieStoragesharedHTTPCookieStorage];
for ( cookiein [storagecookies]) {
[storagedeleteCookie:cookie];
}

四、获取设备UA并修改 to JS

可以判断设备是通过什么设备加载的url,然后再JS本地做一些特殊处理。方法如下:

在AppDelegate的-applicationDidFinishLaunching:方法中增加如下代码,修改默认User Agent
UIWebView*tempWebView= [[UIWebViewalloc]initWithFrame:CGRectZero];NSString*userAgent= [tempWebViewstringByEvaluatingJavaScriptFromString:@"navigator.userAgent"];NSString*ua=[NSStringstringWithFormat:@"%@\\%@",userAgent,@"custom string"];[[NSUserDefaultsstandardUserDefaults]registerDefaults:@{@"UserAgent":ua,@"User-Agent":ua}];

五、JS中的confirm title 消除
在ios中用webview 中的confirm对话框时一般的title是xx.html 很不美观。
可以自己写个UIwebview的类别

@interface UIWebView (JavaScriptAlert)
-(void)webView:(UIWebView *)sender runJavaScriptAlertPanelWithMessage:(NSString *)message initiatedByFrame:(id)frame;

-(BOOL)webView:(UIWebView *)sender runJavaScriptConfirmPanelWithMessage:(NSString *)message initiatedByFrame:(id)frame;
@end

@implementation UIWebView (JavaScriptAlert)


static BOOL diagStat = NO;

static BOOL isEnd = NO;

-(void)webView:(UIWebView *)sender runJavaScriptAlertPanelWithMessage:(NSString *)message initiatedByFrame:(id)frame{

    UIAlertView* dialogue = [[UIAlertView alloc]initWithTitle:nil message:message delegate:nil cancelButtonTitle:@"好" otherButtonTitles:nil, nil];

    [dialogue show];

    [dialogue release];

}


-(BOOL)webView:(UIWebView *)sender runJavaScriptConfirmPanelWithMessage:(NSString *)message initiatedByFrame:(id)frame{

    UIAlertView* dialogue = [[UIAlertView alloc] initWithTitle:nil message:message delegate:self cancelButtonTitle:NSLocalizedString(@"ok", @"Ok") otherButtonTitles:NSLocalizedString(@"Cancel", @"Cancel"), nil];

    [dialogue show];

    float version = [[[UIDevice currentDevice] systemVersion] floatValue];

    if (version >= 7.0)

    {

        while (isEnd == NO) {

            [[NSRunLoop mainRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:0.01]];

        }

    }

    else

    {

        while (isEnd == NO && dialogue.superview != nil) {

            [[NSRunLoop mainRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:0.01]];

        }

    }

    isEnd = NO;

    return diagStat;

}


-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{

    if (buttonIndex==0) {

        diagStat=NO;

    }else if(buttonIndex==1){

        diagStat=YES;

    }

    isEnd = YES;

}

@end
六、待补充

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值