Solve the title problem of JS confirm conversion to iOS Uialertview

Source: Internet
Author: User

When using Cordova for cross-platform development, the Uialertview title of the JS call confirm converted to iOS is displayed as a xxxx.html problem.

The first thing I think about is listening to uiapplication windows and then manually modifying the title of the current alert. Previously did a similar monitoring system notification, modify the style. However, it is much less efficient to interact with JS.

Another way of thinking is if it is better to find confirm into alert? This allows the method to be overloaded and replaced with any style.

On the StackOverflow really found, wow haha.

@interface UIWebView (Javascriptalert)

-(void) WebView: (uiwebview *) sender Runjavascriptalertpanelwithmessage: (nsstring *) Message initiatedbyframe: (ID) frame;

-(BOOL) WebView: (uiwebview *) sender Runjavascriptconfirmpanelwithmessage: (nsstring *) Message initiatedbyframe: (ID) frame;

-(nsstring *) WebView: (uiwebview *) View runjavascripttextinputpanelwithprompt: (nsstring *) Prompt DefaultText: (nsstring *) text initiatedbyframe: (ID) frame;

@end


Then I searched again on the csdn and found someone else's realization, and finally decided it was in this

-(BOOL) WebView: (uiwebview *) sender Runjavascriptconfirmpanelwithmessage: (nsstring *) Message initiatedbyframe: (ID) Frame {


The style of the alert is modified, and the method needs to return the index of the currently selected alert, which means that the current thread must be suspended before the alert is clicked. This is used to run the loop runloop.

The methods found on the Internet are basically outdated, and I have slightly modified the following:

#import "Uiwebview+javascriptalert.h"


@implementation UIWebView (Javascriptalert)

Static BOOL status = NO;

Static BOOL isend =NO;


-(void) WebView: (uiwebview *) sender Runjavascriptalertpanelwithmessage: (nsstring *) Message initiatedbyframe: (ID) Frame {

Uialertview* Customalert = [[uialertviewalloc] initwithtitle:Nil

message: Message

delegate:Nil

cancelbuttontitle:@ " OK "

otherbuttontitles:nil];

[Customalertshow];

}


-(nsstring *) WebView: (uiwebview *) View runjavascripttextinputpanelwithprompt: (nsstring *) Prompt DefaultText: (nsstring *) text initiatedbyframe: (ID) Frame {

return @"";

}


-(BOOL) WebView: (uiwebview *) sender Runjavascriptconfirmpanelwithmessage: (nsstring *) Message initiatedbyframe: (ID) Frame {

uialertview *confirmdiag = [[uialertviewalloc] initwithtitle:Nil

message: Message

delegate: Self

cancelbuttontitle:@ " cancel "

otherbuttontitles:@ " OK ",Nil];

[Confirmdiagshow];

cgfloat Version = [[[uidevicecurrentdevice] systemversion ]floatvalue];

if (Version >= 7.) {

while (isend = =NO) {

[ [nsrunloopmainrunloop] rununtildate: [nsdatedat Ewithtimeintervalsincenow:0.01f];


}

}Else

{

while (isend = =NO && confirmdiag. superview ! =Nil) {

[ [nsrunloopmainrunloop] rununtildate: [nsdatedat Ewithtimeintervalsincenow:0.01f];

}

}

isend = NO;

return status;

}


-(void) Alertview: (uialertview *) Alertview clickedbuttonatindex: (nsinteger) buttonindex {

status = Buttonindex;

isend = YES;

}

@end


Version compatible that piece did not test, because I only do iOS7, 8, no device test. Because it is a method of classifying overloads, dragging this category directly into the project does not require adding any code to achieve the desired effect.




Solve the title problem of JS confirm conversion to iOS Uialertview

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.