Ios 自定义函数中未触发UIWebView loadRequest
首先,感谢所有花时间回答问题的人。这些年来,我从StackOverflow得到了很多问题的快速答案 我对ObjectC和iOS编程还不熟悉,但从我认为应该是超级简单的应用开始。它接收推送通知(工作正常),并在计算出其appid后重定向到网页 问题是,虽然我可以在viewDidLoad中获取UIWebView到loadRequest,但相同的代码不会在另一个函数中执行 代码如下: AppDelegate.mIos 自定义函数中未触发UIWebView loadRequest,ios,iphone,objective-c,uiwebview,Ios,Iphone,Objective C,Uiwebview,首先,感谢所有花时间回答问题的人。这些年来,我从StackOverflow得到了很多问题的快速答案 我对ObjectC和iOS编程还不熟悉,但从我认为应该是超级简单的应用开始。它接收推送通知(工作正常),并在计算出其appid后重定向到网页 问题是,虽然我可以在viewDidLoad中获取UIWebView到loadRequest,但相同的代码不会在另一个函数中执行 代码如下: AppDelegate.m // UFAppDelegate.m #import "UFAppDelegate.h"
// UFAppDelegate.m
#import "UFAppDelegate.h"
#import "UFViewController.h"
@implementation UFAppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Add registration for remote notifications
[[UIApplication sharedApplication] registerForRemoteNotificationTypes:
(UIRemoteNotificationType)(UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound)];
return YES;
}
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)devToken {
// ....
NSString *urlString = @"http://www.google.com";
NSURL *url = [NSURL URLWithString:urlString];
NSLog(@"Navigating to URL: %@", url);
UFViewController *theview = [[UFViewController alloc] init];
[theview handleOpenURL:url];
}
@end
ViewController.h:
// UFViewController.h
#import <UIKit/UIKit.h>
@interface UFViewController : UIViewController
- (void)handleOpenURL:(NSURL *)url;
@end
当按照这里的代码运行时,yahoo页面会从didload中的loadRequest显示,但handleOpenURL页面仍然不会触发。如果我从viewDidLoad中注释掉loadRequest,则会显示一个空白页面,并且handleOpenURL仍然不会触发
下面是调试序列。viewDidLoad在接收AppID和手动触发handleOpenURL之前触发并完成:
2013-12-12 15:28:32.606 UF[5896:60b] UFViewController.viewDidLoad started
2013-12-12 15:28:32.608 UF[5896:60b] UFViewController.viewDidLoad completed
2013-12-12 15:28:32.765 UF[5896:60b] Navigating to URL: http://www.google.com
2013-12-12 15:28:32.769 UF[5896:60b] UFViewController.handleOpenURL started
2013-12-12 15:28:32.773 UF[5896:60b] url = http://www.google.com
2013-12-12 15:28:32.775 UF[5896:60b] UFViewController.handleOpenURL completed
感谢您的帮助 您确定这里只有一个
UFViewController
实例吗
-didRegisterForRemoteNotificationsWithDeviceToken
在局部变量中初始化一个新的web视图控制器,但我看不出它是如何显示给用户的-一旦vc被创建,你就不会对它做任何事
以下是我认为可能发生的情况:
UFViewController
的实例。它加载,调用其-viewDidLoad
,打印日志语句并正确加载webview-didRegisterForRemoteNotificationsWithDeviceToken:
方法。这将实例化一个UFViewController
,并对其调用-handleopeanurl
方法。-handleOpenURL
日志方法触发。但是,此控制器尚未加载到视图层次结构中,因此其视图从未实例化,其-viewDidLoad
也从未调用。
这意味着调用-loadRequest:
的Web视图是nil
。在Objective-C中,向nil
发送消息是有效的-与大多数其他语言不同,不会引发错误或异常。相反,完全没有发生任何事情,因此没有web负载。
-handleOpenUrl
中的UFViewController
仅由方法中的局部变量引用,因此当方法完成时,对它的最后一个强引用消失并释放NSLog(@“%@,[自我描述])代码>在你的vc方法中-这将记录对象的地址并告诉你它们是否相同
-NSLog
web控制器的视图,以检查handleOpenURL
为了解决您的问题,我建议使用由AppDelegate
发布的NSNotification
,并由完全实例化的UFViewController
监听。因此,在vc中,您应该:
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(handleRegistration:) name:@"HandleRegistrationNotification" object:nil];
然后执行handleRegistration
方法:
-(void)handleRegistration:(NSNotification*)notification
{
//your handleOpenUrl code
}
[[NSNotificationCenter defaultCenter]postNotificationName:@"HandleRegistrationNotification" object:nil];
而且
-(void)dealloc
{
[[NSNotificationCenter defaultCenter]removeObserver:self name:@"HandleRegistrationNotification" object:nil];
}
(这一点很重要,因为否则vc解除锁定时可能会崩溃)
然后,在-didRegisterForRemotonifications:
方法中:
-(void)handleRegistration:(NSNotification*)notification
{
//your handleOpenUrl code
}
[[NSNotificationCenter defaultCenter]postNotificationName:@"HandleRegistrationNotification" object:nil];
如果您需要通过通知(例如URL)传递任何信息,您可以使用对象
参数-这可以通过NSNotification
参数的对象
属性在vc上调用的方法中访问
可以在Objective-C中找到通知的完整文档
一些小问题-通常,将视图控制器称为theView
而不是theViewController
是一个坏主意,因为其他人(或6个月后的你)最终会认为他们是UIView
s。此外,webview属性不应以大写字母开头-这通常仅用于类名。这应称为“如何从appdelegate调用情节提要viewcontroller方法”。答案是:
在AppDelegate.m中:
#import "UFViewController.h"
...
UFViewController *rootViewController = (UFViewController*)self.window.rootViewController;
[rootViewController handleOpenURL:url];
在我上面的代码片段中,这意味着替换这些行
UFViewController *theview = [[UFViewController alloc] init];
[theview handleOpenURL:url];
用上面的。区别在于,我的原始代码将一个新的UFViewController对象实例化为一个局部变量,而正确的代码只是获取已在情节提要中创建的视图控制器的句柄并调用其方法。是否确定,您在正确的位置遵守了协议UIWebViewDelegate
?如何将视图添加到窗口/导航控制器中?您已经在方法的开头指出了缺少的代码,但在结尾处却没有指出。富--“theview”正在使用appdelegate中的这一行进行引用:“UFViewController*theview=[[UFViewController alloc]init];”。。。是的,之后没有代码。Fnatastic回答,谢谢!我要试试你的建议。感谢您对点火顺序的详细解释。。。在我有时间坐下来看书之前,这对我的理解有很大帮助。我打算执行“UFViewController*theview=[[UFViewController alloc]init];/[theview HandleAppenUrl:url];”的目的是获取指向已创建的ViewController的指针并调用其HandleAppenUrl。。。因为我实际需要的AppID(不包括在代码示例中)是在DidRegisterForRemotionTificationsWithDeviceToken函数中计算出来的。Rich,您的计算是正确的!它们是两个不同的对象。第二个报告是在手动调用我的handleOpenURL函数时报告的。理想情况下,在didRegisterForRemoteNotificationsWithDeviceToken中,我只想获得已经创建的WebView的句柄,并调用其handleOopenURL函数。但我也会尝试你的其他建议,看看我是否能以那种方式达到目的。非常感谢。[self fhwebview.handleOpenURL:url]??