0

我对 Xcode 比较陌生,我已经开始构建一个使用UIWebView. 为了使其符合 App Store 提交,Apple 更喜欢您使用 Safari。为了克服这个问题,我想在我的UIWebView导航中添加一个按钮,单击该按钮将在 Safari 中打开相同的 url。在 Twitter 应用程序中可以看到一个例子;他们有一个按钮,可以打开当前UIWebView在 Safari 窗口中查看的内容。

4

1 回答 1

2

您可以使用UIWebViewDelegate' 方法

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
    if iWantToOpenThisURLInSafari([request URL]) {
        [UIApplication openUrl:[request URL]];
        return NO; // tell the webView to not navigate to the URL, I'm handling it
    } else {
        return YES;
    }
}

- (BOOL)iWantToOpenThisURLInSafari:(NSURL* url) [
    // you just have to fill in this method.
    return NO;
}

http://developer.apple.com/library/ios/#documentation/uikit/reference/UIWebViewDelegate_Protocol/Reference/Reference.html#//apple_ref/occ/intf/UIWebViewDelegate

编辑:@PaulGraham 要求的更多细节

// You have a pointer to you webView somewhere
UIWebView *myWebView;

// create a class which implements the UIWebViewDelegate protocol
@interface MyUIWebViewDelegate:NSObject<UIWebViewDelegate>

// in this class' @implementation, implement the shouldStartLoad..method

@implementation
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
    if iWantToOpenThisURLInSafari([request URL]) {
        [UIApplication openUrl:[request URL]];
        return NO; // tell the webView to not navigate to the URL, I'm handling it
    } else {
        return YES;
    }
}

// then, set the webView's delegate to an instance of that class

MyUIWebViewDelegate* delegate = [[MyUIWebViewDelegate alloc] init];
webView.delegate = delegate;

// your delegate will now recieve the shouldStartLoad.. messages.
于 2012-07-14T07:59:03.593 回答