3

好的,所以我正在处理的项目有一个推特提要,我将它放入一个表格中,我将推文的主要部分放在 UITextView 中。我希望它能够在 UIWebView 中打开文本中的链接。我设法使用以下方法拦截了打开的 url 调用。

#import "MyApplication.h"
#import "haydnboardAppDelegate.h"

@implementation MyApplication
- (BOOL)openURL:(NSURL *)url
{
   return [self openURL:url forceOpenInSafari:NO];
}


-(BOOL)openURL: (NSURL *)url forceOpenInSafari:(BOOL)forceOpenInSafari
{

    if(forceOpenInSafari)
    {
        // We're overriding our app trying to open this URL, so we'll let UIApplication     federate this request back out
        //  through the normal channels. The return value states whether or not they were able to open the URL.
        return [super openURL:url];
    }

    //
    // Otherwise, we'll see if it is a request that we should let our app open.

    BOOL couldWeOpenUrl = NO;

    NSString* scheme = [url.scheme lowercaseString];
    if([scheme compare:@"http"] == NSOrderedSame
       || [scheme compare:@"https"] == NSOrderedSame)
    {
        // TODO - Here you might also want to check for other conditions where you do not want your app opening URLs (e.g.
        //  Facebook authentication requests, OAUTH requests, etc)

        // TODO - Update the cast below with the name of your AppDelegate
        // Let's call the method you wrote on your AppDelegate to actually open the BrowserViewController
        couldWeOpenUrl = [(haydnboardAppDelegate *)self.delegate openURL:url];
    }

    if(!couldWeOpenUrl)
    {
        return [super openURL:url];
    }
    else
    {
        return YES;
    }
}
@end

我也将 main.h 文件更改为:

return UIApplicationMain(argc, argv, @"MyApplication", nil);

我试图在代码中调用 open url 函数,但它根本不会调用该函数,当我单击 TextView 中的链接时它不会做任何事情。我该怎么做才能让它调用函数来更改视图?

编辑:设法让我的应用程序委托中的函数运行,但是当它尝试推送视图控制器时没有任何反应。我得到错误 viewController 不在窗口层次结构中,所以我决定更改函数,以便它调用 viewController 中的函数,但仍然没有任何反应,但在 viewController 中调用 pushViewController 时没有错误消息。我发现这是因为 navigationController = nil 我该如何解决这个问题?

4

0 回答 0