iPhone:UIWebview在safari中打开超链接

时间:2012-03-14 13:43:07

标签: iphone objective-c ios uiwebview

在我的应用程序中,我使用UIwebview,我需要超链接一些电话号码,电子邮件和URLs.I执行了以下操作:

 - (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] initWithData:Data encoding:NSUTF8StringEncoding];

    NSString * creditsHtml = [NSString stringWithFormat:@" <font face=\"Arial\"><H3 align=\"center\"> Credits </H3> %@ <br>Phone%@</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)webView:(UIWebView*)webView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)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)webView:(UIWebView*)webView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)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 ; at the end&amp; Extraneous ')' before ';'

3 个答案:

答案 0 :(得分:1)

你在loadScreen函数中分配/初始化UIWebView的新对象而没有设置它的委托,因为你假设你在viewDidLoad中这样做了..你必须在定义实例“credits”之后设置委托。

答案 1 :(得分:1)

只需将此行粘贴到您在loadScreen方法

中创建webview信用的位置下方
credits=[[UIWebView alloc]init];
credits.delegate=self;   //Add this line

答案 2 :(得分:0)

这看起来像是this answered question的副本。

如果你的-webView:shouldStartLoadWithRequest:navigationType:方法根本没有被调用,那么连接你的代表就会出现问题 - 如果你没有看到更多你的工作,我们就无法真正进一步诊断它。 (你确定它没有被调用吗?在方法体的顶部粘贴NSLog,或者在调试器中放置一个断点,如果你没有。)

另外,在问题中发布该方法的主体时,为了简洁起见,你已经剥离了太多,或者传入的参数和用于确定在Safari中打开哪些URL的逻辑之间没有联系。我认为你的意思是测试用户点击的链接?这是request参数。