在iOS应用程序中打开网页

时间:2011-11-14 10:45:25

标签: iphone objective-c ipad ios4 ios5

我在启动画面上有6个按钮的应用程序。我的问题是我想打开网页点击这些按钮。我有这样的代码:

  - (IBAction) accomodation: (id) sender
{
    NSString *webPage1 = [NSString stringWithFormat:@"http://www.google.com"];
    NSURL *url = [NSURL URLWithString:webPage1];
//    [[UIApplication sharedApplication] openURL:url];
        [self beginNavigationWith:2 andTitle:NSLocalizedStringFromTable(@"icn_txt_accomodations", LANGID, nil) andMap:nil andHtml:nil withIdTagsAroundMe: 1 withWeb:url];
}

这是初始化其中一个按钮的方法。之后我有方法:

 - (void) beginNavigationWith: (int) navID andTitle:(NSString*) aTitle andMap:(NSString*) map andHtml:(NSString*) html withIdTagsAroundMe:(int)id_tags withWeb: (NSURL*) webPage
{
    // DIRECT LINK

    if (html != nil) {
        if ([html hasPrefix:@"{ID}"]) {
            NSError *error;

            int res_id = [[html substringFromIndex:4] intValue];
            NSLog(@"DIRECT ID From start>> %d", res_id);

            NSFetchRequest* reqForRestaurant = [[NSFetchRequest alloc] init];
            NSEntityDescription *resEntity2 = [NSEntityDescription entityForName:@"Restaurant" inManagedObjectContext:managedObjectContext];
            [reqForRestaurant setEntity:resEntity2];

            NSString *stringRes = [NSString stringWithFormat:@"res_id.intValue == %d", res_id];
            NSPredicate *resPredicate2 = [NSPredicate predicateWithFormat:stringRes];
            [reqForRestaurant setPredicate:resPredicate2];

            NSArray *array3 = [managedObjectContext executeFetchRequest:reqForRestaurant error:&error];


            //NSLog(@">>>> Gallery:%d", [managedObject.res_gallery intValue]);

            if (array3 != nil) {
                //NSLog(@"Restaurants count=%d", [array2 count]);
                if ([array3 count] == 1) {

                    Restaurant *managedObjectRes = (Restaurant*) [array3 objectAtIndex:0];

                    RestaurantDetails *details = [[RestaurantDetails alloc] initWithStyle:UITableViewStylePlain];
                    details.managedObjectContext = self.managedObjectContext;
                    details.parentID = navID;
                    details.title = managedObjectRes.res_title;
                    [details initItemsWithData:managedObjectRes];   
                    [self.navigationController pushViewController:details animated:YES];
                                }
            }

            return;
        }
    }

    // STANDARD NAV o puro HTML



    NavTableView *navTable = [[NavTableView alloc] initWithStyle:UITableViewStylePlain];
    navTable.title = aTitle;
    navTable.managedObjectContext = self.managedObjectContext;
    navTable.selectQuery = [NSPredicate predicateWithFormat:@"nav_parent_id.intValue == %d", navID]; // parent id == 0 is ROOT
    //added for use around me
    navTable.id_tags_arroundme = id_tags;

    [self.navigationController pushViewController:navTable animated:YES];

    if (html != nil) [navTable addHtmlHeader:html];
    if (map != nil) [navTable addActiveMap:map];

    //WEB PAGE

    if ( webPage != nil ) {

        NSString *webPage1 = [NSString stringWithFormat:@"http://www.google.com"];
        NSURL *url = [NSURL URLWithString:webPage1];
        NSLog(@"url = %@",url);
        [[UIApplication sharedApplication] openURL:url];

    }



}

通过这种方式,我设法打开网页,但在Saffari中,我希望它在应用程序中。 有人能给我一个办法吗。

感谢。

1 个答案:

答案 0 :(得分:2)

您需要使用UIWebView,您可以将其添加为任何其他视图的子视图,然后使用您的URL调用'loadRequest'。