【问题标题】:Open Link in UIWebView in Safari在 Safari 的 UIWebView 中打开链接
【发布时间】:2023-03-21 13:42:01
【问题描述】:

我正在开发一个应用程序,该应用程序接收我在 UIWebView 中显示的提要。提要有嵌入的链接,我想在 Safari 而不是 WebView 中打开这些链接。我查看了此处发布的其他几个问题。我不确定我错过了什么,但我认为这很简单。这是我的 .h 和 .m 文件

#进口 @class 博客Rss; @interface EnduranceDailyWorkoutViewController : UIViewController { IBOutlet UIWebView * descriptionTextView; BlogRss * currentSelectedBlogItem; } @property (nonatomic, 保留) UIWebView * descriptionTextView; @property (readwrite, retain) BlogRss * currentSelectedBlogItem; @结尾
#import "EnduranceDailyWorkoutViewController.h"
#import "BlogRss.h"

@implementation EnduranceDailyWorkoutViewController


@synthesize descriptionTextView = descriptionTextView;
@synthesize currentlySelectedBlogItem = currentlySelectedBlogItem;

- (void)viewDidLoad {
    [super viewDidLoad];
    NSString *html = [NSString stringWithFormat:@"%@ %@",currentlySelectedBlogItem.title, currentlySelectedBlogItem.contentEncoded];
    [descriptionTextView loadHTMLString:html baseURL:[NSURL URLWithString:nil]];

}

-(BOOL)webView:(UIWebView *)descriptionTextView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
    if (UIWebViewNavigationTypeLinkClicked == navigationType) {
        [[UIApplication sharedApplication] openURL:[request URL]];
        return NO;
    }
    return YES;
}

使用 Interface Builder 我已经链接了 IBOutlet 和 UIWebView。请让我知道我错过了什么。我已经在 webView 部分设置了断点,但是代码从来没有到达那里,所以它几乎就像 IB 中没有正确链接一样。

【问题讨论】:

    标签: iphone uiwebview


    【解决方案1】:

    您需要确保将 UIWebView 的委托设置为您的控制器。您可以在界面生成器中执行此操作,也可以修改 viewDidLoad 方法:

    - (void)viewDidLoad {
        [super viewDidLoad];
        // add this line to set the delegate of the UIWebView
        descriptionTextView.delegate = self;
        NSString *html = [NSString stringWithFormat:@"%@ %@",currentlySelectedBlogItem.title, currentlySelectedBlogItem.contentEncoded];
        [descriptionTextView loadHTMLString:html baseURL:[NSURL URLWithString:nil]];
    }
    

    【讨论】:

    • GreginYEG,感谢您的帮助。我忘记设置代表了。我以为我已经在 Interface Builder 中做到了,但显然没有。
    猜你喜欢
    • 2023-03-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-01
    • 2016-04-16
    • 2015-12-02
    • 1970-01-01
    相关资源
    最近更新 更多