【问题标题】:Xcode 4.3 from a button in a UIWebView open the same url in safari来自 UIWebView 中的按钮的 Xcode 4.3 在 safari 中打开相同的 url
【发布时间】:2012-07-14 07:55:34
【问题描述】:

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

【问题讨论】:

    标签: iphone ios xcode uiwebview


    【解决方案1】:

    你可以使用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.
    

    【讨论】:

    • Prody,谢谢你,你能解释一下如何实现这个,我理解上面的代码,不知道在哪里使用它。
    • Prody,谢谢你,但是我仍然在努力实现,你有一个工作项目中的例子吗,抱歉可能会让人觉得很愚蠢,但是 Xcode 和 c 都是新的我
    • 对不起,我现在不在 OSX 上,而且 Xcode 在 OSX 之外不可用,但是你需要的一切都在那里,你只需要把这些部分放在一起。如果这对您来说太难了,您应该阅读 Objective-C 教程。你可以在developer.apple.com找到教程
    猜你喜欢
    • 1970-01-01
    • 2015-03-07
    • 2013-01-07
    • 1970-01-01
    • 1970-01-01
    • 2023-03-21
    • 2020-02-26
    • 1970-01-01
    • 2023-03-10
    相关资源
    最近更新 更多