首先,感謝大家花時間回答問題。從StackOverflow開始,我對這些年來的問題得到了如此多的快速回答。UIWebView loadRequest不會在自定義函數中觸發
我是新來的對象C和iOS編程,但從我認爲應該是一個超級簡單的應用程序開始。它收到推送通知(工作正常),並在計算出appid時重定向到網頁。
問題是,雖然我可以在viewDidLoad中將我的UIWebView加載到loadRequest中,但相同的代碼不會在另一個函數中執行。
下面的代碼:
AppDelegate.m
// 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
ViewController.m:
// UFViewController.m
#import "UFViewController.h"
@interface UFViewController()
@property (weak, nonatomic) IBOutlet UIWebView *UFHWebView;
- (void)handleOpenURL:(NSURL *)url;
@end
@implementation UFViewController
- (void)viewDidLoad
{
[super viewDidLoad];
NSLog(@"UFViewController.viewDidLoad started");
// This block that assigns the url and loads it works perfectly here... but not down below.
NSString *urlString = @"http://search.yahoo.com/";
NSURL *url = [NSURL URLWithString:urlString];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[_UFHWebView loadRequest:requestObj];
NSLog(@"UFViewController.viewDidLoad completed");
}
- (void)handleOpenURL:(NSURL *)url
{
NSLog(@"UFViewController.handleOpenURL started");
NSLog(@"url = %@",url);
// The below loadRequest does not load!
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[_UFHWebView loadRequest:requestObj];
NSLog(@"UFViewController.handleOpenURL completed");
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
{
NSLog(@"Error - %@", error);
}
- (BOOL) webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
return YES;
}
@end
運行時這裏編碼,雅虎頁面顯示loadRequest在didload中,但handleOpenURL仍然不會觸發。如果我將loadRequest從viewDidLoad註釋掉,則顯示一個空白頁面,並且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
任何幫助表示讚賞!
你確定,你已經符合協議'UIWebViewDelegate'在正確的地方? –
如何將'viewView'添加到窗口/ navController中?你已經在方法的開始處指示缺少的代碼,但不是在末尾 –
Hi Rich--「theview」正在使用appdelegate中的這一行引用:「UFViewController * theview = [[UFViewController alloc] init];」 ...是的,之後沒有任何代碼。 – Joey