我有一个简单的视图控制器,当点击链接按钮时会显示一个 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”的行处设置了一个断点,并确认在该点正确设置了代表。我错过了什么?