iphone - 在 iOS 应用程序中打开网页

标签 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 中,我希望它在应用程序内。 有人可以给我一个方法吗?

谢谢。

最佳答案

您需要使用 UIWebView,您可以将其作为 subview 添加到任何其他 View ,然后使用您的 URL 调用“loadRequest”。

关于iphone - 在 iOS 应用程序中打开网页,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8120417/

相关文章:

iphone - 如何将对象作为参数传递给 objective-c 中的新类构造函数?

ios - 字符串格式化程序中的内存泄漏

objective-c - 在给定特定分母的情况下,找到 float 小数的分子

iphone - 如何使用 NSUserDefaults 在 iOS 中保存非属性值?

iPhone 应用程序外部显示窗口在 iPad 上运行时放大/缩小

iphone - 更改 UIBarButtonItem 颜色

iphone - 单击引脚注释时如何显示另一个 View

ios - 交互式推送通知 - 隐藏/显示按钮

iphone - MapKit 添加注释 : not working

iPhone 和 iPad 开发 - 良好的设计架构以实现最大的可重用性