在我的应用程序中,我使用了 UIwebview,我需要在其中超链接一些电话号码、电子邮件和 URL。我做了以下操作:
- (void)viewDidLoad
{
credits.delegate=self;
}
-(void)loadScreen
{
scroll=[[UIScrollView alloc]init];
credits=[[UIWebView alloc]init];
NSString *Address = @"www.***.com/php/getjson.php?method=";
jmax = [NSURL URLWithString:Address];
NSData *Data =[ NSData dataWithContentsOfURL:jmax];
String = [[NSString alloc] initWithDataata encoding:NSUTF8StringEncoding];
NSString * creditsHtml = [NSString stringWithFormat" <font face=\"Arial\"><H3 align=\"center\"> Credits </H3> %@ <br>hone%@</font>", String,phone];
credits.userInteractionEnabled = YES;
credits.frame=CGRectMake(10, (frame1.size.height + 564), 300, 100);
credits.dataDetectorTypes=UIDataDetectorTypeAll;
credits loadHTMLString:creditsHtml baseURL:nil];
[scroll addSubview:credits];
}
-(BOOL)webViewUIWebView*)webView shouldStartLoadWithRequestNSURLRequest*)request navigationTypeUIWebViewNavigationType)navigationType{
NSString *urls= jmaxString;
NSURL *requestURL =[NSURL URLWithString: urls];
if ( ( [ [ requestURL scheme ] isEqualToString: @"http" ] || [ [ requestURL scheme ] isEqualToString: @"https" ] || [ [ requestURL scheme ] isEqualToString: @"mailto" ] )
&& ( navigationType == UIWebViewNavigationTypeLinkClicked ) ) {
return ![ [ UIApplication sharedApplication ] openURL: requestURL ];
}
return YES;
}
编辑 shouldStartLoadWithRequest: 方法
-(BOOL)webViewUIWebView*)webView shouldStartLoadWithRequestNSURLRequest*)request navigationTypeUIWebViewNavigationType)navigationType{
NSURL *requestURL =[request URL];
if ( ( [ [ requestURL scheme ] isEqualToString: @"http" ] || [ [ requestURL scheme ] isEqualToString: @"https" ] || [ [ requestURL scheme ] isEqualToString: @"mailto" ] )
&& ( navigationType == UIWebViewNavigationTypeLinkClicked ) ) {
return ![ [ UIApplication sharedApplication ] openURL: requestURL ];
}
NSLog(@"URL %@",requestURL);
return YES;
}
NSURL *requestURL =[request URL];
出现错误,Expected ;在末尾code> &
Extraneous ')' before ';'
您在 loadScreen 函数中分配/初始化 UIWebView 的一个新对象,而没有设置其委托(delegate),因为您假设您在 viewDidLoad 中这样做了。您必须在定义实例“信用”后设置委托(delegate)。
关于iPhone:UIWebview 在 safari 中打开超链接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9703175/
欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://ogeek.cn/) | Powered by Discuz! X3.4 |