簡體   English   中英

UIWebView loadRequest未在自定義函數中觸發

[英]UIWebView loadRequest not firing in custom function

首先,感謝大家花時間回答問題。 多年來,我從StackOverflow獲得了許多快速解決問題的方法。

我是Object C和iOS編程的新手,但從我認為應該是一個超級簡單的應用程序開始。 它會收到一個推送通知(工作正常),並在確定其appid后重定向到網頁。

問題是,雖然我可以將UIWebView放入viewDidLoad中的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

當按此處的代碼運行時,雅虎頁面從didload中的loadRequest中顯示,但是handleOpenURL仍然不會觸發。 如果我從viewDidLoad中注釋掉loadRequest,則會顯示空白頁面,並且handleOpenURL仍然不會觸發。

這是調試順序。 在接收到AppID並手動觸發handleOpenURL之前,viewDidLoad觸發並完成:

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后,您不會對其進行任何操作。

我認為這可能正在發生:

  1. 應用啟動。 您在情節UFViewController中有一個UFViewController實例。 它加載,調用-viewDidLoad ,打印日志語句,並正確加載webview。
  2. 稍后,將-didRegisterForRemoteNotificationsWithDeviceToken:方法。 這將實例化UFViewController ,並在其上調用-handleOpenUrl方法。 -handleOpenURL日志方法啟動。 但是,此控制器尚未加載到視圖層次結構中,因此永遠不會實例化其視圖,也永遠不會調用其-viewDidLoad 這意味着正在調用-loadRequest:-loadRequest:nil 在Objective-C中,將消息發送為nil是有效的-與大多數其他語言不同,不會引發任何錯誤或異常。 相反,絕對沒有任何反應,因此沒有Web負載。 UFViewController中的-handleOpenUrl僅由方法內的局部變量引用,因此,在方法完成時,對該方法的最后一個強引用將消失並被釋放。

要檢查是否正確:

NSLog(@"%@",[self description]); 在您的vc方法中-這將記錄對象的地址並告訴您它們是否相同。

- NSLog網絡控制器的視圖,以檢查它是否是nilhandleOpenURL

為了解決您的問題,我建議使用AppDelegate發布的NSNotification並由完全實例化的UFViewController監聽。 因此,在vc中,您可以執行以下操作:

[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(handleRegistration:) name:@"HandleRegistrationNotification" object:nil];

然后實現handleRegistration方法:

-(void)handleRegistration:(NSNotification*)notification
{
     //your handleOpenUrl code 
}

並且

-(void)dealloc
{
    [[NSNotificationCenter defaultCenter]removeObserver:self name:@"HandleRegistrationNotification" object:nil];
}

(這一點很重要,因為否則在取消分配vc時可能會崩潰)

然后,在-didRegisterForRemoteNotifications:方法中:

[[NSNotificationCenter defaultCenter]postNotificationName:@"HandleRegistrationNotification" object:nil];

如果您需要通過通知傳遞任何信息(例如url),則可以使用object參數-在vc上通過NSNotification參數的object屬性調用的方法中可以訪問該參數。

有關Objective-C中通知的完整文檔,請參見此處

有幾點要點-將視圖控制器之類的東西theView而不是theViewController通常是一個壞主意,因為其他人(或您六個月后的時間)最終會認為它們是UIView 同樣,webview屬性不應以大寫字母開頭-通常僅用於類名。

這應該稱為“如何從appdelegate調用情節提要viewviewler方法”。 答案是:

在AppDelegate.m中:

#import "UFViewController.h"
...
UFViewController *rootViewController = (UFViewController*)self.window.rootViewController;
[rootViewController handleOpenURL:url];

在上面的代碼段中,這意味着替換這些行

UFViewController *theview = [[UFViewController alloc] init];
[theview handleOpenURL:url];

與上面的那些。 區別在於我的原始代碼將一個新的UFViewController對象實例化為一個局部變量,而正確的代碼只是獲取了已在情節提要中創建的視圖控制器的句柄並調用了它的方法。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM