我正在尝试在iOS UIwebview上禁用超链接和复制/粘贴菜单 所以我用Google搜索,有很多答案。 然后我遇到另一个问题'webviewdidfinishload not called' 所以我添加我的代码来征求你的帮助。 (我确定我正确连接了IBOutlet)
另外,我仍然没有想到把mainWebView.delegate = self而不是self.mainWebView.delegate = self
有人可以解释这个区别吗?
所以我不确定我的其他功能是什么
[[[mainWebView subviews] lastObject] setScrollEnabled:YES]
[[[mainWebView subviews] lastObject] setScrollsToTop:NO]
mainWebView.scalesPageToFit = YES
这些是否有效。
它应该在ios5到ios6.1上工作 谢谢
<。>文件中的
@interface HRViewController : UIViewController <UIWebViewDelegate>
{
UIWebView *mainWebView;
}
@property (strong, nonatomic) IBOutlet UIWebView *mainWebView;
@end
@interface HRViewController ()
@end
@implementation HRViewController
@synthesize mainWebView;
- (void)viewDidLoad
{
[self startWebView];
[super viewDidLoad];
}
- (void)startWebView
{
mainWebView.delegate = self;
[[[mainWebView subviews] lastObject] setScrollEnabled:YES];
[[[mainWebView subviews] lastObject] setScrollsToTop:NO];
mainWebView.scalesPageToFit = YES;
mainWebView = [[UIWebView alloc] initWithFrame:self.view.bounds];
mainWebView.scalesPageToFit = YES;
[self.view addSubview:self.mainWebView];
NSURL *url = [NSURL URLWithString:/" @"http://www.mysite.com" "/];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[mainWebView loadRequest:request];
}
- (void)webViewDidFinishLoad:(UIWebView *)WebView {
NSLog(@"finish");
[WebView stringByEvaluatingJavaScriptFromString: @"document.documentElement.style.webkitUserSelect='none';"];
[WebView stringByEvaluatingJavaScriptFromString: @"document.documentElement.style.webkitTouchCallout='none';"];
}
- (void)webViewDidStartLoad:(UIWebView *)mainWebView {
NSLog(@"start");
}
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error {
NSLog(@"Error for WEBVIEW: %@", [error description]);
}
EDIED 我使用下面的代码,但仍然无法工作...... 这些都不起作用!!!!!
- (BOOL)canPerformAction:(SEL)action withSender:(id)sender
{
NSLog(@"canPerformAction: %@",NSStringFromSelector(action));
if (action == @selector(copy:) ||
action == @selector(paste:)||
action == @selector(cut:)) {
return NO;
}
return NO;
}
和这个
- (BOOL)canPerformAction:(SEL)action withSender:(id)sender
{
UIMenuController *menuController = [UIMenuController sharedMenuController];
if (menuController) {
[UIMenuController sharedMenuController].menuVisible = NO;
}
return NO;
}
甚至这个
- (void)viewDidLoad {
mainWebView.delegate = self;
}
- (void)webViewDidFinishLoad:(UIWebView *)webView {
NSLog(@"finish");
// Disable user selection
[webView stringByEvaluatingJavaScriptFromString:@"document.documentElement.style.webkitUserSelect='none';"];
// Disable callout
[webView stringByEvaluatingJavaScriptFromString:@"document.documentElement.style.webkitTouchCallout='none';"];
}
也是这个 - (void)viewDidLoad { [self longPress:self.mainWebView]; }
- (void)longPress:(UIView *)webView {
UILongPressGestureRecognizer* longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handleLongPress)];
longPress.allowableMovement=100;
longPress.minimumPressDuration=0.3;
longPress.delaysTouchesBegan=YES;
longPress.cancelsTouchesInView=YES;
[webView addGestureRecognizer:longPress];
}
// I just need this for the selector in the gesture recognizer.
- (void)handleLongPress {
}
请帮助我..它让我疯狂了一个星期..
答案 0 :(得分:-1)
隐藏复制/粘贴菜单:
重写canPerformAction:withSender:
方法以返回NO
您不想允许的操作:
- (BOOL)canPerformAction:(SEL)action withSender:(id)sender
{
if (action == @selector(paste:))
return NO;
if (action == @selector(select:))
return NO;
if (action == @selector(selectAll:))
return NO;
return [super canPerformAction:action withSender:sender];
}
另一种方式:
- (BOOL)canPerformAction:(SEL)action withSender:(id)sender
{
UIMenuController *menuController = [UIMenuController sharedMenuController];
if (menuController) {
[UIMenuController sharedMenuController].menuVisible = NO;
}
return NO;
}
同时检查This link
要停用超链接:
您可以从this site获得此问题的答案。
关于UIWebView
:
如果您使用ARC,则只写,
@property (strong, nonatomic) IBOutlet UIWebView *mainWebView;
无需@synthesize
财产,其他方面,
@interface HRViewController : UIViewController <UIWebViewDelegate>
{
UIWebView *mainWebView;
}
@property (nonatomic,retain) IBOutlet UIWebView *mainWebView;
@end
// .m file
- (void)viewDidLoad
{
mainWebView = [[UIWebView alloc] initWithFrame:self.view.bounds];
mainWebView.scalesPageToFit = YES;
mainWebView.delegate = self;
[[[mainWebView subviews] lastObject] setScrollEnabled:YES];
[[[mainWebView subviews] lastObject] setScrollsToTop:NO];
mainWebView.scalesPageToFit = YES;
[self.view addSubview:self.mainWebView];
NSURL *url = [NSURL URLWithString:@"http://www.mysite.com"];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[mainWebView loadRequest:request];
}
这可能对您有所帮助。