开发者

alert pop up in safari(iPhone)

How can i change the title (usual开发者_如何学运维ly the domain) that comes in a window.alert("message") popup in (Safari)iPhone ??


You would need to use the open-source framework PhoneGap (http://www.phonegap.com/).

Then, use:

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

via Javascript.

Edit: This would only be for developing a web app, not for a website. Changing the alert title is not possible.


You may use a generic version that works for both desktop/browser testing environment and PhoneGap/Native environment. Here is what worked for me:

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();
    }

}


For anyone wanting to do this without PhoneGap framework, you can pass the data to iOS, then show an alert.

In your webview delegate:

- (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;
}

In your 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);
    } 
}

Then call the alert function myAlert('Testing', 'One, Two, Three');

Notice, the scheme alert must match in the delegate function and the javascript href.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜