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

Posted

技术标签:

【中文标题】如何在iphone中单击按钮时调用url【英文标题】:How to call url on button click in iphone 【发布时间】:2011-09-21 05:53:13 【问题描述】:

当用户使用此方法单击按钮时,我传递了一个 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];

【问题讨论】:

你做错了什么是你在上面的代码行中将 NSUrl 分配给一个字符串对象,[NSURLRequest requestWithURL:[NSURL URLWithString:url],在这一行中,url 不是一个 NSString,因为它是一个预期的参数网址。 【参考方案1】:

我确定错误是由于注意力不集中造成的 ;-) 您正在传递一个 NSURL 实例作为您应该传递一个 NSString 的参数。

上线,

NSURLRequest *theRequest = [NSURLRequest requestWithURL:...

任何一组,

requestWithURL:url

或者,

requestWithURL:[NSURL URLWithString:urlString];

【讨论】:

【参考方案2】:

将此代码用于NSURLRequest

NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url];

【讨论】:

以上是关于如何在iphone中单击按钮时调用url的主要内容,如果未能解决你的问题,请参考以下文章

如何在vuejs中的父组件中单击按钮时调用子组件

Django admin:如何在单击自定义按钮时调用操作?

Python - 如何在单击按钮时调用的函数中使用变量的值

Python Kivy:如何在按钮单击时调用函数?

如何在动态操作中单击按钮时调用Oracle APEX中的javascript函数?

如何使用Angular 5在单击按钮时调用多个方法?