【问题标题】:How to create a view, that can be updated remotely without app store update [closed]如何创建一个无需应用商店更新即可远程更新的视图[关闭]
【发布时间】:2014-04-18 17:47:19
【问题描述】:

我需要创建一个 View 或 ImageView,无需重大 App Store 更新即可从计算机远程更新。所以基本上,它需要能够通过互联网浏览器或命令行工具进行更改。因此,例如今天我可以拥有一张图片,并且在某些日子里我可以远程更改它。是否有我需要实现的代码,或者是否有可以帮助我的服务?非常感谢各位!

这是目前为止的代码:

#import "ViewController.h"

 @interface ViewController ()
 @property (strong, nonatomic) IBOutlet UIImageView *imageView;

 @end

 @implementation ViewController



 - (void) viewDidLoad
 {
     [super viewDidLoad];
     //do stuff here
     if(&UIApplicationWillEnterForegroundNotification) { //needed to run on older devices,  otherwise you'll get EXC_BAD_ACCESS
         NSNotificationCenter *notificationCenter = [NSNotificationCenter defaultCenter];
         [notificationCenter addObserver:self selector:@selector(enteredForeground:)  name:UIApplicationWillEnterForegroundNotification object:nil];
     }


 }
 - (void)enteredForeground:(NSNotification*) not
{
    UIImageView *imageView; // Assumed to already be setup in a view controller
    PFQuery *query = [PFQuery queryWithClassName:@"Image"];
    [query getObjectInBackgroundWithId:@"nMW24stvhT" block:^(PFObject *imageObject, NSError *error) {

        if (!error) {
            PFFile *imageFile = imageObject[@"image"];
            [imageFile getDataInBackgroundWithBlock:^(NSData *data, NSError *error) {
                if (!error && data) {
                    UIImage *image = [UIImage imageWithData:data];
                    if (image) {
                        imageView.image = image;
                    }
                }
            }];
        }
    }];}

    - (void)didReceiveMemoryWarning
 {
 [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
 }

 @end

【问题讨论】:

  • 这个问题有点宽泛,但基本上您将图像/文件托管在服务器上,您的应用程序会偶尔查找更新的文件。当找到更新的文件时,应用程序会下载该文件并向用户显示新文件。

标签: ios view


【解决方案1】:

我会使用Parse 之类的东西,并有一个包含图像的记录。然后在应用程序中,您可以在每次显示该屏幕时使用 Parse SDK 来获取该图像。您还可以使用 Parses 推送通知来告诉用户存在新图像,如果您想要真正花哨,请使用 iOS 7 新的静默推送在后台获取新图像,这样用户甚至看不到它正在加载!

另一种选择是在某处托管图像,然后使用图像获取类别(AFNetworkingSDWebImage 都有一个)在屏幕显示时再次更新图像。

由于它只是一张图片,您最好将其托管在某个地方 - 我想这取决于您想要投入的精力:)

如果您自己托管并使用 AFNetworking 的一些示例代码:

UIImageView *imageView; // Assumed to already be setup in a view controller
[imageView setImageWithURL:[NSURL URLWithString:@"http://hostname.com/path/to/your/image.png"]];

和解析

UIImageView *imageView; // Assumed to already be setup in a view controller
PFQuery *query = [PFQuery queryWithClassName:@"Image"];
[query getObjectInBackgroundWithId:@"OBJECT ID HERE" block:^(PFObject *imageObject, NSError *error) {

     if (imageObject) {
          PFFile *imageFile = imageObject[@"image"];
          [imageFile getDataInBackgroundWithBlock:^(NSData *data, NSError *error) {
              if (data) {
                  UIImage *image = [UIImage imageWithData:data];
                  if (image) {
                      imageView.image = image;
                  }
              } else {
                  NSLog(@"Error fetching image file: %@", error);
              }
          }];
     } else {
          NSLog(@"Error fetching object: %@", error);
     }
 }];

然后在 Parse 数据浏览器中添加这样的东西:

基本上,创建一个名为Image 的新自定义类。向此类添加另一列,名为 image,类型为 File

然后添加一个文件,您将自动获得为您创建的objectId(上面的代码示例需要它):

用你的代码更新:

#import "ViewController.h"

 @interface ViewController ()
 @property (strong, nonatomic) IBOutlet UIImageView *imageView;

 @end

 @implementation ViewController

 -(void)dealloc
 {
     [[NSNotificationCenter defaultCenter] removeObserver:self];
 }

 - (void) viewDidLoad
 {
     [super viewDidLoad];
     //do stuff here
     NSNotificationCenter *notificationCenter = [NSNotificationCenter defaultCenter];
     [notificationCenter addObserver:self selector:@selector(enteredForeground:)  name:UIApplicationWillEnterForegroundNotification object:nil];   
 }

 -(void)viewWillAppear:(BOOL)animated
 {
     [super viewWillAppear:animated];

     [self updateImage];
 }

 - (void)enteredForeground:(NSNotification*) not
 {
     [self updateImage];
 }

 -(void)updateImage
 {

    PFQuery *query = [PFQuery queryWithClassName:@"Image"];
    [query getObjectInBackgroundWithId:@"nMW24stvhT" block:^(PFObject *imageObject, NSError *error) {

        if (imageObject) {
            PFFile *imageFile = imageObject[@"image"];
            [imageFile getDataInBackgroundWithBlock:^(NSData *data, NSError *error) {
                if (data) {
                    UIImage *image = [UIImage imageWithData:data];
                    if (image) {
                        self.imageView.image = image;
                    }
                } else {
                    NSLog(@"Error fetching image file: %@", error);
                }
            }];
        } else {
          NSLog(@"Error fetching object: %@", error);
        }
    }];
}


@end

【讨论】:

  • 我将如何使用 parse 或主机来做到这一点......我对 ios 完全陌生,需要帮助......你会推荐什么?
  • 嘿!你能给我一些示例代码吗?谢谢!
  • 您将创建一个 Parse 帐户,然后使用 SDK(您可以使用 Cocoapodss 添加)连接到 Parse 并获取图像。
  • @user3549694 在 Parse 文档中有一个很好的 guide。 Parse 入门指南也不错。
  • 如何获取图片?
【解决方案2】:

您可以让您的应用程序定期与服务器签入(或响应推送通知,具体取决于您的需求和资源)。然后它可以根据您在某处服务器上更新的 URL 文件下载图像。

或者,您可以让您的应用响应特殊的 URL 方案 (myApp://),并在网页上创建链接,点击这些链接会导致您的应用打开并执行某些操作。您可以详细了解自定义 URL 方案here

【讨论】:

  • 它如何与推送通知一起使用?
  • 通知基本上是应用程序检查服务器和下载新内容的指令。
  • 能否给我看示例代码或告诉我如何编写一些代码?哦,如果我没有服务器,该选择哪个托管服务?
  • 抱歉,我从未使用过推送通知。但是一旦应用程序运行,您可以使用Rich’s answer 中的AFNetworking 或SDWebImage 来获取图像。
  • 好的,但是代码会是什么样子?只是为了每隔几分钟检查一次?
猜你喜欢
  • 1970-01-01
  • 2016-08-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-07-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多