如何在 iPhone 中单击按钮时调用 url

发布于 2024-12-06 02:32:54 字数 3340 浏览 0 评论 0原文

当用户使用此方法单击按钮时,我正在传递一个 URL,但我的应用程序崩溃了,我不知道我做错了什么。

我已经在另一个控制器中编写了这段代码(处理连接的代码),该控制器是从第一个视图中的按钮事件调用的。

特别是,这行代码:

NSURLRequest *theRequest=[NSURLRequest requestWithURL:[NSURL URLWithString:url]cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0];

产生以下错误:

/Users/pradeepyadav/Desktop/RegexKitLiteDemo/Classes/SearchView.m:19:0 /Users/pradeepyadav/Desktop/RegexKitLiteDemo/Classes/SearchView.m:19:警告:不兼容的 Objective-C 类型“struct NSURL *” ,当从不同的 Objective-C 类型传递“URLWithString:”的参数 1 时,预期为“struct NSString *”

这是完整的代码:

- (void)LoadAirport

{    
    NSString *urlString = [NSString stringWithFormat:@"http://www.bookryanair.com/SkySales/FRSearch.aspx?AvailabilitySearchInputFRSearchView%24ButtonSubmit=Search%20For%20Flights%20&AvailabilitySearchInputFRSearchView%24DropDownListMarketDay1=<%i>&AvailabilitySearchInputFRSearchView%24DropDownListMarketDay2=<%i>&AvailabilitySearchInputFRSearchView%24DropDownListMarketDestination1=<%@>&AvailabilitySearchInputFRSearchView%24DropDownListMarketMonth1=<%i>&AvailabilitySearchInputFRSearchView%24DropDownListMarketMonth2=<%i>&AvailabilitySearchInputFRSearchView%24DropDownListMarketOrigin1=<%@>&AvailabilitySearchInputFRSearchView%24DropDownListPassengerType_ADT=1&AvailabilitySearchInputFRSearchView%24DropDownListPassengerType_CHD=0&AvailabilitySearchInputFRSearchView%24DropDownListPassengerType_INFANT=0&AvailabilitySearchInputFRSearchView%24RadioButtonFlowSelector=FlightAndCar&AvailabilitySearchInputFRSearchView%24RadioButtonMarketStructure=RoundTrip&AvailabilitySearchInputFRSearchView%24discountPax=0&__EVENTARGUMENT=&__EVENTTARGET=&__VIEWSTATE=%2FwEPDwUBMGRkg4UKvNNb1NbM14%2F2n9zUxhNQ%2B%2BA%3D&errorlist=",day,day1,DestinCode,fromDate1,fromDate2,OriginCode];
    NSURL *url = [NSURL URLWithString:urlString];
    NSURLRequest *theRequest=[NSURLRequest requestWithURL:[NSURL URLWithString:url]cachePolicy:NSURLRequestUseProtocolCachePolicy
                                          timeoutInterval:60.0];
    NSURLConnection *theConnection=[[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
    if (theConnection) {
        webData = [[NSMutableData data] retain];
        NSLog(@"%@",webData);
    } else {

    }
}

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response 
{
    //responseData = [[NSMutableData alloc] init];
    [webData setLength: 0]; 
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data 
{
    //[responseData appendData:data];
    [webData appendData:data]; 
}

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error 
{
    [connection release];
    [webData release]; 
    NSLog(@"Connection failed! Error - %@ %@",
          [error localizedDescription],
          [[error userInfo] objectForKey:NSURLErrorFailingURLStringErrorKey]);
    //[textView setString:@"Unable to fetch data"];
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection 
{
    loginStatus = [[NSString alloc] initWithBytes: [webData mutableBytes] length:[webData length] encoding:NSUTF8StringEncoding]; 
    [connection release];
}

I am passing a URL when the user clicks on a button with this method, but my application crashes and I don't know what am I doing wrong.

I've written this code (the one that handles the connection) in another Controller which is being called from the button event in the first view.

In particular, this line of code:

NSURLRequest *theRequest=[NSURLRequest requestWithURL:[NSURL URLWithString:url]cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0];

Produces the following error:

/Users/pradeepyadav/Desktop/RegexKitLiteDemo/Classes/SearchView.m:19:0 /Users/pradeepyadav/Desktop/RegexKitLiteDemo/Classes/SearchView.m:19: warning: incompatible Objective-C types 'struct NSURL *', expected 'struct NSString *' when passing argument 1 of 'URLWithString:' from distinct Objective-C type

Here's the full code:

- (void)LoadAirport

{    
    NSString *urlString = [NSString stringWithFormat:@"http://www.bookryanair.com/SkySales/FRSearch.aspx?AvailabilitySearchInputFRSearchView%24ButtonSubmit=Search%20For%20Flights%20&AvailabilitySearchInputFRSearchView%24DropDownListMarketDay1=<%i>&AvailabilitySearchInputFRSearchView%24DropDownListMarketDay2=<%i>&AvailabilitySearchInputFRSearchView%24DropDownListMarketDestination1=<%@>&AvailabilitySearchInputFRSearchView%24DropDownListMarketMonth1=<%i>&AvailabilitySearchInputFRSearchView%24DropDownListMarketMonth2=<%i>&AvailabilitySearchInputFRSearchView%24DropDownListMarketOrigin1=<%@>&AvailabilitySearchInputFRSearchView%24DropDownListPassengerType_ADT=1&AvailabilitySearchInputFRSearchView%24DropDownListPassengerType_CHD=0&AvailabilitySearchInputFRSearchView%24DropDownListPassengerType_INFANT=0&AvailabilitySearchInputFRSearchView%24RadioButtonFlowSelector=FlightAndCar&AvailabilitySearchInputFRSearchView%24RadioButtonMarketStructure=RoundTrip&AvailabilitySearchInputFRSearchView%24discountPax=0&__EVENTARGUMENT=&__EVENTTARGET=&__VIEWSTATE=%2FwEPDwUBMGRkg4UKvNNb1NbM14%2F2n9zUxhNQ%2B%2BA%3D&errorlist=",day,day1,DestinCode,fromDate1,fromDate2,OriginCode];
    NSURL *url = [NSURL URLWithString:urlString];
    NSURLRequest *theRequest=[NSURLRequest requestWithURL:[NSURL URLWithString:url]cachePolicy:NSURLRequestUseProtocolCachePolicy
                                          timeoutInterval:60.0];
    NSURLConnection *theConnection=[[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
    if (theConnection) {
        webData = [[NSMutableData data] retain];
        NSLog(@"%@",webData);
    } else {

    }
}

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response 
{
    //responseData = [[NSMutableData alloc] init];
    [webData setLength: 0]; 
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data 
{
    //[responseData appendData:data];
    [webData appendData:data]; 
}

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error 
{
    [connection release];
    [webData release]; 
    NSLog(@"Connection failed! Error - %@ %@",
          [error localizedDescription],
          [[error userInfo] objectForKey:NSURLErrorFailingURLStringErrorKey]);
    //[textView setString:@"Unable to fetch data"];
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection 
{
    loginStatus = [[NSString alloc] initWithBytes: [webData mutableBytes] length:[webData length] encoding:NSUTF8StringEncoding]; 
    [connection release];
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

心房的律动 2024-12-13 02:32:54

我确信错误是由于注意力不集中而发生的;-) 您正在传递一个 NSURL 实例作为参数,而您应该在其中传递一个 NSString

就行了,

NSURLRequest *theRequest = [NSURLRequest requestWithURL:...

要么设置,

requestWithURL:url

要么,

requestWithURL:[NSURL URLWithString:urlString];

I am sure the error occurred due to the lack of concentration ;-) You are passing an NSURL instance as argument where you are supposed to pass a NSString.

On the line,

NSURLRequest *theRequest = [NSURLRequest requestWithURL:...

either set,

requestWithURL:url

or,

requestWithURL:[NSURL URLWithString:urlString];
潜移默化 2024-12-13 02:32:54

将此代码用于 NSURLRequest

NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url];

Use This Code for NSURLRequest.

NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文