4

我有一个简单的视图控制器,当点击链接按钮时会显示一个 SFSafariViewController。

#import "ViewController.h"
#import "SafariDelegate.h"

@interface ViewController ()

@property(nonatomic, strong) NSString *url;
@property(nonatomic, weak) SafariDelegate *safariDelegate;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.url = @"http://www.google.com";
}

- (IBAction)linkTapped:(id)sender {
    SFSafariViewController *vc = [[SFSafariViewController alloc] initWithURL:[NSURL URLWithString:self.url]];
    SafariDelegate *safariDelegate = [[SafariDelegate alloc] init];
    vc.delegate = safariDelegate;
    [self presentViewController:vc animated:YES completion:nil];
}

@end

SafariDelegate 符合 SFSafariViewControllerDelegate 并且仅在委托方法触发时记录。

SafariDelegate.h

#import <Foundation/Foundation.h>
#import <SafariServices/SafariServices.h>

@interface SafariDelegate : NSObject <SFSafariViewControllerDelegate>

@end

SafariDelegate.m

#import "SafariDelegate.h"

@implementation SafariDelegate

- (NSArray<UIActivity *> *)safariViewController:(SFSafariViewController *)controller activityItemsForURL:(NSURL *)URL     title:(nullable NSString *)title
{
    NSLog(@"safariViewController activityItemsForURL");

    return @[];
}

- (void)safariViewControllerDidFinish:(SFSafariViewController *)controller
{
    NSLog(@"safariViewControllerDidFinish");
}


- (void)safariViewController:(SFSafariViewController *)controller didCompleteInitialLoad:(BOOL)didLoadSuccessfully
{
    NSLog(@"safariViewController didCompleteInitialLoad");
}

@end

当我点击按钮时,safariviewcontroller 会正确加载页面,但从不调用委托方法(包括当我点击“完成”时)。我在出现“vc”的行处设置了一个断点,并确认在该点正确设置了代表。我错过了什么?

4

2 回答 2

6

SafariViewController不保留其委托,因此在您的linkTapped:方法返回后它会立即被释放,因为没有其他任何东西对它有强引用。

您需要在某处保持对它的强烈引用。您可能只需将当前视图控制器中的属性更改为strong并将其存储在那里。您还可以将其保存在 safari 视图控制器上的关联对象中。

于 2016-03-28T23:59:41.897 回答
3

我也出现了同样的问题。我在函数内部创建了“SafariViewController”。因此,SafariViewController 内存被清除。所以,我无法从 SafariViewControllerDelegate 获得触发器。

最后我找到了解决这个问题的方法。

当你在一个类中创建一个SFSafariViewController时。您必须将其声明为全局变量

class Foo: NSObject {
    var safariVC: SFSafariViewController!
}

然后你可以分配你的SafariViewController

func config(_ loadUrl: URL) {

   if #available(iOS 11.0, *) {
       let config = SFSafariViewController.Configuration()
       config.barCollapsingEnabled = true
       config.entersReaderIfAvailable = true
       safariVC = SFSafariViewController(url: loadUrl, configuration: config)
   } else {
      // Fallback on earlier versions
      safariVC = SFSafariViewController(url: loadUrl)
   }
}

然后设置一个委托

self.safariVC.delegate = self

最后呈现 safariViewController。

 UIApplication.shared.keyWindow?.rootViewController?.present(safariVC, animated: true, completion: nil)

现在肯定会触发 safariViewController 委托。

// MARK: - SFSafariViewControllerDelegate
extension Foo: SFSafariViewControllerDelegate {

    func safariViewControllerDidFinish(_ controller: SFSafariViewController) {
        print("SafariVC is dismissed")
    }
}
于 2019-03-29T07:15:38.800 回答