如何在iphone上点击按钮调用url

时间:2011-09-21 05:53:13

标签: ios objective-c nsurl

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

我在另一个Controller中编写了这段代码(处理连接的代码),这个Controller是从第一个视图中的button事件调用的。

特别是这行代码:

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

2 个答案:

答案 0 :(得分:3)

我确定错误是由于缺乏注意力而发生的;-)您正在传递 NSURL 实例作为参数,您应该传递 NSString 。< / p>

在线,

NSURLRequest *theRequest = [NSURLRequest requestWithURL:...

要么设置,

requestWithURL:url

,或者

requestWithURL:[NSURL URLWithString:urlString];

答案 1 :(得分:3)

将此代码用于NSURLRequest

NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url];