在safari中弹出警报(iPhone)

时间:2010-11-02 12:26:09

标签: javascript iphone safari

如何更改(Safari)iPhone中的window.alert(“message”)弹出窗口中的标题(通常是域名)?

3 个答案:

答案 0 :(得分:5)

您需要使用开源框架PhoneGap(http://www.phonegap.com/)。

然后,使用:

navigator.notification.alert("message", callback, "title", "button title");

通过Javascript。

编辑:这只适用于开发网络应用,而不适用于网站。无法更改提醒标题。

答案 1 :(得分:2)

您可以使用适用于桌面/浏览器测试环境和PhoneGap / Native环境的通用版本。这对我有用:

function showMessage(message, title, callback, buttonName){

    title = title || "";
    buttonName = buttonName || 'OK';

    if(navigator.notification){

        navigator.notification.alert(
            message,    // message
            callback,   // callback
            title,      // title
            buttonName  // buttonName
        );

    }else{

        alert(message);

        if(callback)
            callback();
    }

}

答案 2 :(得分:0)

对于想要在没有PhoneGap框架的情况下执行此操作的任何人,您可以将数据传递到iOS,然后显示警报。

在您的网络视图委托中:

- (BOOL) webView:(UIWebView*)webView
        shouldStartLoadWithRequest:(NSURLRequest*)request
        navigationType:(UIWebViewNavigationType)type {
    NSURL* url = [request URL];
    NSString* scheme;
    NSString* host;
    NSString* path;
    BOOL isRealUrl = YES;

    switch (type) {
        case UIWebViewNavigationTypeLinkClicked:
            // Open link in Safari
            [[UIApplication sharedApplication] openURL:url];
            return NO;
            break;

        case UIWebViewNavigationTypeFormSubmitted:
        case UIWebViewNavigationTypeOther:
            scheme = [url scheme];
            host = [url host];
            path = [url path];

            if ([scheme isEqualToString:@"alert"]) {
                [[[UIAlertView alloc] initWithTitle:host
                                            message:[path substringFromIndex:1]
                                          delegate:nil
                                 cancelButtonTitle:@"OK"
                                 otherButtonTitles:nil] show];
                isRealUrl = NO;
            } else {
                // Go to another page in your app. 
                isRealUrl = YES;
            }
            break;

        default:
            break;
    }

    return isRealUrl;
}

在你的javascript中:

function myAlert(message, title) {
    if (/iphone|ipod|ipad/.test(navigator.userAgent) &&
            !/safari/i.test(navigator.userAgent)) {
        document.location.href = 'alert://' + encodeURIComponent(title) + '/' +
            encodeURIComponent(message);
    } else {
        alert(message);
    } 
}

然后调用警报功能myAlert('Testing', 'One, Two, Three');

注意,方案alert必须在委托函数和javascript href中匹配。

相关问题