【问题标题】:How to tell when Safari is done loading a webpage如何判断 Safari 何时完成加载网页
【发布时间】:2012-05-25 19:08:38
【问题描述】:

我正在尝试确定是否可以跟踪 Safari 何时完成加载页面请求。我调用 Safari 以使用此代码打开:

// Open Safari
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"http://www.example.com/" description]]];

但是,由于我的应用程序进入后台,它可能是suspended (and not able to execute code)。这甚至可以在 Xcode 4 中完成吗?

编辑

之所以这样做是因为the UIWebView and Safari are not running at the same level和我想在一些网站上做一些性能相关的测试。

【问题讨论】:

  • 您的目标是 AppStore 还是越狱设备?我有越狱设备的解决方案。
  • 应用商店。但是,如果您可以发布越狱的那将是很棒的!

标签: objective-c ios macos


【解决方案1】:

所以,如果你想看看我是如何为越狱的 iOS 做的(而不是相当无聊的“不可能”):我基本上是用 safari 来访问页面完成时调用的特定方法闲置。使用 MobileSubstrate(注入动态库时过滤 com.apple.mobilesafari):

#import <substrate.h>
#import <UIKit/UIKit.h>

/* Some globals */
static IMP _orig_1, _orig_2;
static id tabController;

id _mod_1(id __self, SEL __cmd, CGRect frame, id tabDocument);
void _mod_2(id __self, SEL __cmd, id doc, BOOL error);


/* The library constructor */
__attribute__((constructor))
static void init()
{
    Class tabClass;

    tabClass = objc_getClass("TabController");

    MSHookMessageEx(tabClass, @selector(initWithFrame:tabDocument:),    
        (IMP)_mod_1, &_orig_1);
    MSHookMessageEx(tabClass, @selector(tabDocument:didFinishLoadingWithError:),
        (IMP)_mod_2, &_orig_2);
}


/* This hook merely captures the TabController of Safari. */
id _mod_1(id __self, SEL __cmd, CGRect frame, id tabDocument)
{
    __self = _orig_1(__self, __cmd, frame, tabDocument);
    tabController = __self;
    return __self;
}

/* This is called when the page loading is done */
void _mod_2(id __self, SEL __cmd, id doc, BOOL error)
{
    /* Make sure you always call the original method */
    _orig_2(__self, __cmd, doc, error);

    /* then do what you want */
}

【讨论】:

  • 有趣。感谢您发布此信息!
  • 没问题。希望有一天它会有用。
【解决方案2】:

这通常是不可能的。

您可以做的一件事是从要加载的页面,重定向到您的应用程序的 URL 方案,这将启动您的应用程序。有关此问题的更多信息,请查看-[UIApplicationDelegate application:openURL:sourceApplication:annotation:] 方法。

我认为您的应用程序不能通过这样的 URL 方案拒绝激活,因此这意味着您的应用程序将变为活动状态。也许上面方法的返回值(布尔值)可以使用,你必须测试一下。

此方法还假设您可以访问您正在打开的页面,以便您可以添加重定向。

【讨论】:

  • 我正在测试不完全是我的页面...是否可以在 Safari 网页中插入 Javascript 以在加载完成后重定向回我的应用程序?
  • 我猜没有,因为你实际上是在进行 XSS 攻击。
  • 从安全的角度来看这是有道理的......我可能认为也没有办法。
  • 很遗憾,您将不得不等待 Apple 将 UIWebView 更新为 Mobile Safari 中使用的快速 Javascript 引擎。也许iOS6?距离 WWDC 还有两周!
【解决方案3】:

不,这是不可能的。但是,如果您在自己的应用程序中打开 UIWebView 中的链接,则可以执行此操作。

【讨论】:

    猜你喜欢
    • 2017-09-18
    • 2018-03-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-04
    相关资源
    最近更新 更多