2014-11-04 62 views
0

我是iOS新手。我想打開從www開始的鏈接。我試圖追加http.But它不是幹活。我點擊鏈接以http是開放的,但不是以www請Help.I很感謝所有幫助.....我們如何打開從www開始的鏈接

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType 
{ 
    if (webView==myWebView) { 
     if ([request.URL.absoluteString rangeOfString:@"http://"].location != NSNotFound) { 
      [[UIApplication sharedApplication] openURL:request.URL]; 
      return NO; 
     } 

     else if([request.URL.absoluteString rangeOfString:@"www."].location != NSNotFound) { 
         [request.URL.absoluteString stringByReplacingOccurrencesOfString:@"www." withString:@"http://www."]; 

      [[UIApplication sharedApplication] openURL:request.URL]; 
      NSLog(@"%@",request.URL); 
      return NO; 
     } 

     else { 
      return YES; 
     } 
    } else { 
     if ([request.URL.absoluteString rangeOfString:@"http://"].location != NSNotFound) { 
      NSString *tempString=[request.URL.absoluteString stringByReplacingOccurrencesOfString:@"http://" withString:@""]; 
      NSArray *itemArray = [tempString componentsSeparatedByString:@","]; 
      NSDictionary *dict; 
      NSString *[email protected]""; 
      if ([[itemArray objectAtIndex:0] integerValue]==0) { 
       dict=[toArray objectAtIndex:[[itemArray objectAtIndex:1] integerValue]]; 
       [email protected]"Recipient"; 
      } else if ([[itemArray objectAtIndex:0] integerValue]==1) { 
       dict=[ccArray objectAtIndex:[[itemArray objectAtIndex:1] integerValue]]; 
       [email protected]"Cc"; 
      } else if ([[itemArray objectAtIndex:0] integerValue]==2) { 
       dict=[bccArray objectAtIndex:[[itemArray objectAtIndex:1] integerValue]]; 
       [email protected]"Bcc"; 
      } 
      [appDelegate.navigationController setNavigationBarHidden:NO]; 
      [self showUnknownPersonViewController:[dict objectForKey:@"address"] :[dict objectForKey:@"name"]:title]; 
      return NO; 
     } else { 
      return YES; 
     } 
    } 
} 
+0

因爲它不能工作阻止。 – varun 2014-11-04 07:14:58

回答

0

你是使用'stringByReplacingOccurrencesOfString',但不使用該函數的返回值。使用返回的值創建一個新的URL:

NSString *newURLString = [request.URL.absoluteString stringByReplacingOccurrencesOfString:@"www." withString:@"http://www."]; 
NSURL *newURL = [NSURL URLWithString:newURLString]; 
[[UIApplication sharedApplication] openURL:newURL]; 
NSLog(@"%@",newURL); 
return NO;