macOS development - SafariServices

SafariServices


Description Framework

SafariServices can use Safari behavior or macOS iOS applications, or expand Safari.

  • Application provides almost the same UI and Safari. Users can browse the web in this view, then return to your application. Compared to implement a custom browser solutions, the view uses less methods to achieve the same effect with the Safari UI. (IOS)
  • Add items to the user's Safari reading list. (IOS)
  • In your application to determine whether the content is loaded blocker extension, if it is loaded, tell it to refresh its content. (IOS, macOS)
  • Safari application extensions to achieve. Determine whether Safari extensions are loaded in your application. (MacOS)
  • Cookies allow users to share data between applications and the network and Safari, by SFAuthenticationSession to get single sign-on (SSO) experience.

Classes and related use

1, showing the content of Safari (iOS) in your application

  • SFSafariExtensionViewController // display a web browsing standard view
  • SFAuthenticationCompletionHandler // user canceled or completes the login authorization session

Tutorial can refer to: iOS SFSafariViewController of 9 - CocoonJin - blog Park

demo:(swift) tutsplus/iOS-SafariViewControllerStarterProject: iOS 9: Getting Started with SFSafariViewController

Core code

- (IBAction)onButtonClick:(id)sender{

    NSString *urlString = @"http://www.baidu.com";

    SFSafariViewController *sfViewControllr = [[SFSafariViewController alloc] initWithURL:[NSURL URLWithString:urlString]];

    sfViewControllr.delegate = self;

    [self presentViewController:sfViewControllr animated:YES completion:^{

       //...

    }];
}

 
// Done 按钮
- (void)safariViewControllerDidFinish:(nonnull SFSafariViewController *)controller{

    [controller dismissViewControllerAnimated:YES completion:nil];

}

 
/*
SFSafariViewController 的接口比较少,就不再继续一一列举了。
另外一个定制功能在于 SFSafariViewControllerDelegate 里面的一个方法:

这个代理会在用户点击动作(Action)按钮(底部工具栏中间的按钮)的时候调用,可以传入UIActivity的数组,创建添加一些自定义的各类插件式的服务,比如分享到微信,微博什么的。
*/

-(NSArray<UIActivity *> *)safariViewController:(SFSafariViewController *)controller activityItemsForURL:(NSURL *)URL title:(nullable NSString *)title{
  
  return @[];
}


2, Safari reading list (iOS)


Tutorial may refer to: add a Web page to the Reading List --SSReadingList - Sail breeze - CSDN blog

Core code:

[SSReadingList supportsURL:[NSURL URLWithString:@"https://www.baidu.com"]]; //检测阅读列表是否支持添加该网页
[[SSReadingList defaultReadingList]addReadingListItemWithURL:[NSURL URLWithString:@"https://www.baidu.com"] title:@"baidu" previewText:@"hellofdn" error:nil];

/*
URL:需要添加到阅读列表的网页地址
title:网页的标题
previewText:预览的信息
errror:若添加出错后的错误信息,添加成功该值为nil
*/

3, content blocking

Official Tutorial: Creating A Content Blocker | the Apple Developer Documentation

Sample : samisharaf98/AdBlockerPlus: An AdBlocker extension for safari on iOS 9 and later.


4、Safari App Extensions (macOS 10.12)

  • SFSafariExtensionHandler

  • SFSafariExtensionState

  • SFSafariExtensionManager

  • SFSafariExtensionHandling agreement

  • SFSafariApplication

  • SFSafariExtensionViewController

  • SFSafariWindow // create not only can read

  • SFSafariTab

  • SFSafariToolbarItem

  • SFSafariPage

  • SFSafariPageProperties


Safari App Extensions | Apple Developer Documentation

Safari Extensions Development Guide

Building a Safari App Extension


Suitable for macOS 10.12 and later, or install the macOS 10.11.5 Safari 10's.


Safari application development can be read and adjust Web page content, add new features to Safari.

A Safari application development and local applications can communicate and share data. To the application and the web version provides a unified experience.

Here Insert Picture Description

The above is the main app Safari extension and shared data is described in FIG.

When you create a Safari extension project, extension as a target of the total project, the main target may be invoked.

Published 164 original articles · won praise 162 · Views 650,000 +

Guess you like

Origin blog.csdn.net/lovechris00/article/details/101849432