如何在 iPhone 中单击按钮时调用 url
当用户使用此方法单击按钮时,我正在传递一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我确信错误是由于注意力不集中而发生的;-) 您正在传递一个 NSURL 实例作为参数,而您应该在其中传递一个 NSString。
就行了,
要么设置,
要么,
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,
either set,
or,
将此代码用于
NSURLRequest
。Use This Code for
NSURLRequest
.