如何创建视图,可以远程更新,无需更新应用程序

时间:2014-04-18 17:47:19

标签: ios view

我需要创建一个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

2 个答案:

答案 0 :(得分:1)

我会使用类似Parse的内容,并在其中包含带有图像的记录。然后在应用程序中,您可以使用Parse SDK在每次显示该屏幕时获取该图像。您还可以使用Parses推送通知告诉用户存在新图像,如果您想要真正花哨使用iOS 7新的slient推送在后台获取新图像,以便用户甚至看不到它加载!

另一种选择是在某处托管图像,然后使用图像提取类别(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"]];

并使用Parse

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数据浏览器中添加以下内容: Setup

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

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

更新了您的代码:

#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

答案 1 :(得分:0)

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

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

相关问题