如何在Objective-c中使用SafariViewController ?

时间:2022-09-07 08:13:28

Apple rejected my app today suggesting, among other things, I shouldn't force my users out to Safari to visit my web page. They suggested using SafariWebController which is new in iOS9. I went searching for guidance and only found Swift tutorials.

苹果拒绝了我今天的应用,其中一个建议是,我不应该强迫我的用户去Safari浏览我的网页。他们建议使用iOS9中的新SafariWebController。我去寻找指导,只找到了快速教程。

I had been using the following to launch Safari, associated with a button:

我一直在使用下面的按钮来启动Safari浏览器:

NSURL *url = [NSURL URLWithString:WEBSITE_REGISTRATION_URL];
[[UIApplication sharedApplication] openURL:url];

So, I'll share my simple configuration for those of us trying to keep up who are not yet on Swift.

因此,我将分享我的简单配置,以供那些试图跟上我们谁还没有在斯威夫特。

1 个解决方案

#1


23  

There are 5 steps:

有5个步骤:

  1. Click on your project target and then select "Build Phases". Under "Build Phases", click on the arrow by "Link Binary with Libraries" and click the plus. Search for SafariServices in the window that will open. Highlight it in the list and click "Add". 如何在Objective-c中使用SafariViewController ?

    单击项目目标,然后选择“构建阶段”。在“构建阶段”下,单击箭头“与库链接二进制”,并单击加号。在将要打开的窗口中搜索安全服务。在列表中突出显示并单击“添加”。

  2. Import the library into .m file where your button resides:

    将库导入按钮所在的.m文件:

    #import <SafariServices/SafariServices.h>
    
  3. Define a class extension in your .m file to conform to Safari VC protocol:

    在.m文件中定义一个类扩展名,以符合Safari VC协议:

    @interface SKWelcomeViewController () <SFSafariViewControllerDelegate>
    
  4. Implement the following delegate method that will be called when the Safari View Controller needs to be dismissed dismissed:

    实现以下委托方法,当Safari视图控制器需要被驳回时调用:

    - (void)safariViewControllerDidFinish:(SFSafariViewController *)controller {
        [self dismissViewControllerAnimated:true completion:nil];
    }
    
  5. And finally, the code inside the IBAction:

    最后,IBAction中的代码:

    SFSafariViewController *svc = [[SFSafariViewController alloc] initWithURL:url];
    svc.delegate = self;
    [self presentViewController:svc animated:YES completion:nil];
    

Enjoy!

享受吧!

#1


23  

There are 5 steps:

有5个步骤:

  1. Click on your project target and then select "Build Phases". Under "Build Phases", click on the arrow by "Link Binary with Libraries" and click the plus. Search for SafariServices in the window that will open. Highlight it in the list and click "Add". 如何在Objective-c中使用SafariViewController ?

    单击项目目标,然后选择“构建阶段”。在“构建阶段”下,单击箭头“与库链接二进制”,并单击加号。在将要打开的窗口中搜索安全服务。在列表中突出显示并单击“添加”。

  2. Import the library into .m file where your button resides:

    将库导入按钮所在的.m文件:

    #import <SafariServices/SafariServices.h>
    
  3. Define a class extension in your .m file to conform to Safari VC protocol:

    在.m文件中定义一个类扩展名,以符合Safari VC协议:

    @interface SKWelcomeViewController () <SFSafariViewControllerDelegate>
    
  4. Implement the following delegate method that will be called when the Safari View Controller needs to be dismissed dismissed:

    实现以下委托方法,当Safari视图控制器需要被驳回时调用:

    - (void)safariViewControllerDidFinish:(SFSafariViewController *)controller {
        [self dismissViewControllerAnimated:true completion:nil];
    }
    
  5. And finally, the code inside the IBAction:

    最后,IBAction中的代码:

    SFSafariViewController *svc = [[SFSafariViewController alloc] initWithURL:url];
    svc.delegate = self;
    [self presentViewController:svc animated:YES completion:nil];
    

Enjoy!

享受吧!